You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cryptpad/www/common/metadata-manager.js

67 lines
2.2 KiB
JavaScript

7 years ago
define([], function () {
7 years ago
var create = function (sframeChan) {
var personalMetadata = 'uninitialized';
var myID = 'uninitialized';
var members = [];
var metadataObj = 'unintialized';
var dirty = true;
var changeHandlers = [];
7 years ago
7 years ago
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);
7 years ago
};
7 years ago
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();
});
7 years ago
7 years ago
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 });
7 years ago
});