pull/1/head
Caleb James DeLisle 7 years ago
parent 2caaaffc5d
commit 0dde1d7507

@ -1,17 +1,67 @@
define([], function () { 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 checkUpdate = function () {
var getMetadata = function (ctx) { if (!dirty) { return; }
if (metadataObj === 'uninitialized') { throw new Error(); }
}; if (myID === 'uninitialized') { throw new Error(); }
var create = function (sframeChan, cpNfInner) { if (personalMetadata === 'uninitialized') { throw new Error(); }
var ctx = { var mdo = {};
sframeChan: sframeChan, Object.keys(metadataObj).forEach(function (x) {
personalMetadata: {} 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);
}; };
}; sframeChan.on('EV_USERDATA_UPDATE', function (ev) {
return { create: create }; 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 });
}); });

@ -15,57 +15,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
define([ define([
'/bower_components/chainpad/chainpad.dist.js', '/common/metadata-manager.js',
], function () { '/bower_components/chainpad/chainpad.dist.js'
], function (MetadataMgr) {
var ChainPad = window.ChainPad; var ChainPad = window.ChainPad;
var module = { exports: {} }; var module = { exports: {} };
var verbose = function (x) { console.log(x); }; var verbose = function (x) { console.log(x); };
verbose = function () {}; // comment out to enable verbose logging 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) { module.exports.start = function (config) {
var onConnectionChange = config.onConnectionChange || function () { }; var onConnectionChange = config.onConnectionChange || function () { };
var onRemote = config.onRemote || function () { }; var onRemote = config.onRemote || function () { };
@ -84,15 +42,13 @@ define([
config = undefined; config = undefined;
var chainpad; var chainpad;
var userList = mkUserList();
var myID; var myID;
var isReady = false; var isReady = false;
sframeChan.on('EV_RT_JOIN', userList.onJoin); var metadataMgr = MetadataMgr.create(sframeChan);
sframeChan.on('EV_RT_LEAVE', userList.onLeave);
sframeChan.on('EV_RT_DISCONNECT', function () { sframeChan.on('EV_RT_DISCONNECT', function () {
isReady = false; isReady = false;
userList.onReset();
onConnectionChange({ state: false }); onConnectionChange({ state: false });
}); });
sframeChan.on('EV_RT_CONNECT', function (content) { sframeChan.on('EV_RT_CONNECT', function (content) {
@ -121,7 +77,6 @@ define([
onInit({ onInit({
myID: myID, myID: myID,
realtime: chainpad, realtime: chainpad,
userList: userList,
readOnly: readOnly readOnly: readOnly
}); });
}); });
@ -137,13 +92,12 @@ define([
isReady = true; isReady = true;
chainpad.start(); chainpad.start();
setMyID({ myID: myID }); 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 }); onReady({ realtime: chainpad });
}); });
return { return Object.freeze({
getMyID: function () { return myID; } getMyID: function () { return myID; },
}; metadataMgr: metadataMgr
});
}; };
return module.exports; return Object.freeze(module.exports);
}); });

@ -51,21 +51,22 @@ define([
otherWindow.postMessage(JSON.stringify({ content: content, q: e }), '*'); 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 (!otherWindow) { throw new Error('not yet initialized'); }
if (typeof(handlers[queryType]) !== 'undefined') { throw new Error('already registered'); }
if (!SFrameProtocol[queryType]) { if (!SFrameProtocol[queryType]) {
throw new Error('please only register handlers which are defined in sframe-protocol.js'); 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) { handler(data.content, function (replyContent) {
msg.source.postMessage(JSON.stringify({ msg.source.postMessage(JSON.stringify({
txid: data.txid, txid: data.txid,
content: replyContent content: replyContent
}), '*'); }), '*');
}, msg); }, msg);
}; });
event('EV_REGISTER_HANDLER', queryType); if (!quiet) {
event('EV_REGISTER_HANDLER', queryType);
}
}; };
chan.whenReg = function (queryType, handler) { 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]) { if (callWhenRegistered[data.content]) {
callWhenRegistered[data.content].forEach(function (f) { f(); }); callWhenRegistered[data.content].forEach(function (f) { f(); });
delete callWhenRegistered[data.content]; delete callWhenRegistered[data.content];
} }
insideHandlers.push(data.content); insideHandlers.push(data.content);
}; });
var intr; var intr;
var txid; var txid;
@ -104,9 +105,12 @@ define([
otherWindow = ow; otherWindow = ow;
cb(chan); cb(chan);
} else if (typeof(data.q) === 'string' && handlers[data.q]) { } 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]) { } else if (typeof(data.q) === 'undefined' && queries[data.txid]) {
queries[data.txid](data, msg); queries[data.txid](msg, msg);
} else if (data.txid === txid) { } else if (data.txid === txid) {
// stray message from init // stray message from init
return; return;

@ -660,10 +660,10 @@ define([
realtimeOptions.onConnectionChange = function (info) { realtimeOptions.onConnectionChange = function (info) {
setEditable(info.state); setEditable(info.state);
toolbar.failed(); //toolbar.failed(); TODO
if (info.state) { if (info.state) {
initializing = true; initializing = true;
toolbar.reconnecting(info.myId); //toolbar.reconnecting(info.myId); // TODO
Cryptpad.findOKButton().click(); Cryptpad.findOKButton().click();
} else { } else {
Cryptpad.alert(Messages.common_connectionLost, undefined, true); Cryptpad.alert(Messages.common_connectionLost, undefined, true);

Loading…
Cancel
Save