Do not expose the store methods until it is ready

This commit is contained in:
yflory 2017-02-09 12:56:05 +01:00
parent 67d881b2cf
commit fc3995054d

View File

@ -16,44 +16,44 @@ define([
*/ */
var Store = {}; var Store = {};
var storeObj; var store;
var ready = false;
var filesOp; var initStore = function (filesOp, storeObj, exp) {
var exp = {}; var ret = {};
var safeSet = function (key, val) { var safeSet = function (key, val) {
storeObj[key] = val; storeObj[key] = val;
}; };
// Store uses nodebacks... // Store uses nodebacks...
Store.set = function (key, val, cb) { ret.set = function (key, val, cb) {
safeSet(key, val); safeSet(key, val);
cb(); cb();
}; };
// implement in alternative store // implement in alternative store
Store.setBatch = function (map, cb) { ret.setBatch = function (map, cb) {
Object.keys(map).forEach(function (key) { Object.keys(map).forEach(function (key) {
safeSet(key, map[key]); safeSet(key, map[key]);
}); });
cb(void 0, map); cb(void 0, map);
}; };
Store.setDrive = function (key, val, cb) { ret.setDrive = function (key, val, cb) {
storeObj.drive[key] = val; storeObj.drive[key] = val;
cb(); cb();
}; };
var safeGet = window.safeGet = function (key) { var safeGet = function (key) {
return storeObj[key]; return storeObj[key];
}; };
Store.get = function (key, cb) { ret.get = function (key, cb) {
cb(void 0, safeGet(key)); cb(void 0, safeGet(key));
}; };
// implement in alternative store // implement in alternative store
Store.getBatch = function (keys, cb) { ret.getBatch = function (keys, cb) {
var res = {}; var res = {};
keys.forEach(function (key) { keys.forEach(function (key) {
res[key] = safeGet(key); res[key] = safeGet(key);
@ -61,7 +61,7 @@ define([
cb(void 0, res); cb(void 0, res);
}; };
Store.getDrive = function (key, cb) { ret.getDrive = function (key, cb) {
cb(void 0, storeObj.drive[key]); cb(void 0, storeObj.drive[key]);
}; };
@ -69,80 +69,63 @@ define([
delete storeObj[key]; delete storeObj[key];
}; };
Store.remove = function (key, cb) { ret.remove = function (key, cb) {
safeRemove(key); safeRemove(key);
cb(); cb();
}; };
// implement in alternative store // implement in alternative store
Store.removeBatch = function (keys, cb) { ret.removeBatch = function (keys, cb) {
keys.forEach(function (key) { keys.forEach(function (key) {
safeRemove(key); safeRemove(key);
}); });
cb(); cb();
}; };
Store.keys = function (cb) { ret.keys = function (cb) {
cb(void 0, Object.keys(storeObj)); cb(void 0, Object.keys(storeObj));
}; };
Store.addPad = function (href, path, name) { ret.addPad = function (href, path, name) {
filesOp.addPad(href, path, name); filesOp.addPad(href, path, name);
}; };
Store.forgetPad = function (href, cb) { ret.forgetPad = function (href, cb) {
filesOp.forgetPad(href); filesOp.forgetPad(href);
cb(); cb();
}; };
Store.addTemplate = function (href) { ret.addTemplate = function (href) {
filesOp.addTemplate(href); filesOp.addTemplate(href);
}; };
Store.listTemplates = function () { ret.listTemplates = function () {
return filesOp.listTemplates(); return filesOp.listTemplates();
}; };
Store.getProxy = function () { ret.getProxy = function () {
return exp; return exp;
}; };
Store.getLoginName = function () { ret.getLoginName = function () {
return storeObj.login_name; return storeObj.login_name;
}; };
var changeHandlers = Store.changeHandlers = []; var changeHandlers = ret.changeHandlers = [];
Store.change = function (f) { ret.change = function (f) {};
if (typeof(f) !== 'function') {
throw new Error('[Store.change] callback must be a function');
}
changeHandlers.push(f);
if (changeHandlers.length === 1) { return ret;
// start listening for changes
/* TODO: listen for changes in the proxy
window.addEventListener('storage', function (e) {
changeHandlers.forEach(function (f) {
f({
key: e.key,
oldValue: e.oldValue,
newValue: e.newValue,
});
});
});
*/
}
}; };
var onReady = function (f, proxy, storageKey) { var onReady = function (f, proxy, storageKey, exp) {
filesOp = FO.init(proxy.drive, { var fo = FO.init(proxy.drive, {
storageKey: storageKey storageKey: storageKey
}); });
storeObj = proxy; //storeObj = proxy;
ready = true; store = initStore(fo, proxy, exp);
if (typeof(f) === 'function') { if (typeof(f) === 'function') {
f(void 0, Store); f(void 0, store);
} }
}; };
@ -167,6 +150,8 @@ define([
logLevel: 1, logLevel: 1,
}; };
var exp = {};
window.addEventListener('storage', function (e) { window.addEventListener('storage', function (e) {
var key = e.key; var key = e.key;
if (e.key !== Cryptpad.userHashKey) { return; } if (e.key !== Cryptpad.userHashKey) { return; }
@ -175,8 +160,6 @@ define([
if (!o && n) { if (!o && n) {
window.location.reload(); window.location.reload();
} else if (o && !n) { } else if (o && !n) {
//window.location.reload();
//window.location.href = '/';
$(window).on('keyup', function (e) { $(window).on('keyup', function (e) {
if (e.keyCode === 27) { if (e.keyCode === 27) {
Cryptpad.removeLoadingScreen(); Cryptpad.removeLoadingScreen();
@ -192,6 +175,7 @@ define([
}); });
var rt = window.rt = Listmap.create(listmapConfig); var rt = window.rt = Listmap.create(listmapConfig);
exp.proxy = rt.proxy; exp.proxy = rt.proxy;
rt.proxy.on('create', function (info) { rt.proxy.on('create', function (info) {
exp.info = info; exp.info = info;
@ -199,7 +183,7 @@ define([
localStorage.FS_hash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys); localStorage.FS_hash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys);
} }
}).on('ready', function () { }).on('ready', function () {
if (ready) { return; } if (store) { return; } // the store is already ready, it is a reconnection
if (!rt.proxy.drive || typeof(rt.proxy.drive) !== 'object') { rt.proxy.drive = {}; } if (!rt.proxy.drive || typeof(rt.proxy.drive) !== 'object') { rt.proxy.drive = {}; }
var drive = rt.proxy.drive; var drive = rt.proxy.drive;
// Creating a new anon drive: import anon pads from localStorage // Creating a new anon drive: import anon pads from localStorage
@ -207,36 +191,29 @@ define([
var oldStore = Cryptpad.getStore(true); var oldStore = Cryptpad.getStore(true);
Cryptpad.getRecentPads(function (err, s) { Cryptpad.getRecentPads(function (err, s) {
drive[Cryptpad.storageKey] = s; drive[Cryptpad.storageKey] = s;
onReady(f, rt.proxy, Cryptpad.storageKey); onReady(f, rt.proxy, Cryptpad.storageKey, exp);
}, true); }, true);
return; return;
} }
onReady(f, rt.proxy, Cryptpad.storageKey); // Return the existing drive
onReady(f, rt.proxy, Cryptpad.storageKey, exp);
}) })
.on('disconnect', function (info) { .on('disconnect', function (info) {
//setEditable(false); // We only manage errors during the loadin screen here. Other websocket errors are handled by the apps
if (info.error) { if (info.error) {
//Cryptpad.alert(Messages.websocketError);
if (typeof Cryptpad.storeError === "function") { if (typeof Cryptpad.storeError === "function") {
Cryptpad.storeError(); Cryptpad.storeError();
} }
return; return;
} }
//Cryptpad.alert(Messages.common_connectionLost);
}); });
}; };
Store.ready = function (f, Cryptpad) { Store.ready = function (f, Cryptpad) {
/*if (Cryptpad.parsePadUrl(window.location.href).type === "file") { if (store) { // Store.ready probably called twice, store already ready
if (typeof(f) === 'function') { if (typeof(f) === 'function') {
f(void 0, Cryptpad.getStore(true)); f(void 0, store);
}
return;
}*/
if (ready) {
if (typeof(f) === 'function') {
f(void 0, Store);
} }
} else { } else {
init(f, Cryptpad); init(f, Cryptpad);