From e77d85bc825b54bfb6836430dcb7c4e8cb0ae233 Mon Sep 17 00:00:00 2001 From: Yann Flory Date: Wed, 16 Mar 2016 14:00:14 +0100 Subject: [PATCH] Trigger an onReady() method when the user's pad is fully synced (the history is loaded) --- NetFluxWebsocketServer.js | 1 + www/common/netflux.js | 5 +++- www/common/realtime-input.js | 44 ++++++++++++++++++++++-------------- www/vdom/main.js | 2 -- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/NetFluxWebsocketServer.js b/NetFluxWebsocketServer.js index 3ffb96fe7..a1d73fb12 100644 --- a/NetFluxWebsocketServer.js +++ b/NetFluxWebsocketServer.js @@ -97,6 +97,7 @@ let handleMessage = function (ctx, user, msg) { getHistory(ctx, parsed[1], function (msg) { sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, JSON.stringify(msg)]); }); + sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, 0]); } return; } diff --git a/www/common/netflux.js b/www/common/netflux.js index 68a90e386..9dbbade75 100644 --- a/www/common/netflux.js +++ b/www/common/netflux.js @@ -1340,11 +1340,14 @@ return /******/ (function(modules) { // webpackBootstrap }; return; } - if (msg[2] === 'MSG') {} // We have received a new direct message from another user if (msg[2] === 'MSG' && msg[3] === socket.uid) { // If it comes form the history keeper, send it to the user if (msg[1] === HISTORY_KEEPER) { + if (msg[4] === 0) { + webChannel.onmessage(msg[1], msg[4]); + return; + } var msgHistory = JSON.parse(msg[4]); webChannel.onmessage(msgHistory[1], msgHistory[4]); } diff --git a/www/common/realtime-input.js b/www/common/realtime-input.js index fd69f6da0..d39bae228 100644 --- a/www/common/realtime-input.js +++ b/www/common/realtime-input.js @@ -148,6 +148,7 @@ define([ messagesHistory.forEach(function(msg) { wc.sendTo(toId, '1:y'+msg); }); + wc.sendTo(toId, '0'); } }; @@ -155,8 +156,12 @@ define([ return '\\' +c; })); - var onMessage = function(peer, msg) { + var onMessage = function(peer, msg, wc) { + if(msg === 0 || msg === '0') { + onReady(wc); + return; + } var message = chainpadAdapter.msgIn(peer, msg); verbose(message); @@ -263,22 +268,7 @@ define([ }); }; - var onOpen = function(wc) { - // Add the handlers to the WebChannel - wc.onmessage = onMessage; // On receiving message - wc.onJoining = onJoining; // On user joining the session - wc.onLeaving = onLeaving; // On user leaving the session - wc.onPeerMessage = function(peerId, type) { - onPeerMessage(peerId, type, wc); - }; - if(config.setMyID) { - config.setMyID({ - myID: wc.myID - }); - } - // Open a Chainpad session - realtime = createRealtime(); - + var onReady = function(wc) { if(config.onInit) { config.onInit({ myID: wc.myID, @@ -297,6 +287,25 @@ define([ if (config.onReady) { config.onReady(); } + } + + var onOpen = function(wc) { + // Add the handlers to the WebChannel + wc.onmessage = function(peer, msg) { // On receiving message + onMessage(peer, msg, wc); + }; + wc.onJoining = onJoining; // On user joining the session + wc.onLeaving = onLeaving; // On user leaving the session + wc.onPeerMessage = function(peerId, type) { + onPeerMessage(peerId, type, wc); + }; + if(config.setMyID) { + config.setMyID({ + myID: wc.myID + }); + } + // Open a Chainpad session + realtime = createRealtime(); // On sending message realtime.onMessage(function(message) { @@ -343,6 +352,7 @@ define([ var webchannel = Netflux.create(); webchannel.openForJoining(options).then(function(data) { onOpen(webchannel); + onReady(webchannel); }, function(error) { warn(error); }); diff --git a/www/vdom/main.js b/www/vdom/main.js index e849166c7..9f055462d 100644 --- a/www/vdom/main.js +++ b/www/vdom/main.js @@ -158,11 +158,9 @@ define([ // apply patches, and try not to lose the cursor in the process! var applyHjson = function (shjson) { var hjson = JSON.parse(shjson); - console.log(hjson); var peerUserList = hjson[hjson.length-1]; if(peerUserList.mydata) { var userData = peerUserList.mydata; - console.log(userData); addToUserList(userData); delete hjson[hjson.length-1]; }