diff options
Diffstat (limited to '')
-rw-r--r-- | lib/realtime.js | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/lib/realtime.js b/lib/realtime.js index fadea4f2..c1db6886 100644 --- a/lib/realtime.js +++ b/lib/realtime.js @@ -122,6 +122,12 @@ function updateNote(note, callback) { } }).then(function (_note) { if (!_note) return callback(null, null); + // update user note history + var tempUsers = Object.assign({}, note.tempUsers); + note.tempUsers = {}; + Object.keys(tempUsers).forEach(function (key) { + updateHistory(key, note, tempUsers[key]); + }); if (note.lastchangeuser) { if (_note.lastchangeuserId != note.lastchangeuser) { models.User.findOne({ @@ -348,9 +354,12 @@ function clearSocketQueue(queue, socket) { } function connectNextSocket() { - isConnectionBusy = false; - if (connectionSocketQueue.length > 0) - startConnection(connectionSocketQueue[0]); + setTimeout(function () { + isConnectionBusy = false; + if (connectionSocketQueue.length > 0) { + startConnection(connectionSocketQueue[0]); + } + }, 1); } function interruptConnection(socket, note, user) { @@ -405,10 +414,7 @@ function finishConnection(socket, note, user) { note.server.setColor(socket, user.color); // update user note history - setTimeout(function () { - var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id); - if (note.server) history.updateHistory(user.userid, noteId, note.server.document); - }, 0); + updateHistory(user.userid, note); emitOnlineUsers(socket); emitRefresh(socket); @@ -497,6 +503,7 @@ function startConnection(socket) { lastchangeuserprofile: lastchangeuserprofile, socks: [], users: {}, + tempUsers: {}, createtime: moment(createtime).valueOf(), updatetime: moment(updatetime).valueOf(), server: server, @@ -687,15 +694,17 @@ function operationCallback(socket, operation) { return logger.error('operation callback failed: ' + err); }); } - // update user note history - setTimeout(function() { - var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id); - if (note.server) history.updateHistory(userId, noteId, note.server.document); - }, 0); - + note.tempUsers[userId] = Date.now(); } - // save authorship - note.authorship = models.Note.updateAuthorshipByOperation(operation, userId, note.authorship); + // save authorship - use timer here because it's an O(n) complexity algorithm + setImmediate(function () { + note.authorship = models.Note.updateAuthorshipByOperation(operation, userId, note.authorship); + }); +} + +function updateHistory(userId, note, time) { + var noteId = note.alias ? note.alias : LZString.compressToBase64(note.id); + if (note.server) history.updateHistory(userId, noteId, note.server.document, time); } function connection(socket) { @@ -925,4 +934,4 @@ function connection(socket) { }); } -module.exports = realtime; +module.exports = realtime;
\ No newline at end of file |