From 663e98734560a8d28008f92c7f02e485a2e13dd4 Mon Sep 17 00:00:00 2001 From: ansuz Date: Sat, 16 Jan 2016 07:37:00 -0500 Subject: [PATCH] fix race condition in codemirror initialization over slow connections, iframes don't load fast enough to be ready for code which depends on certain features existing. wait until they're ready, then initialize. --- www/code/main.js | 72 ++++++++++++++++++++++++++++++------------------ 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/www/code/main.js b/www/code/main.js index 7a46ce4e3..fd9de0108 100644 --- a/www/code/main.js +++ b/www/code/main.js @@ -8,7 +8,7 @@ define([ '/customize/pad.js' var $ = window.jQuery; var ifrw = $('#pad-iframe')[0].contentWindow; - var CMeditor = ifrw.CodeMirror; + var CMeditor; $(function () { $(window).on('hashchange', function() { @@ -18,32 +18,50 @@ define([ window.location.href = window.location.href + '#' + Crypto.genKey(); return; } - var key = Crypto.parseKey(window.location.hash.substring(1)); - var editor = CMeditor.fromTextArea($('#pad-iframe').contents().find('#editor1')[0], { - lineNumbers: true, - lineWrapping: true, - autoCloseBrackets: true, - matchBrackets : true, - showTrailingSpace : true, - styleActiveLine : true, - search: true, - highlightSelectionMatches: {showToken: /\w+/}, - extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }}, - foldGutter: true, - gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"], - mode: "javascript" - }); - editor.setValue(Messages.codeInitialState); - var rtw = - RTWiki.start(ifrw, - Config.websocketURL, - Crypto.rand64(8), - key.channel, - key.cryptKey); - editor.on('change', function() { - editor.save(); - rtw.onEvent(); - }); + var andThen = function () { + var key = Crypto.parseKey(window.location.hash.substring(1)); + var editor = CMeditor.fromTextArea($('#pad-iframe').contents().find('#editor1')[0], { + lineNumbers: true, + lineWrapping: true, + autoCloseBrackets: true, + matchBrackets : true, + showTrailingSpace : true, + styleActiveLine : true, + search: true, + highlightSelectionMatches: {showToken: /\w+/}, + extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }}, + foldGutter: true, + gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"], + mode: "javascript" + }); + editor.setValue(Messages.codeInitialState); + + var rtw = + RTWiki.start(ifrw, + Config.websocketURL, + Crypto.rand64(8), + key.channel, + key.cryptKey); + editor.on('change', function() { + editor.save(); + rtw.onEvent(); + }); + }; + + var interval = 100; + + var first = function () { + if (CMeditor = ifrw.CodeMirror) { + // it exists, call your continuation + andThen(); + } else { + console.log("CMeditor was not defined. Trying again in %sms", interval); + // try again in 'interval' ms + setTimeout(first, interval); + } + }; + + first(); }); });