refactor cryptpad.ready to use nthen

pull/1/head
ansuz 8 years ago
parent 4d688289cc
commit 513f8e73d4

@ -20,9 +20,10 @@ define([
'/common/pinpad.js', '/common/pinpad.js',
'/customize/application_config.js', '/customize/application_config.js',
'/common/media-tag.js', '/common/media-tag.js',
'/bower_components/nthen/index.js',
], function ($, Config, Messages, Store, Util, Hash, UI, History, UserList, Title, Metadata, ], function ($, Config, Messages, Store, Util, Hash, UI, History, UserList, Title, Metadata,
Messaging, CodeMirror, Files, FileCrypto, Realtime, Clipboard, Messaging, CodeMirror, Files, FileCrypto, Realtime, Clipboard,
Pinpad, AppConfig, MediaTag) { Pinpad, AppConfig, MediaTag, Nthen) {
// Configure MediaTags to use our local viewer // Configure MediaTags to use our local viewer
if (MediaTag && MediaTag.PdfPlugin) { if (MediaTag && MediaTag.PdfPlugin) {
@ -2071,54 +2072,12 @@ define([
return function (f) { return function (f) {
if (initialized) { if (initialized) {
return void window.setTimeout(function () { return void setTimeout(function () { f(void 0, env); });
f(void 0, env);
});
}
var block = 0;
var cb = function () {
block--;
if (!block) {
initialized = true;
updateLocalVersion();
common.addTooltips();
f(void 0, env);
if (typeof(window.onhashchange) === 'function') { window.onhashchange(); }
}
};
if (sessionStorage[newPadNameKey]) {
common.initialName = sessionStorage[newPadNameKey];
delete sessionStorage[newPadNameKey];
}
if (sessionStorage[newPadPathKey]) {
common.initialPath = sessionStorage[newPadPathKey];
delete sessionStorage[newPadPathKey];
} }
common.onFriendRequest = function (confirmText, cb) { var proxy;
common.confirm(confirmText, cb, null, true); var network;
}; var provideFeedback = function () {
common.onFriendComplete = function (data) {
common.log(data.logText);
};
Store.ready(function (err, storeObj) {
store = common.store = env.store = storeObj;
common.addDirectMessageHandler(common);
var proxy = getProxy();
var network = getNetwork();
network.on('disconnect', function () {
Realtime.setConnectionState(false);
});
network.on('reconnect', function () {
Realtime.setConnectionState(true);
});
if (Object.keys(proxy).length === 1) { if (Object.keys(proxy).length === 1) {
feedback("FIRST_APP_USE", true); feedback("FIRST_APP_USE", true);
} }
@ -2141,110 +2100,138 @@ define([
} }
common.reportScreenDimensions(); common.reportScreenDimensions();
common.reportLanguage(); common.reportLanguage();
};
$(function() { Nthen(function (waitFor) {
// Race condition : if document.body is undefined when alertify.js is loaded, Alertify if (sessionStorage[newPadNameKey]) {
// won't work. We have to reset it now to make sure it uses a correct "body" common.initialName = sessionStorage[newPadNameKey];
UI.Alertify.reset(); delete sessionStorage[newPadNameKey];
// clear any tooltips that might get hung }
setInterval(function () { common.clearTooltips(); }, 5000); if (sessionStorage[newPadPathKey]) {
common.initialPath = sessionStorage[newPadPathKey];
// Load the new pad when the hash has changed delete sessionStorage[newPadPathKey];
var oldHref = document.location.href; }
window.onhashchange = function () { common.onFriendRequest = function (confirmText, cb) {
var newHref = document.location.href; common.confirm(confirmText, cb, null, true);
var parsedOld = parsePadUrl(oldHref).hashData; };
var parsedNew = parsePadUrl(newHref).hashData; common.onFriendComplete = function (data) {
if (parsedOld && parsedNew && ( common.log(data.logText);
parsedOld.type !== parsedNew.type };
|| parsedOld.channel !== parsedNew.channel }).nThen(function (waitFor) {
|| parsedOld.mode !== parsedNew.mode Store.ready(waitFor(function (err, storeObj) {
|| parsedOld.key !== parsedNew.key)) { store = common.store = env.store = storeObj;
if (!parsedOld.channel) { oldHref = newHref; return; } common.addDirectMessageHandler(common);
document.location.reload(); proxy = getProxy();
return; network = getNetwork();
} network.on('disconnect', function () {
if (parsedNew) { Realtime.setConnectionState(false);
oldHref = newHref; });
} network.on('reconnect', function () {
}; Realtime.setConnectionState(true);
});
if (PINNING_ENABLED && isLoggedIn()) { provideFeedback();
console.log("logged in. pads will be pinned"); }), common);
block++; }).nThen(function (waitFor) {
$(waitFor());
Pinpad.create(network, proxy, function (e, call) { }).nThen(function (waitFor) {
if (e) { // Race condition : if document.body is undefined when alertify.js is loaded, Alertify
console.error(e); // won't work. We have to reset it now to make sure it uses a correct "body"
return cb(); UI.Alertify.reset();
} // clear any tooltips that might get hung
setInterval(function () { common.clearTooltips(); }, 5000);
// Load the new pad when the hash has changed
var oldHref = document.location.href;
window.onhashchange = function () {
var newHref = document.location.href;
var parsedOld = parsePadUrl(oldHref).hashData;
var parsedNew = parsePadUrl(newHref).hashData;
if (parsedOld && parsedNew && (
parsedOld.type !== parsedNew.type
|| parsedOld.channel !== parsedNew.channel
|| parsedOld.mode !== parsedNew.mode
|| parsedOld.key !== parsedNew.key)) {
if (!parsedOld.channel) { oldHref = newHref; return; }
document.location.reload();
return;
}
if (parsedNew) { oldHref = newHref; }
};
console.log('RPC handshake complete'); if (PINNING_ENABLED && isLoggedIn()) {
rpc = common.rpc = env.rpc = call; console.log("logged in. pads will be pinned");
var w0 = waitFor();
Pinpad.create(network, proxy, function (e, call) {
if (e) {
console.error(e);
return w0();
}
common.getPinLimit(function (e, limit, plan, note) { console.log('RPC handshake complete');
if (e) { return void console.error(e); } rpc = common.rpc = env.rpc = call;
common.account.limit = limit;
localStorage.plan = common.account.plan = plan;
common.account.note = note;
cb();
});
common.arePinsSynced(function (err, yes) { common.getPinLimit(function (e, limit, plan, note) {
if (!yes) { if (e) { return void console.error(e); }
common.resetPins(function (err) { common.account.limit = limit;
if (err) { localStorage.plan = common.account.plan = plan;
console.error("Pin Reset Error"); common.account.note = note;
return console.error(err); w0();
}
console.log('RESET DONE');
});
}
});
}); });
} else if (PINNING_ENABLED) {
console.log('not logged in. pads will not be pinned');
} else {
console.log('pinning disabled');
}
block++; common.arePinsSynced(function (err, yes) {
require([ if (!yes) {
'/common/rpc.js', common.resetPins(function (err) {
], function (Rpc) { if (err) {
Rpc.createAnonymous(network, function (e, call) { console.error("Pin Reset Error");
if (e) { return console.error(err);
console.error(e); }
return void cb(); console.log('RESET DONE');
});
} }
anon_rpc = common.anon_rpc = env.anon_rpc = call;
cb();
}); });
}); });
} else if (PINNING_ENABLED) {
console.log('not logged in. pads will not be pinned');
} else {
console.log('pinning disabled');
}
var w1 = waitFor();
// Everything's ready, continue... require([
if($('#pad-iframe').length) { '/common/rpc.js',
block++; ], function (Rpc) {
var $iframe = $('#pad-iframe'); Rpc.createAnonymous(network, function (e, call) {
var iframe = $iframe[0]; if (e) {
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document; console.error(e);
if (iframeDoc.readyState === 'complete') { return void w1();
cb();
return;
} }
$iframe.load(cb); anon_rpc = common.anon_rpc = env.anon_rpc = call;
return; w1();
} });
block++;
cb();
}); });
}, common);
// Everything's ready, continue...
if($('#pad-iframe').length) {
var w2 = waitFor();
var $iframe = $('#pad-iframe');
var iframe = $iframe[0];
var iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
if (iframeDoc.readyState === 'complete') {
return void w2();
}
$iframe.load(w2); //cb);
}
}).nThen(function () {
updateLocalVersion();
common.addTooltips();
f(void 0, env);
if (typeof(window.onhashchange) === 'function') { window.onhashchange(); }
});
}; };
}()); }());
// MAGIC that happens implicitly
$(function () { $(function () {
Messages._applyTranslation(); Messages._applyTranslation();
}); });

Loading…
Cancel
Save