From ac4322cfab819bebeb44e137f6d66a3e4ed93242 Mon Sep 17 00:00:00 2001 From: yflory Date: Fri, 25 Sep 2020 17:24:24 +0200 Subject: [PATCH] Improve reconnect when in history or snapshot view --- www/common/sframe-app-framework.js | 24 ++++++++++++++++-------- www/common/sframe-common-history.js | 27 ++++++++++++++++++--------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/www/common/sframe-app-framework.js b/www/common/sframe-app-framework.js index 20973cb11..deb9563c0 100644 --- a/www/common/sframe-app-framework.js +++ b/www/common/sframe-app-framework.js @@ -291,7 +291,15 @@ define([ evEditableStateChange.fire(state === STATE.READY && !unsyncMode); stateChange(state); }; + + // History mode: + // When "bool" is true, we're entering in history mode + // When "bool" is false and "update" is true, it means we're closing the history + // and should update the content + // When "bool" is false and "update" is false, it means we're restoring an old version, + // no need to refresh var setHistoryMode = function (bool, update) { + if (!bool && !update && state !== STATE.READY) { return false; } cpNfInner.metadataMgr.setHistory(bool); toolbar.setHistory(bool); setUnsyncMode(bool); @@ -299,17 +307,19 @@ define([ else { setTimeout(cpNfInner.metadataMgr.refresh); } + return true; }; var closeSnapshot = function (restore) { - setUnsyncMode(false); - if (restore) { - onLocal(); - } - onRemote(); + if (restore && state !== STATE.READY) { return false; } + setUnsyncMode(false); // Unlock onLocal and onRemote + if (restore) { onLocal(); } // Restore? commit the content + onRemote(); // Make sure we're back to the realtime content + return true; }; var loadSnapshot = function (hash, data) { setUnsyncMode(true); Snapshots.create(common, { + readOnly: readOnly, $toolbar: $(toolbarContainer), hash: hash, data: data, @@ -436,9 +446,7 @@ define([ evOnDefaultContentNeeded.fire(); } }).nThen(function () { - if (!unsyncMode) { - stateChange(STATE.READY); - } + stateChange(STATE.READY); firstConnection = false; oldContent = undefined; diff --git a/www/common/sframe-common-history.js b/www/common/sframe-common-history.js index 76094f3ae..4c463c24c 100644 --- a/www/common/sframe-common-history.js +++ b/www/common/sframe-common-history.js @@ -2,11 +2,12 @@ define([ 'jquery', '/common/common-interface.js', '/common/hyperscript.js', + '/customize/messages.js', '/bower_components/nthen/index.js', //'/bower_components/chainpad-json-validator/json-ot.js', '/bower_components/chainpad/chainpad.dist.js', -], function ($, UI, h, nThen, ChainPad /* JsonOT */) { +], function ($, UI, h, Messages, nThen, ChainPad /* JsonOT */) { //var ChainPad = window.ChainPad; var History = {}; @@ -93,17 +94,23 @@ define([ console.error(e); } }; - var onClose = function () { config.setHistory(false, true); }; + var onClose = function () { + config.setHistory(false, true); + }; + + Messages.history_cantRestore = "Can't restore now. Disconnected."; // XXX var onRevert = function () { - config.setHistory(false, false); + var closed = config.setHistory(false, false); + if (!closed) { + return void UI.alert(Messages.history_cantRestore); + } config.onLocal(); config.onRemote(); + return true; }; config.setHistory(true); - var Messages = common.Messages; - var realtime; var states = []; @@ -372,15 +379,17 @@ define([ // Close & restore buttons $close.click(function () { states = []; - close(); onClose(); + close(); }); $rev.click(function () { UI.confirm(Messages.history_restorePrompt, function (yes) { if (!yes) { return; } - close(); - onRevert(); - UI.log(Messages.history_restoreDone); + var done = onRevert(); + if (done) { + close(); + UI.log(Messages.history_restoreDone); + } }); });