From ef0ac7768d5dbd6a99c4126a1ed8b091f004d3bf Mon Sep 17 00:00:00 2001 From: Wu Cheng-Han Date: Fri, 3 Feb 2017 21:47:38 +0800 Subject: [PATCH] Update realtime to use timer to avoid memory leaks on busy tick --- lib/realtime.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/realtime.js b/lib/realtime.js index def8f217..c1db6886 100644 --- a/lib/realtime.js +++ b/lib/realtime.js @@ -354,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) { @@ -693,8 +696,11 @@ function operationCallback(socket, operation) { } 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);