Do not expose the store methods until it is ready

pull/1/head
yflory 8 years ago
parent 67d881b2cf
commit fc3995054d

@ -16,133 +16,116 @@ define([
*/
var Store = {};
var storeObj;
var ready = false;
var filesOp;
var exp = {};
var store;
var safeSet = function (key, val) {
storeObj[key] = val;
};
var initStore = function (filesOp, storeObj, exp) {
var ret = {};
// Store uses nodebacks...
Store.set = function (key, val, cb) {
safeSet(key, val);
cb();
};
var safeSet = function (key, val) {
storeObj[key] = val;
};
// implement in alternative store
Store.setBatch = function (map, cb) {
Object.keys(map).forEach(function (key) {
safeSet(key, map[key]);
});
cb(void 0, map);
};
// Store uses nodebacks...
ret.set = function (key, val, cb) {
safeSet(key, val);
cb();
};
Store.setDrive = function (key, val, cb) {
storeObj.drive[key] = val;
cb();
};
// implement in alternative store
ret.setBatch = function (map, cb) {
Object.keys(map).forEach(function (key) {
safeSet(key, map[key]);
});
cb(void 0, map);
};
var safeGet = window.safeGet = function (key) {
return storeObj[key];
};
ret.setDrive = function (key, val, cb) {
storeObj.drive[key] = val;
cb();
};
Store.get = function (key, cb) {
cb(void 0, safeGet(key));
};
var safeGet = function (key) {
return storeObj[key];
};
// implement in alternative store
Store.getBatch = function (keys, cb) {
var res = {};
keys.forEach(function (key) {
res[key] = safeGet(key);
});
cb(void 0, res);
};
ret.get = function (key, cb) {
cb(void 0, safeGet(key));
};
Store.getDrive = function (key, cb) {
cb(void 0, storeObj.drive[key]);
};
// implement in alternative store
ret.getBatch = function (keys, cb) {
var res = {};
keys.forEach(function (key) {
res[key] = safeGet(key);
});
cb(void 0, res);
};
var safeRemove = function (key) {
delete storeObj[key];
};
ret.getDrive = function (key, cb) {
cb(void 0, storeObj.drive[key]);
};
Store.remove = function (key, cb) {
safeRemove(key);
cb();
};
var safeRemove = function (key) {
delete storeObj[key];
};
// implement in alternative store
Store.removeBatch = function (keys, cb) {
keys.forEach(function (key) {
ret.remove = function (key, cb) {
safeRemove(key);
});
cb();
};
cb();
};
Store.keys = function (cb) {
cb(void 0, Object.keys(storeObj));
};
// implement in alternative store
ret.removeBatch = function (keys, cb) {
keys.forEach(function (key) {
safeRemove(key);
});
cb();
};
Store.addPad = function (href, path, name) {
filesOp.addPad(href, path, name);
};
ret.keys = function (cb) {
cb(void 0, Object.keys(storeObj));
};
Store.forgetPad = function (href, cb) {
filesOp.forgetPad(href);
cb();
};
ret.addPad = function (href, path, name) {
filesOp.addPad(href, path, name);
};
Store.addTemplate = function (href) {
filesOp.addTemplate(href);
};
ret.forgetPad = function (href, cb) {
filesOp.forgetPad(href);
cb();
};
Store.listTemplates = function () {
return filesOp.listTemplates();
};
ret.addTemplate = function (href) {
filesOp.addTemplate(href);
};
Store.getProxy = function () {
return exp;
};
ret.listTemplates = function () {
return filesOp.listTemplates();
};
Store.getLoginName = function () {
return storeObj.login_name;
};
ret.getProxy = function () {
return exp;
};
var changeHandlers = Store.changeHandlers = [];
ret.getLoginName = function () {
return storeObj.login_name;
};
Store.change = function (f) {
if (typeof(f) !== 'function') {
throw new Error('[Store.change] callback must be a function');
}
changeHandlers.push(f);
if (changeHandlers.length === 1) {
// 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 changeHandlers = ret.changeHandlers = [];
ret.change = function (f) {};
return ret;
};
var onReady = function (f, proxy, storageKey) {
filesOp = FO.init(proxy.drive, {
var onReady = function (f, proxy, storageKey, exp) {
var fo = FO.init(proxy.drive, {
storageKey: storageKey
});
storeObj = proxy;
ready = true;
//storeObj = proxy;
store = initStore(fo, proxy, exp);
if (typeof(f) === 'function') {
f(void 0, Store);
f(void 0, store);
}
};
@ -167,6 +150,8 @@ define([
logLevel: 1,
};
var exp = {};
window.addEventListener('storage', function (e) {
var key = e.key;
if (e.key !== Cryptpad.userHashKey) { return; }
@ -175,8 +160,6 @@ define([
if (!o && n) {
window.location.reload();
} else if (o && !n) {
//window.location.reload();
//window.location.href = '/';
$(window).on('keyup', function (e) {
if (e.keyCode === 27) {
Cryptpad.removeLoadingScreen();
@ -192,6 +175,7 @@ define([
});
var rt = window.rt = Listmap.create(listmapConfig);
exp.proxy = rt.proxy;
rt.proxy.on('create', function (info) {
exp.info = info;
@ -199,7 +183,7 @@ define([
localStorage.FS_hash = Cryptpad.getEditHashFromKeys(info.channel, secret.keys);
}
}).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 = {}; }
var drive = rt.proxy.drive;
// Creating a new anon drive: import anon pads from localStorage
@ -207,36 +191,29 @@ define([
var oldStore = Cryptpad.getStore(true);
Cryptpad.getRecentPads(function (err, s) {
drive[Cryptpad.storageKey] = s;
onReady(f, rt.proxy, Cryptpad.storageKey);
onReady(f, rt.proxy, Cryptpad.storageKey, exp);
}, true);
return;
}
onReady(f, rt.proxy, Cryptpad.storageKey);
// Return the existing drive
onReady(f, rt.proxy, Cryptpad.storageKey, exp);
})
.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) {
//Cryptpad.alert(Messages.websocketError);
if (typeof Cryptpad.storeError === "function") {
Cryptpad.storeError();
}
return;
}
//Cryptpad.alert(Messages.common_connectionLost);
});
};
Store.ready = function (f, Cryptpad) {
/*if (Cryptpad.parsePadUrl(window.location.href).type === "file") {
if (typeof(f) === 'function') {
f(void 0, Cryptpad.getStore(true));
}
return;
}*/
if (ready) {
if (store) { // Store.ready probably called twice, store already ready
if (typeof(f) === 'function') {
f(void 0, Store);
f(void 0, store);
}
} else {
init(f, Cryptpad);

Loading…
Cancel
Save