Commit 01e21767 by Иван Кубота

store version updater

parent 8367742b
...@@ -20,6 +20,12 @@ const fs = require( 'fs' ); ...@@ -20,6 +20,12 @@ const fs = require( 'fs' );
var cache = {}; var cache = {};
const {execSync} = require('child_process');
let commit;
const updateCommitInfo = function() {
commit = execSync('git rev-parse HEAD').toString().trim();
};
updateCommitInfo();
const serveBundle = async function(tpl, res){ const serveBundle = async function(tpl, res){
var fileName = dir(pack.src, pack.entry.js); var fileName = dir(pack.src, pack.entry.js);
...@@ -30,10 +36,12 @@ const serveBundle = async function(tpl, res){ ...@@ -30,10 +36,12 @@ const serveBundle = async function(tpl, res){
return res.end(err.message); return res.end(err.message);
} }
const cacheEntry = posixName;//+new Date()+'_'+Math.random().toString(36).substr(2) const cacheEntry = posixName;//+new Date()+'_'+Math.random().toString(36).substr(2)
updateCommitInfo();
res.end(tpl res.end(tpl
//.replace('$DATA$', JSON.stringify(data)) //.replace('$DATA$', JSON.stringify(data))
.replace('$BUNDLE$', .replace('$BUNDLE$',
'<script src="'+cacheEntry+'"></script>') '<script src="'+cacheEntry+'"></script>')
.replace('$COMMIT$', '<script>var CommitID = "'+commit+'";</script>')
); );
transformJSX(jsx+'', pack.entry.js, function(err, result) { transformJSX(jsx+'', pack.entry.js, function(err, result) {
......
...@@ -7,7 +7,9 @@ export const AsyncAuthAjax = { ...@@ -7,7 +7,9 @@ export const AsyncAuthAjax = {
return false; return false;
return await AsyncAjax.get(url, { return await AsyncAjax.get(url, {
authorization: 'Bearer '+ store.get('account.token') headers: {
authorization: 'Bearer ' + store.get( 'account.token' )
}
}) })
}, },
async post(url, data) { async post(url, data) {
...@@ -15,7 +17,9 @@ export const AsyncAuthAjax = { ...@@ -15,7 +17,9 @@ export const AsyncAuthAjax = {
return false; return false;
return await AsyncAjax.post(url, data, { return await AsyncAjax.post(url, data, {
authorization: 'Bearer '+ store.get('account.token') headers: {
authorization: 'Bearer ' + store.get( 'account.token' )
}
}) })
} }
}; };
\ No newline at end of file
...@@ -48,9 +48,10 @@ export const Ajax = { ...@@ -48,9 +48,10 @@ export const Ajax = {
let stringData=''; let stringData='';
try{ try{
stringData = JSON.stringify(data); stringData = JSON.stringify(data);
const xhr = new XMLHttpRequest();
cfg = transformCfg(cfg || {}); cfg = transformCfg(cfg || {});
cfg.url = url; cfg.url = url;
const xhr = new XMLHttpRequest();
xhrProceed("POST", xhr, cfg, cb); xhrProceed("POST", xhr, cfg, cb);
xhr.send(stringData); xhr.send(stringData);
}catch (e) { }catch (e) {
...@@ -65,11 +66,9 @@ export const Ajax = { ...@@ -65,11 +66,9 @@ export const Ajax = {
get(url, cb, cfg){ get(url, cb, cfg){
try{ try{
const xhr = new XMLHttpRequest();
cfg = transformCfg(cfg || {}); cfg = transformCfg(cfg || {});
cfg.url = url; cfg.url = url;
xhr.open("GET", url, true); const xhr = new XMLHttpRequest();
xhrProceed(xhr, url, cb);
xhrProceed("GET", xhr, cfg, cb); xhrProceed("GET", xhr, cfg, cb);
xhr.send(null); xhr.send(null);
}catch (e) { }catch (e) {
......
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
<script src="core/data/store/Store.js"></script> <script src="core/data/store/Store.js"></script>
$COMMIT$
<script src="model/Store.js"></script> <script src="model/Store.js"></script>
$BUNDLE$ $BUNDLE$
</head> </head>
<body> <body>
......
const store = new Store({ const store = new Store({
commit: window.CommitID,
'navigation': { 'navigation': {
current: 'login' current: 'login'
}, },
...@@ -15,8 +16,25 @@ const store = new Store({ ...@@ -15,8 +16,25 @@ const store = new Store({
try{ try{
var data = JSON.parse( localStorage.getItem( 'store' ) ); var data = JSON.parse( localStorage.getItem( 'store' ) );
window.addEventListener && console.log(data) window.addEventListener && console.log(data)
for(var k in data){ if(window.CommitID !== store.get('commit')){
store.set(k, data[k]); console.warn('STORE: warn outdated:');
console.warn('\t\tCurrent data: '+ store.get('commit'), store._props);
console.warn('\t\tSaved data: '+ window.CommitID, data);
console.warn('\tRun restoreStore() for restore saved state');
window.restoreStore = function() {
for( var k in data ){
if(k !== 'commit'){
store.set( k, data[ k ] );
}
}
};
}else{
for( var k in data ){
store.set( k, data[ k ] );
}
console.log('STORE: loaded from localStorage');
} }
}catch( e ){} }catch( e ){}
store.on('change', function() { store.on('change', function() {
......
...@@ -2,21 +2,17 @@ import Button from "/view/cmp/button/Button.jsx"; ...@@ -2,21 +2,17 @@ import Button from "/view/cmp/button/Button.jsx";
import LabeledField from "../../cmp/field/LabeledField.jsx"; import LabeledField from "../../cmp/field/LabeledField.jsx";
import PhoneInput from "../../cmp/field/PhoneInput.jsx"; import PhoneInput from "../../cmp/field/PhoneInput.jsx";
import Input from "../../cmp/field/Input"; import Input from "../../cmp/field/Input";
import { AsyncAuthAjax } from "../../../controller/Ajax";
import {API} from "../../../dict/Consts";
const TestRequests = D.declare('view.page.tmp.TestRequests', () => { const TestRequests = D.declare('view.page.tmp.TestRequests', () => {
var data = new Store({ let response;
phone: '',
phone3: '7',
phone4: '8',
phone5: '79132251365',
phone2: '894',
});
return <div> return <div>
<div>
<h5>Labeled field</h5>
<LabeledField label={'Label'}/>
</div>
Курвалол Курвалол
<button onclick={async()=>{
const result = await AsyncAuthAjax.get(API.ENDPOINTS.GET_USER(store.get('account.userID')));
response.innerText = JSON.stringify(result, null, 2);
}}>Get user</button>
<div className='error-block'>{response = <div></div>}</div>
</div> </div>
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment