From 0dde1d750740ddbf62d40a0922de3210e5d9bd72 Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Thu, 10 Aug 2017 14:49:21 +0200 Subject: [PATCH] wip --- www/common/metadata-manager.js | 72 +++++++++++++++++---- www/common/sframe-chainpad-netflux-inner.js | 66 +++---------------- www/common/sframe-channel.js | 22 ++++--- www/pad2/main.js | 4 +- 4 files changed, 86 insertions(+), 78 deletions(-) diff --git a/www/common/metadata-manager.js b/www/common/metadata-manager.js index 9d26d5588..822880a02 100644 --- a/www/common/metadata-manager.js +++ b/www/common/metadata-manager.js @@ -1,17 +1,67 @@ define([], function () { - var metadataChange = function (ctx, newMeta) { + var create = function (sframeChan) { + var personalMetadata = 'uninitialized'; + var myID = 'uninitialized'; + var members = []; + var metadataObj = 'unintialized'; + var dirty = true; + var changeHandlers = []; - }; - var getMetadata = function (ctx) { - - }; - var create = function (sframeChan, cpNfInner) { - var ctx = { - sframeChan: sframeChan, - personalMetadata: {} + var checkUpdate = function () { + if (!dirty) { return; } + if (metadataObj === 'uninitialized') { throw new Error(); } + if (myID === 'uninitialized') { throw new Error(); } + if (personalMetadata === 'uninitialized') { throw new Error(); } + var mdo = {}; + Object.keys(metadataObj).forEach(function (x) { + if (members.indexOf(x) === -1) { return; } + mdo[x] = metadataObj[x]; + }); + mdo[myID] = personalMetadata; + metadataObj = mdo; + dirty = false; + changeHandlers.forEach(function (f) { f(); }); + }; + var change = function () { + dirty = true; + setTimeout(checkUpdate); }; - }; - return { create: create }; + sframeChan.on('EV_USERDATA_UPDATE', function (ev) { + personalMetadata = ev; + change(); + }); + sframeChan.on('EV_RT_CONNECT', function (ev) { + myID = ev.myID; + members = ev.members; + change(); + }); + sframeChan.on('EV_RT_JOIN', function (ev) { + members.push(ev); + change(); + }); + sframeChan.on('EV_RT_LEAVE', function (ev) { + var idx = members.indexOf(ev); + if (idx === -1) { console.log('Error: ' + ev + ' not in members'); return; } + members.splice(idx, 1); + change(); + }); + sframeChan.on('EV_RT_DISCONNECT', function () { + members = []; + change(); + }); + return Object.freeze({ + metadataChange: function (meta) { + metadataObj = meta; + change(); + }, + getMetadata: function () { + checkUpdate(); + return metadataObj; + }, + onChange: function (f) { changeHandlers.push(f); } + }); + }; + return Object.freeze({ create: create }); }); \ No newline at end of file diff --git a/www/common/sframe-chainpad-netflux-inner.js b/www/common/sframe-chainpad-netflux-inner.js index efce47a91..d4bd31f0e 100644 --- a/www/common/sframe-chainpad-netflux-inner.js +++ b/www/common/sframe-chainpad-netflux-inner.js @@ -15,57 +15,15 @@ * along with this program. If not, see . */ define([ - '/bower_components/chainpad/chainpad.dist.js', -], function () { + '/common/metadata-manager.js', + '/bower_components/chainpad/chainpad.dist.js' +], function (MetadataMgr) { var ChainPad = window.ChainPad; var module = { exports: {} }; var verbose = function (x) { console.log(x); }; verbose = function () {}; // comment out to enable verbose logging - var mkUserList = function () { - var userList = Object.freeze({ - change : [], - onChange : function(newData) { - userList.change.forEach(function (el) { - el(newData); - }); - }, - users: [] - }); - - var onJoining = function (peer) { - if(peer.length !== 32) { return; } - var list = userList.users; - var index = list.indexOf(peer); - if(index === -1) { - userList.users.push(peer); - } - userList.onChange(); - }; - - // update UI components to show that one of the other peers has left - var onLeaving = function (peer) { - var list = userList.users; - var index = list.indexOf(peer); - if(index !== -1) { - userList.users.splice(index, 1); - } - userList.onChange(); - }; - - var onReset = function () { - userList.users.forEach(onLeaving); - }; - - return Object.freeze({ - list: userList, - onJoin: onJoining, - onLeave: onLeaving, - onReset: onReset - }); - }; - module.exports.start = function (config) { var onConnectionChange = config.onConnectionChange || function () { }; var onRemote = config.onRemote || function () { }; @@ -84,15 +42,13 @@ define([ config = undefined; var chainpad; - var userList = mkUserList(); var myID; var isReady = false; - sframeChan.on('EV_RT_JOIN', userList.onJoin); - sframeChan.on('EV_RT_LEAVE', userList.onLeave); + var metadataMgr = MetadataMgr.create(sframeChan); + sframeChan.on('EV_RT_DISCONNECT', function () { isReady = false; - userList.onReset(); onConnectionChange({ state: false }); }); sframeChan.on('EV_RT_CONNECT', function (content) { @@ -121,7 +77,6 @@ define([ onInit({ myID: myID, realtime: chainpad, - userList: userList, readOnly: readOnly }); }); @@ -137,13 +92,12 @@ define([ isReady = true; chainpad.start(); setMyID({ myID: myID }); - // Trigger onJoining with our own Cryptpad username to tell the toolbar that we are synced - if (!readOnly) { userList.onJoin(myID); } onReady({ realtime: chainpad }); }); - return { - getMyID: function () { return myID; } - }; + return Object.freeze({ + getMyID: function () { return myID; }, + metadataMgr: metadataMgr + }); }; - return module.exports; + return Object.freeze(module.exports); }); \ No newline at end of file diff --git a/www/common/sframe-channel.js b/www/common/sframe-channel.js index 12b390fa7..3dd0a3d8d 100644 --- a/www/common/sframe-channel.js +++ b/www/common/sframe-channel.js @@ -51,21 +51,22 @@ define([ otherWindow.postMessage(JSON.stringify({ content: content, q: e }), '*'); }; - chan.on = function (queryType, handler) { + chan.on = function (queryType, handler, quiet) { if (!otherWindow) { throw new Error('not yet initialized'); } - if (typeof(handlers[queryType]) !== 'undefined') { throw new Error('already registered'); } if (!SFrameProtocol[queryType]) { throw new Error('please only register handlers which are defined in sframe-protocol.js'); } - handlers[queryType] = function (data, msg) { + (handlers[queryType] = handlers[queryType] || []).push(function (data, msg) { handler(data.content, function (replyContent) { msg.source.postMessage(JSON.stringify({ txid: data.txid, content: replyContent }), '*'); }, msg); - }; - event('EV_REGISTER_HANDLER', queryType); + }); + if (!quiet) { + event('EV_REGISTER_HANDLER', queryType); + } }; chan.whenReg = function (queryType, handler) { @@ -80,13 +81,13 @@ define([ } }; - handlers['EV_REGISTER_HANDLER'] = function (data) { + (handlers['EV_REGISTER_HANDLER'] = handlers['EV_REGISTER_HANDLER'] || []).push(function (data) { if (callWhenRegistered[data.content]) { callWhenRegistered[data.content].forEach(function (f) { f(); }); delete callWhenRegistered[data.content]; } insideHandlers.push(data.content); - }; + }); var intr; var txid; @@ -104,9 +105,12 @@ define([ otherWindow = ow; cb(chan); } else if (typeof(data.q) === 'string' && handlers[data.q]) { - handlers[data.q](data, msg); + handlers[data.q].forEach(function (f) { + f(data || JSON.parse(msg.data), msg); + data = undefined; + }); } else if (typeof(data.q) === 'undefined' && queries[data.txid]) { - queries[data.txid](data, msg); + queries[data.txid](msg, msg); } else if (data.txid === txid) { // stray message from init return; diff --git a/www/pad2/main.js b/www/pad2/main.js index 064153914..675b05a25 100644 --- a/www/pad2/main.js +++ b/www/pad2/main.js @@ -660,10 +660,10 @@ define([ realtimeOptions.onConnectionChange = function (info) { setEditable(info.state); - toolbar.failed(); + //toolbar.failed(); TODO if (info.state) { initializing = true; - toolbar.reconnecting(info.myId); + //toolbar.reconnecting(info.myId); // TODO Cryptpad.findOKButton().click(); } else { Cryptpad.alert(Messages.common_connectionLost, undefined, true);