Check if the worker is available when focusing a tab again

pull/1/head
yflory 5 years ago
parent 21c3abde6f
commit 718cdee907

@ -6,6 +6,7 @@ define([
'/common/common-messaging.js',
'/common/common-constants.js',
'/common/common-feedback.js',
'/common/visible.js',
'/common/userObject.js',
'/common/outer/local-store.js',
'/common/outer/worker-channel.js',
@ -14,7 +15,7 @@ define([
'/customize/application_config.js',
'/bower_components/nthen/index.js',
], function (Config, Messages, Util, Hash,
Messaging, Constants, Feedback, UserObject, LocalStore, Channel, Block,
Messaging, Constants, Feedback, Visible, UserObject, LocalStore, Channel, Block,
AppConfig, Nthen) {
/* This file exposes functionality which is specific to Cryptpad, but not to
@ -1749,19 +1750,37 @@ define([
cb();
};
var lastPing = +new Date();
var onPing = function (data, cb) {
lastPing = +new Date();
cb();
};
var timeout = false;
common.onTimeoutEvent = Util.mkEvent();
var onTimeout = function () {
var onTimeout = function (fromOuter) {
var key = fromOuter ? "TIMEOUT_OUTER" : "TIMEOUT_KICK";
Feedback.send(key, true);
timeout = true;
common.onNetworkDisconnect.fire();
common.padRpc.onDisconnectEvent.fire();
common.onTimeoutEvent.fire();
};
Visible.onChange(function (visible) {
if (!visible) { return; }
var now = +new Date();
// If last ping is bigger than 2min, ping the worker
if (now - lastPing > (2 * 60 * 1000)) {
var to = setTimeout(function () {
onTimeout(true);
});
postMessage('PING', null, function () {
clearTimeout(to);
});
}
});
var queries = {
PING: onPing,
TIMEOUT: onTimeout,

@ -13,6 +13,7 @@ define([
DISCONNECT: Store.disconnect,
CREATE_README: Store.createReadme,
MIGRATE_ANON_DRIVE: Store.migrateAnonDrive,
PING: function (cId, data, cb) { cb(); },
// RPC
UPDATE_PIN_LIMIT: Store.updatePinLimit,
GET_PIN_LIMIT: Store.getPinLimit,

@ -79,7 +79,10 @@ define([
var metaObj;
nThen(function (waitFor) {
Cryptpad.getMetadata(waitFor(function (err, n) {
if (err) { console.log(err); }
if (err) {
waitFor.abort();
return void console.log(err);
}
metaObj = n;
}));
}).nThen(function (/*waitFor*/) {

Loading…
Cancel
Save