Trigger an onReady() method when the user's pad is fully synced (the history is loaded)

pull/1/head
Yann Flory 9 years ago
parent eca4ec699c
commit e77d85bc82

@ -97,6 +97,7 @@ let handleMessage = function (ctx, user, msg) {
getHistory(ctx, parsed[1], function (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, JSON.stringify(msg)]);
}); });
sendMsg(ctx, user, [0, HISTORY_KEEPER_ID, 'MSG', user.id, 0]);
} }
return; return;
} }

@ -1340,11 +1340,14 @@ return /******/ (function(modules) { // webpackBootstrap
}; };
return; return;
} }
if (msg[2] === 'MSG') {}
// We have received a new direct message from another user // We have received a new direct message from another user
if (msg[2] === 'MSG' && msg[3] === socket.uid) { if (msg[2] === 'MSG' && msg[3] === socket.uid) {
// If it comes form the history keeper, send it to the user // If it comes form the history keeper, send it to the user
if (msg[1] === HISTORY_KEEPER) { if (msg[1] === HISTORY_KEEPER) {
if (msg[4] === 0) {
webChannel.onmessage(msg[1], msg[4]);
return;
}
var msgHistory = JSON.parse(msg[4]); var msgHistory = JSON.parse(msg[4]);
webChannel.onmessage(msgHistory[1], msgHistory[4]); webChannel.onmessage(msgHistory[1], msgHistory[4]);
} }

@ -148,6 +148,7 @@ define([
messagesHistory.forEach(function(msg) { messagesHistory.forEach(function(msg) {
wc.sendTo(toId, '1:y'+msg); wc.sendTo(toId, '1:y'+msg);
}); });
wc.sendTo(toId, '0');
} }
}; };
@ -155,8 +156,12 @@ define([
return '\\' +c; 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); var message = chainpadAdapter.msgIn(peer, msg);
verbose(message); verbose(message);
@ -263,22 +268,7 @@ define([
}); });
}; };
var onOpen = function(wc) { var onReady = 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();
if(config.onInit) { if(config.onInit) {
config.onInit({ config.onInit({
myID: wc.myID, myID: wc.myID,
@ -297,6 +287,25 @@ define([
if (config.onReady) { if (config.onReady) {
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 // On sending message
realtime.onMessage(function(message) { realtime.onMessage(function(message) {
@ -343,6 +352,7 @@ define([
var webchannel = Netflux.create(); var webchannel = Netflux.create();
webchannel.openForJoining(options).then(function(data) { webchannel.openForJoining(options).then(function(data) {
onOpen(webchannel); onOpen(webchannel);
onReady(webchannel);
}, function(error) { }, function(error) {
warn(error); warn(error);
}); });

@ -158,11 +158,9 @@ define([
// apply patches, and try not to lose the cursor in the process! // apply patches, and try not to lose the cursor in the process!
var applyHjson = function (shjson) { var applyHjson = function (shjson) {
var hjson = JSON.parse(shjson); var hjson = JSON.parse(shjson);
console.log(hjson);
var peerUserList = hjson[hjson.length-1]; var peerUserList = hjson[hjson.length-1];
if(peerUserList.mydata) { if(peerUserList.mydata) {
var userData = peerUserList.mydata; var userData = peerUserList.mydata;
console.log(userData);
addToUserList(userData); addToUserList(userData);
delete hjson[hjson.length-1]; delete hjson[hjson.length-1];
} }

Loading…
Cancel
Save