From b6ccff28db30ff5daab4e517020be13fd4144ca0 Mon Sep 17 00:00:00 2001 From: yflory Date: Tue, 2 Feb 2021 17:12:29 +0100 Subject: [PATCH 1/3] Throttle kanban refresh on remote changes --- www/kanban/inner.js | 23 ++++++++++++++--------- www/kanban/jkanban_cp.js | 19 ++++++++++++++----- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/www/kanban/inner.js b/www/kanban/inner.js index 61a7ad23d..036a563b0 100644 --- a/www/kanban/inner.js +++ b/www/kanban/inner.js @@ -133,6 +133,12 @@ define([ return tags; }; + var updateBoards = Util.throttle(function (framework, kanban, boards) { + kanban.setBoards(Util.clone(boards)); + kanban.inEditMode = false; + addEditItemButton(framework, kanban); + }, 500); + var addEditItemButton = function () {}; var onRemoteChange = Util.mkEvent(); var editModal; @@ -146,10 +152,9 @@ define([ var isBoard, id; var offline = false; - var update = Util.throttle(function () { - kanban.setBoards(kanban.options.boards); - addEditItemButton(framework, kanban); - }, 400); + var update = function () { + updateBoards(framework, kanban, kanban.options.boards); + }; var commit = function () { framework.localChange(); @@ -830,7 +835,8 @@ define([ openLink: openLink, getTags: getExistingTags, cursors: remoteCursors, - boards: boards + boards: boards, + _boards: Util.clone(boards), }); framework._.cpNfInner.metadataMgr.onChange(function () { @@ -841,7 +847,7 @@ define([ // If the rendering has changed, update the value and redraw kanban.options.tagsAnd = tagsAnd; _tagsAnd = tagsAnd; - kanban.setBoards(kanban.options.boards); + updateBoards(kanban.options.boards); }); if (migrated) { framework.localChange(); } @@ -1166,9 +1172,8 @@ define([ if (Sortify(currentContent) !== Sortify(remoteContent)) { var cursor = getCursor(); verbose("Content is different.. Applying content"); - kanban.setBoards(remoteContent); - kanban.inEditMode = false; - addEditItemButton(framework, kanban); + kanban.options.boards = remoteContent; + updateBoards(framework, kanban, remoteContent); restoreCursor(cursor); onRemoteChange.fire(); } diff --git a/www/kanban/jkanban_cp.js b/www/kanban/jkanban_cp.js index f122eed59..bddbfd704 100644 --- a/www/kanban/jkanban_cp.js +++ b/www/kanban/jkanban_cp.js @@ -32,7 +32,8 @@ define([ data: {}, items: {}, list: [] - }, + }, // The realtime kanban + _boards: {}, // The displayed kanban. We need to remember the old columns when we redraw getAvatar: function () {}, openLink: function () {}, getTags: function () {}, @@ -707,21 +708,29 @@ define([ }; this.addBoard = function (board) { if (!board || !board.id) { return; } + // We need to store all the columns in _boards too because it's used to + // remember what columns were already displayed when we redraw (in order to + // preserve their scroll value) var boards = self.options.boards; boards.data = boards.data || {}; boards.list = boards.list || []; + var _boards = self.options._boards; + _boards.data = _boards.data || {}; + _boards.list = _boards.list || []; // If it already there, abort boards.data[board.id] = board; + _boards.data[board.id] = board; if (boards.list.indexOf(board.id) !== -1) { return; } boards.list.push(board.id); + _boards.list.push(board.id); var boardNode = getBoardNode(board); self.container.appendChild(boardNode); }; this.addBoards = function() { //for on all the boards - var boards = self.options.boards; + var boards = self.options._boards; boards.list = boards.list || []; boards.data = boards.data || {}; var toRemove = []; @@ -759,10 +768,10 @@ define([ var $el = $(self.element); var scrollLeft = $el.scrollLeft(); // Get existing boards list - var list = Util.clone(this.options.boards.list); + var list = Util.clone(this.options._boards.list); // Update memory - this.options.boards = boards; + this.options._boards = boards; // If the tab is not focused but a handler already exists: abort if (!Visible.currently() && onVisibleHandler) { return; } @@ -779,7 +788,7 @@ define([ self.addBoards(); self.options.refresh(); // Preserve scroll - self.options.boards.list.forEach(function (id) { + self.options._boards.list.forEach(function (id) { if (!scroll[id]) { return; } $('.kanban-board[data-id="'+id+'"] .kanban-drag').scrollTop(scroll[id]); }); From d17b17c2e3fbd67e4c42945903b4328ea4200b95 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 3 Feb 2021 12:14:05 +0100 Subject: [PATCH 2/3] Better kanban throttle --- www/kanban/inner.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/www/kanban/inner.js b/www/kanban/inner.js index 036a563b0..d32042780 100644 --- a/www/kanban/inner.js +++ b/www/kanban/inner.js @@ -133,13 +133,24 @@ define([ return tags; }; - var updateBoards = Util.throttle(function (framework, kanban, boards) { + var addEditItemButton = function () {}; + + var now = function () { return +new Date(); }; + var _lastUpdate = 0; + var _updateBoards = function (framework, kanban, boards) { + _lastUpdate = now(); kanban.setBoards(Util.clone(boards)); kanban.inEditMode = false; addEditItemButton(framework, kanban); - }, 500); + }; + var _updateBoardsThrottle = Util.throttle(_updateBoards, 500); + var updateBoards = function (framework, kanban, boards) { + if ((now() - _lastUpdate) > 5000) { + _updateBoards(framework, kanban, boards); + } + _updateBoardsThrottle(); + }; - var addEditItemButton = function () {}; var onRemoteChange = Util.mkEvent(); var editModal; var PROPERTIES = ['title', 'body', 'tags', 'color']; From f8ae3a8c8e3775d3506bd739d7d18adf3b385231 Mon Sep 17 00:00:00 2001 From: yflory Date: Wed, 3 Feb 2021 12:14:54 +0100 Subject: [PATCH 3/3] Add missing return --- www/kanban/inner.js | 1 + 1 file changed, 1 insertion(+) diff --git a/www/kanban/inner.js b/www/kanban/inner.js index d32042780..8dea40415 100644 --- a/www/kanban/inner.js +++ b/www/kanban/inner.js @@ -147,6 +147,7 @@ define([ var updateBoards = function (framework, kanban, boards) { if ((now() - _lastUpdate) > 5000) { _updateBoards(framework, kanban, boards); + return; } _updateBoardsThrottle(); };