First prototype of an offline cache

This commit is contained in:
yflory 2020-11-03 10:49:13 +01:00
parent b1f29678ca
commit d16cc2472d
5 changed files with 77 additions and 5 deletions

View File

@ -419,9 +419,11 @@ const getHistoryOffset = (Env, channelName, lastKnownHash, _cb) => {
// fall through to the next block if the offset of the hash in question is not in memory
if (lastKnownHash && typeof(lkh) !== "number") { return; }
// If we have a lastKnownHash or we didn't ask for one, we don't need the next blocks
waitFor.abort();
// Since last 2 checkpoints
if (!lastKnownHash) {
waitFor.abort();
// Less than 2 checkpoints in the history: return everything
if (index.cpIndex.length < 2) { return void cb(null, 0); }
// Otherwise return the second last checkpoint's index
@ -436,7 +438,14 @@ const getHistoryOffset = (Env, channelName, lastKnownHash, _cb) => {
to reconcile their differences. */
}
offset = lkh;
// If our lastKnownHash is older than the 2nd to last checkpoint,
// only send the last 2 checkpoints and ignore "lkh"
if (lkh && index.cpIndex.length >= 2 && lkh < index.cpIndex[0].offset) {
return void cb(null, index.cpIndex[0].offset);
}
// Otherwise use our lastKnownHash
cb(null, lkh);
}));
}).nThen((w) => {
// skip past this block if the offset is anything other than -1

View File

@ -10,6 +10,7 @@ define([
'/common/common-realtime.js',
'/common/common-messaging.js',
'/common/pinpad.js',
'/common/outer/cache-store.js',
'/common/outer/sharedfolder.js',
'/common/outer/cursor.js',
'/common/outer/onlyoffice.js',
@ -28,7 +29,7 @@ define([
'/bower_components/nthen/index.js',
'/bower_components/saferphore/index.js',
], function (Sortify, UserObject, ProxyManager, Migrate, Hash, Util, Constants, Feedback,
Realtime, Messaging, Pinpad,
Realtime, Messaging, Pinpad, Cache,
SF, Cursor, OnlyOffice, Mailbox, Profile, Team, Messenger, History,
NetConfig, AppConfig,
Crypto, ChainPad, CpNetflux, Listmap, nThen, Saferphore) {
@ -1590,6 +1591,7 @@ define([
Store.leavePad(null, data, function () {});
};
var conf = {
Cache: Cache,
onReady: function (pad) {
var padData = pad.metadata || {};
channel.data = padData;
@ -2640,6 +2642,7 @@ define([
readOnly: false,
validateKey: secret.keys.validateKey || undefined,
crypto: Crypto.createEncryptor(secret.keys),
Cache: Cache,
userName: 'fs',
logLevel: 1,
ChainPad: ChainPad,

View File

@ -0,0 +1,55 @@
define([
'/bower_components/localforage/dist/localforage.min.js',
], function (localForage) {
var S = {};
var cache = localForage.createInstance({
name: "cp_cache"
});
// id: channel ID or blob ID
// returns array of messages
S.getChannelCache = function (id, cb) {
cache.getItem(id, function (err, obj) {
if (err || !obj || !Array.isArray(obj.c)) {
return void cb(err || 'EINVAL');
}
cb(null, obj);
});
};
var checkCheckpoints = function (array) {
if (!Array.isArray(array)) { return; }
var cp = 0;
for (var i = array.length - 1; i >= 0; i--) {
if (array[i].isCheckpoint) { cp++; }
if (cp === 2) {
array.splice(0, i);
break;
}
}
};
S.storeCache = function (id, validateKey, val, cb) {
cb = cb || function () {};
if (!Array.isArray(val) || !validateKey) { return void cb('EINVAL'); }
checkCheckpoints(val);
cache.setItem(id, {
k: validateKey,
c: val
}, function (err) {
cb(err);
});
};
S.clearChannel = function (id, cb) {
cb = cb || function () {};
cache.removeItem(id, cb);
};
S.clear = function () {
cache.clear();
};
return S;
});

View File

@ -2,12 +2,13 @@ define([
'/common/common-hash.js',
'/common/common-util.js',
'/common/userObject.js',
'/common/outer/cache-store.js',
'/bower_components/nthen/index.js',
'/bower_components/chainpad-crypto/crypto.js',
'/bower_components/chainpad-listmap/chainpad-listmap.js',
'/bower_components/chainpad/chainpad.dist.js',
], function (Hash, Util, UserObject,
], function (Hash, Util, UserObject, Cache,
nThen, Crypto, Listmap, ChainPad) {
var SF = {};
@ -174,6 +175,7 @@ define([
ChainPad: ChainPad,
classic: true,
network: network,
Cache: Cache,
metadata: {
validateKey: secret.keys.validateKey || undefined,
owners: owners

View File

@ -12,6 +12,7 @@ define([
'/common/common-feedback.js',
'/common/outer/invitation.js',
'/common/cryptget.js',
'/common/outer/cache-store.js',
'/bower_components/chainpad-listmap/chainpad-listmap.js',
'/bower_components/chainpad-crypto/crypto.js',
@ -21,7 +22,7 @@ define([
'/bower_components/saferphore/index.js',
'/bower_components/tweetnacl/nacl-fast.min.js',
], function (Util, Hash, Constants, Realtime,
ProxyManager, UserObject, SF, Roster, Messaging, Feedback, Invite, Crypt,
ProxyManager, UserObject, SF, Roster, Messaging, Feedback, Invite, Crypt, Cache,
Listmap, Crypto, CpNetflux, ChainPad, nThen, Saferphore) {
var Team = {};
@ -426,6 +427,7 @@ define([
channel: secret.channel,
crypto: crypto,
ChainPad: ChainPad,
Cache: Cache,
metadata: {
validateKey: secret.keys.validateKey || undefined,
},
@ -573,6 +575,7 @@ define([
logLevel: 1,
classic: true,
ChainPad: ChainPad,
Cache: Cache,
owners: [ctx.store.proxy.edPublic]
};
nThen(function (waitFor) {