From 1e2cc6bea47b128c6446a9f85f1f5a4714f524ac Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 7 Feb 2017 18:47:10 +0100 Subject: [PATCH] Fix race condition with the iframe store --- www/common/cryptpad-common.js | 90 +++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/www/common/cryptpad-common.js b/www/common/cryptpad-common.js index c48add08f..27b19b971 100644 --- a/www/common/cryptpad-common.js +++ b/www/common/cryptpad-common.js @@ -696,50 +696,58 @@ define([ f(void 0, env); }; - storeToUse.ready(function (err, store) { - common.store = env.store = store; - if (USE_FS_STORE) { - fsStore = store; - } - - $(function() { - // Race condition : if document.body is undefined when alertify.js is loaded, Alertify - // won't work. We have to reset it now to make sure it uses a correct "body" - Alertify.reset(); - - // Load the new pad when the hash has changed - var oldHash = document.location.hash.slice(1); - window.onhashchange = function () { - var newHash = document.location.hash.slice(1); - var parsedOld = parseHash(oldHash); - var parsedNew = parseHash(newHash); - if (parsedOld && parsedNew && ( - parsedOld.channel !== parsedNew.channel - || parsedOld.mode !== parsedNew.mode - || parsedOld.key !== parsedNew.key)) { - document.location.reload(); - return; - } - if (parsedNew) { - oldHash = newHash; - } - }; + var todo = function () { + storeToUse.ready(function (err, store) { + common.store = env.store = store; + if (USE_FS_STORE) { + fsStore = store; + } - // Everything's ready, continue... - if($('#pad-iframe').length) { - var $iframe = $('#pad-iframe'); - var iframe = $iframe[0]; - var iframeDoc = iframe.contentDocument || iframe.contentWindow.document; - if (iframeDoc.readyState === 'complete') { - cb(); + $(function() { + // Race condition : if document.body is undefined when alertify.js is loaded, Alertify + // won't work. We have to reset it now to make sure it uses a correct "body" + Alertify.reset(); + + // Load the new pad when the hash has changed + var oldHash = document.location.hash.slice(1); + window.onhashchange = function () { + var newHash = document.location.hash.slice(1); + var parsedOld = parseHash(oldHash); + var parsedNew = parseHash(newHash); + if (parsedOld && parsedNew && ( + parsedOld.channel !== parsedNew.channel + || parsedOld.mode !== parsedNew.mode + || parsedOld.key !== parsedNew.key)) { + document.location.reload(); + return; + } + if (parsedNew) { + oldHash = newHash; + } + }; + + // Everything's ready, continue... + if($('#pad-iframe').length) { + var $iframe = $('#pad-iframe'); + var iframe = $iframe[0]; + var iframeDoc = iframe.contentDocument || iframe.contentWindow.document; + if (iframeDoc.readyState === 'complete') { + cb(); + return; + } + $iframe.load(cb); return; } - $iframe.load(cb); - return; - } - cb(); - }); - }, common); + cb(); + }); + }, common); + }; + // If we use the fs store, make sure the localStorage or iframe store is ready + if (USE_FS_STORE) { + Store.ready(todo); + return; + } + todo(); }; var errorHandlers = [];