Display pad content from cache before joining the network/channel

pull/1/head
yflory 4 years ago
parent d16cc2472d
commit 4126ae5d8b

@ -992,6 +992,8 @@ define([
pad.onJoinEvent = Util.mkEvent(); pad.onJoinEvent = Util.mkEvent();
pad.onLeaveEvent = Util.mkEvent(); pad.onLeaveEvent = Util.mkEvent();
pad.onDisconnectEvent = Util.mkEvent(); pad.onDisconnectEvent = Util.mkEvent();
pad.onCacheEvent = Util.mkEvent();
pad.onCacheReadyEvent = Util.mkEvent();
pad.onConnectEvent = Util.mkEvent(); pad.onConnectEvent = Util.mkEvent();
pad.onErrorEvent = Util.mkEvent(); pad.onErrorEvent = Util.mkEvent();
pad.onMetadataEvent = Util.mkEvent(); pad.onMetadataEvent = Util.mkEvent();
@ -1957,6 +1959,8 @@ define([
PAD_JOIN: common.padRpc.onJoinEvent.fire, PAD_JOIN: common.padRpc.onJoinEvent.fire,
PAD_LEAVE: common.padRpc.onLeaveEvent.fire, PAD_LEAVE: common.padRpc.onLeaveEvent.fire,
PAD_DISCONNECT: common.padRpc.onDisconnectEvent.fire, PAD_DISCONNECT: common.padRpc.onDisconnectEvent.fire,
PAD_CACHE: common.padRpc.onCacheEvent.fire,
PAD_CACHE_READY: common.padRpc.onCacheReadyEvent.fire,
PAD_CONNECT: common.padRpc.onConnectEvent.fire, PAD_CONNECT: common.padRpc.onConnectEvent.fire,
PAD_ERROR: common.padRpc.onErrorEvent.fire, PAD_ERROR: common.padRpc.onErrorEvent.fire,
PAD_METADATA: common.padRpc.onMetadataEvent.fire, PAD_METADATA: common.padRpc.onMetadataEvent.fire,

@ -1592,6 +1592,12 @@ define([
}; };
var conf = { var conf = {
Cache: Cache, Cache: Cache,
onCacheStart: function () {
postMessage(clientId, "PAD_CACHE");
},
onCacheReady: function (info) {
postMessage(clientId, "PAD_CACHE_READY");
},
onReady: function (pad) { onReady: function (pad) {
var padData = pad.metadata || {}; var padData = pad.metadata || {};
channel.data = padData; channel.data = padData;

@ -467,7 +467,44 @@ define([
}); });
}; };
var onCacheReady = function () {
stateChange(STATE.DISCONNECTED);
toolbar.offline(true);
var newContentStr = cpNfInner.chainpad.getUserDoc();
if (toolbar) {
// Check if we have a new chainpad instance
toolbar.resetChainpad(cpNfInner.chainpad);
}
console.log(newContentStr);
// Invalid cache? abort
// XXX tell outer/worker to invalidate cache
if (newContentStr === '') { return; }
var privateDat = cpNfInner.metadataMgr.getPrivateData();
var type = privateDat.app;
var newContent = JSON.parse(newContentStr);
var metadata = extractMetadata(newContent);
console.log('OKOK');
// Make sure we're using the correct app for this cache
if (metadata && typeof(metadata.type) !== 'undefined' && metadata.type !== type) {
console.error('return');
return;
}
cpNfInner.metadataMgr.updateMetadata(metadata);
newContent = normalize(newContent);
if (!unsyncMode) {
contentUpdate(newContent, function () { return function () {}});
}
UI.removeLoadingScreen(emitResize);
};
var onReady = function () { var onReady = function () {
toolbar.offline(false);
console.error('READY');
var newContentStr = cpNfInner.chainpad.getUserDoc(); var newContentStr = cpNfInner.chainpad.getUserDoc();
if (state === STATE.DELETED) { return; } if (state === STATE.DELETED) { return; }
@ -732,6 +769,7 @@ define([
onRemote: onRemote, onRemote: onRemote,
onLocal: onLocal, onLocal: onLocal,
onInit: onInit, onInit: onInit,
onCacheReady: onCacheReady,
onReady: function () { evStart.reg(onReady); }, onReady: function () { evStart.reg(onReady); },
onConnectionChange: onConnectionChange, onConnectionChange: onConnectionChange,
onError: onError, onError: onError,

@ -34,6 +34,7 @@ define([
var onLocal = config.onLocal || function () { }; var onLocal = config.onLocal || function () { };
var setMyID = config.setMyID || function () { }; var setMyID = config.setMyID || function () { };
var onReady = config.onReady || function () { }; var onReady = config.onReady || function () { };
var onCacheReady = config.onCacheReady || function () { };
var onError = config.onError || function () { }; var onError = config.onError || function () { };
var userName = config.userName; var userName = config.userName;
var initialState = config.initialState; var initialState = config.initialState;
@ -93,6 +94,14 @@ define([
evInfiniteSpinner.fire(); evInfiniteSpinner.fire();
}, 2000); }, 2000);
sframeChan.on('EV_RT_CACHE', function (isPermanent) {
// XXX
});
sframeChan.on('EV_RT_CACHE_READY', function (isPermanent) {
// XXX
onCacheReady({realtime: chainpad});
console.error('PEWPEWPEW');
});
sframeChan.on('EV_RT_DISCONNECT', function (isPermanent) { sframeChan.on('EV_RT_DISCONNECT', function (isPermanent) {
isReady = false; isReady = false;
chainpad.abort(); chainpad.abort();
@ -134,6 +143,7 @@ define([
evConnected.fire(); evConnected.fire();
}); });
sframeChan.on('Q_RT_MESSAGE', function (content, cb) { sframeChan.on('Q_RT_MESSAGE', function (content, cb) {
console.log(content);
if (isReady) { if (isReady) {
onLocal(true); // should be onBeforeMessage onLocal(true); // should be onBeforeMessage
} }

@ -89,6 +89,7 @@ define([], function () {
validateKey = msgObj.validateKey; validateKey = msgObj.validateKey;
} }
var message = msgIn(msgObj.user, msgObj.msg); var message = msgIn(msgObj.user, msgObj.msg);
console.log(message);
if (!message) { return; } if (!message) { return; }
lastTime = msgObj.time; lastTime = msgObj.time;
@ -114,16 +115,26 @@ define([], function () {
if (firstConnection) { if (firstConnection) {
firstConnection = false; firstConnection = false;
// Add the handlers to the WebChannel // Add the handlers to the WebChannel
padRpc.onMessageEvent.reg(function (msg) { onMessage(msg); });
padRpc.onJoinEvent.reg(function (m) { sframeChan.event('EV_RT_JOIN', m); }); padRpc.onJoinEvent.reg(function (m) { sframeChan.event('EV_RT_JOIN', m); });
padRpc.onLeaveEvent.reg(function (m) { sframeChan.event('EV_RT_LEAVE', m); }); padRpc.onLeaveEvent.reg(function (m) { sframeChan.event('EV_RT_LEAVE', m); });
} }
}; };
padRpc.onMessageEvent.reg(function (msg) { onMessage(msg); });
padRpc.onDisconnectEvent.reg(function (permanent) { padRpc.onDisconnectEvent.reg(function (permanent) {
sframeChan.event('EV_RT_DISCONNECT', permanent); sframeChan.event('EV_RT_DISCONNECT', permanent);
}); });
padRpc.onCacheReadyEvent.reg(function () {
console.log('ONCACHEREADY');
sframeChan.event('EV_RT_CACHE_READY');
});
padRpc.onCacheEvent.reg(function () {
sframeChan.event('EV_RT_CACHE');
});
padRpc.onConnectEvent.reg(function (data) { padRpc.onConnectEvent.reg(function (data) {
onOpen(data); onOpen(data);
}); });

@ -1365,6 +1365,18 @@ MessengerUI, Messages) {
} }
}; };
toolbar.offline = function (bool) {
toolbar.connected = !bool; // Can't edit title
toolbar.history = bool; // Stop "Initializing" state
toolbar.isErrorState = bool; // Stop kickSpinner
toolbar.title.toggleClass('cp-toolbar-unsync', bool); // "read only" next to the title
if (bool && toolbar.spinner) {
toolbar.spinner.text("OFFLINE"); // XXX
} else {
kickSpinner(toolbar, config);
}
};
// On log out, remove permanently the realtime elements of the toolbar // On log out, remove permanently the realtime elements of the toolbar
Common.onLogout(function () { Common.onLogout(function () {
failed(); failed();

Loading…
Cancel
Save