diff --git a/www/common/realtime-input.js b/www/common/realtime-input.js index ed665e9e3..ed982580e 100644 --- a/www/common/realtime-input.js +++ b/www/common/realtime-input.js @@ -18,11 +18,10 @@ define([ '/common/messages.js', '/common/netflux-client.js', '/common/crypto.js', - '/common/TextPatcher.js', '/common/es6-promise.min.js', '/common/chainpad.js', '/bower_components/jquery/dist/jquery.min.js', -], function (Messages, Netflux, Crypto, TextPatcher) { +], function (Messages, Netflux, Crypto) { var $ = window.jQuery; var ChainPad = window.ChainPad; var PARANOIA = true; @@ -117,6 +116,7 @@ define([ initializing = false; // execute an onReady callback if one was supplied + // FIXME this should be once the chain has synced if (config.onReady) { config.onReady({ realtime: realtime @@ -227,13 +227,6 @@ define([ // Open a Chainpad session realtime = createRealtime(); - toReturn.onEvent = function (newText) { - // assert to show that we're not out of sync - if (realtime.getUserDoc() !== newText) { - warn("realtime.getUserDoc() !== newText"); - } - }; - // Sending a message... realtime.onMessage(function(message) { // Filter messages sent by Chainpad to make it compatible with Netflux @@ -265,12 +258,6 @@ define([ wc.history_keeper = hc; if (hc) { network.sendto(hc, JSON.stringify(['GET_HISTORY', wc.id])); } - - toReturn.patchText = TextPatcher.create({ - realtime: realtime, - logging: true - }); - realtime.start(); }; @@ -290,6 +277,16 @@ define([ Netflux.connect(websocketUrl).then(function(network) { // pass messages that come out of netflux into our local handler + network.on('disconnect', function (evt) { + // TODO also abort if Netflux times out + // that will be managed in Netflux-client.js + if (config.onAbort) { + config.onAbort({ + reason: evt.reason + }); + } + }); + network.on('message', function (msg, sender) { // Direct message var wchan = findChannelById(network.webChannels, channel); if(wchan) { diff --git a/www/pad/main.js b/www/pad/main.js index 9429e552c..901fce1c5 100644 --- a/www/pad/main.js +++ b/www/pad/main.js @@ -11,15 +11,18 @@ define([ '/common/json-ot.js', '/common/TypingTests.js', 'json.sortify', + '/common/TextPatcher.js', '/bower_components/diff-dom/diffDOM.js', '/bower_components/jquery/dist/jquery.min.js', '/customize/pad.js' -], function (Config, Messages, Crypto, realtimeInput, Hyperjson, Hyperscript, Toolbar, Cursor, JsonOT, TypingTest, JSONSortify) { +], function (Config, Messages, Crypto, realtimeInput, Hyperjson, Hyperscript, + Toolbar, Cursor, JsonOT, TypingTest, JSONSortify, TextPatcher) { + var $ = window.jQuery; var ifrw = $('#pad-iframe')[0].contentWindow; var Ckeditor; // to be initialized later... var DiffDom = window.diffDOM; - + var stringify = function (obj) { return JSONSortify(obj); }; @@ -288,7 +291,7 @@ define([ var shjson2 = stringifyDOM(inner); if (shjson2 !== shjson) { console.error("shjson2 !== shjson"); - module.realtimeInput.patchText(shjson2); + module.patchText(shjson2); } }; @@ -304,12 +307,21 @@ define([ /* TODO handle disconnects and such*/ }; + // this should only ever get called once, when the chain syncs var onReady = realtimeOptions.onReady = function (info) { - console.log("Unlocking editor"); - initializing = false; - setEditable(true); + module.patchText = TextPatcher.create({ + realtime: info.realtime, + logging: false, + }); + + module.realtime = info.realtime; + var shjson = info.realtime.getUserDoc(); applyHjson(shjson); + + console.log("Unlocking editor"); + setEditable(true); + initializing = false; }; var onAbort = realtimeOptions.onAbort = function (info) { @@ -320,21 +332,9 @@ define([ toolbar.failed(); }; + var onLocal = realtimeOptions.onLocal = function () { + if (initializing) { return; } - - - - var rti = module.realtimeInput = realtimeInput.start(realtimeOptions); - - /* It's incredibly important that you assign 'rti.onLocal' - It's used inside of realtimeInput to make sure that all changes - make it into chainpad. - - It's being assigned this way because it can't be passed in, and - and can't be easily returned from realtime input without making - the code less extensible. - */ - var propogate = rti.onLocal = function () { // serialize your DOM into an object var hjson = Hyperjson.fromDOM(inner, isNotMagicLine, brFilter); @@ -344,12 +344,15 @@ define([ } // stringify the json and send it into chainpad var shjson = stringify(hjson); - if (!rti.patchText(shjson)) { - return; + module.patchText(shjson); + + if (module.realtime.getUserDoc() !== shjson) { + console.error("realtime.getUserDoc() !== shjson"); } - rti.onEvent(shjson); }; + var rti = module.realtimeInput = realtimeInput.start(realtimeOptions); + /* hitting enter makes a new line, but places the cursor inside of the
instead of the

. This makes it such that you cannot type until you click, which is rather unnacceptable. @@ -359,7 +362,7 @@ define([ the first such keypress will not be inserted into the P. */ inner.addEventListener('keydown', cursor.brFix); - editor.on('change', propogate); + editor.on('change', onLocal); // export the typing tests to the window. // call like `test = easyTest()` @@ -367,8 +370,8 @@ define([ var easyTest = window.easyTest = function () { cursor.update(); var start = cursor.Range.start; - var test = TypingTest.testInput(inner, start.el, start.offset, propogate); - propogate(); + var test = TypingTest.testInput(inner, start.el, start.offset, onLocal); + onLocal(); return test; }; });