Add lazy metadata to fix parse errors in pad2

pull/1/head
yflory 7 years ago
parent 226ef1fd00
commit fc5bdd3bed

@ -4,10 +4,11 @@ define([], function () {
var meta = UNINIT;
var members = [];
var metadataObj = UNINIT;
var metadataLazyObj = UNINIT;
var dirty = true;
var changeHandlers = [];
var checkUpdate = function () {
var checkUpdate = function (lazy) {
if (!dirty) { return; }
if (meta === UNINIT) { throw new Error(); }
if (metadataObj === UNINIT) {
@ -17,6 +18,7 @@ define([], function () {
type: meta.doc.type,
users: {}
};
metadataLazyObj = JSON.parse(JSON.stringify(metadataObj));
}
var mdo = {};
// We don't want to add our user data to the object multiple times.
@ -34,56 +36,65 @@ define([], function () {
//if (!containsYou) { mdo[meta.user.netfluxId] = meta.user; }
mdo[meta.user.netfluxId] = meta.user;
metadataObj.users = mdo;
if (lazy) {
metadataLazyObj.users = mdo;
}
dirty = false;
changeHandlers.forEach(function (f) { f(); });
};
var change = function () {
var change = function (lazy) {
dirty = true;
setTimeout(checkUpdate);
setTimeout(function () {
checkUpdate(lazy);
});
};
sframeChan.on('EV_METADATA_UPDATE', function (ev) {
meta = ev;
change();
change(true);
});
sframeChan.on('EV_RT_CONNECT', function (ev) {
meta.user.netfluxId = ev.myID;
members = ev.members;
change();
change(true);
});
sframeChan.on('EV_RT_JOIN', function (ev) {
members.push(ev);
change();
change(false);
});
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();
change(false);
});
sframeChan.on('EV_RT_DISCONNECT', function () {
members = [];
change();
change(true);
});
return Object.freeze({
updateMetadata: function (m) {
if (JSON.stringify(metadataObj) === JSON.stringify(m)) { return; }
metadataObj = m;
change();
change(true);
},
getMetadata: function () {
checkUpdate();
checkUpdate(false);
return metadataObj;
},
getMetadataLazy: function () {
return metadataLazyObj;
},
onChange: function (f) { changeHandlers.push(f); },
getNetfluxId: function () {
return meta && meta.user && meta.user.netfluxId;
isConnected : function () {
return members.indexOf(meta.user.netfluxId) !== -1;
},
getUserlist: function () {
getViewers : function () {
checkUpdate(false);
var list = members.slice().filter(function (m) { return m.length === 32; });
return list;
return list.length - Object.keys(metadataObj.users).length;
}
});
};

@ -122,7 +122,7 @@ define([
// Userlist elements
var getOtherUsers = function(config) {
var userList = config.userList.getUserlist();
//var userList = config.userList.getUserlist();
var userData = config.userList.getMetadata().users;
var i = 0; // duplicates counter
@ -130,12 +130,12 @@ define([
// Display only one time each user (if he is connected in multiple tabs)
var uids = [];
userList.forEach(function(user) {
Object.keys(userData).forEach(function(user) {
//if (user !== userNetfluxId) {
var data = userData[user] || {};
var userId = data.uid;
if (!userId) { return; }
data.netfluxId = user;
//data.netfluxId = user;
if (uids.indexOf(userId) === -1) {// && (!myUid || userId !== myUid)) {
uids.push(userId);
list.push(data);
@ -176,16 +176,15 @@ define([
var $userButtons = toolbar.userlist;
var $userlistContent = toolbar.userlistContent;
var userList = config.userList.getUserlist();
var userData = config.userList.getMetadata().users;
console.log(userList, userData);
var numberOfUsers = userList.length;
var metadataMgr = config.userList;
var userData = metadataMgr.getMetadata().users;
var viewers = metadataMgr.getViewers();
// If we are using old pads (readonly unavailable), only editing users are in userList.
// With new pads, we also have readonly users in userList, so we have to intersect with
// the userData to have only the editing users. We can't use userData directly since it
// may contain data about users that have already left the channel.
userList = config.readOnly === -1 ? userList : arrayIntersect(userList, Object.keys(userData));
//userList = config.readOnly === -1 ? userList : arrayIntersect(userList, Object.keys(userData));
// Names of editing users
var others = getOtherUsers(config);
@ -198,8 +197,8 @@ console.log(userList, userData);
return na.toLowerCase() > nb.toLowerCase();
});
var numberOfEditUsers = userList.length - duplicates;
var numberOfViewUsers = numberOfUsers - userList.length;
var numberOfEditUsers = Object.keys(userData).length - duplicates;
var numberOfViewUsers = viewers;
// Update the userlist
var $editUsers = $userlistContent.find('.' + USERLIST_CLS).html('');
@ -287,8 +286,7 @@ console.log(userList, userData);
//userList.change.push
var metadataMgr = config.userList;
metadataMgr.onChange(function () {
var users = metadataMgr.getUserlist();
if (users.indexOf(metadataMgr.getNetfluxId()) !== -1) {toolbar.connected = true;}
if (metadataMgr.isConnected()) {toolbar.connected = true;}
if (!toolbar.connected) { return; }
//if (config.userList.data) {
updateUserList(toolbar, config);

@ -341,7 +341,7 @@ define([
var stringifyDOM = module.stringifyDOM = function (dom) {
var hjson = Hyperjson.fromDOM(dom, isNotMagicLine, brFilter);
hjson[3] = {
metadata: cpNfInner.metadataMgr.getMetadata()
metadata: cpNfInner.metadataMgr.getMetadataLazy()
};
/*hjson[3] = { TODO
users: UserList.userData,
@ -470,8 +470,11 @@ define([
realtimeOptions.onInit = function (info) {
console.log('onInit');
var titleCfg = { getHeadingText: getHeadingText };
Title = Cryptpad.createTitle(titleCfg, realtimeOptions.onLocal, Cryptpad);
var configTb = {
displayed: ['userlist'],
//title: Title.getTitleConfig(),
userList: cpNfInner.metadataMgr,
readOnly: readOnly,
ifrw: window,

Loading…
Cancel
Save