This commit is contained in:
Caleb James DeLisle 2017-08-10 14:49:21 +02:00
parent 2caaaffc5d
commit 0dde1d7507
4 changed files with 86 additions and 78 deletions

View File

@ -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 });
});

View File

@ -15,57 +15,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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);
});

View File

@ -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;

View File

@ -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);