From 34d023e1f972ed246f418eca2878ac475405cbc8 Mon Sep 17 00:00:00 2001 From: Jonathan Belcher Date: Thu, 27 Aug 2020 17:00:18 -0400 Subject: [PATCH] Only request revisions when the revisions panel is opened (#2305) Change how revisions are requested to not spam Simperium on every note switch. --- lib/state/simperium/middleware.ts | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/state/simperium/middleware.ts b/lib/state/simperium/middleware.ts index 779f45e90..e7b461444 100644 --- a/lib/state/simperium/middleware.ts +++ b/lib/state/simperium/middleware.ts @@ -187,6 +187,8 @@ export const initSimperium = ( const queueNoteUpdate = (noteId: T.EntityId, delay = 2000) => noteQueue.add(noteId, Date.now() + delay); + const hasRequestedRevisions = new Set(); + const tagQueue = new BucketQueue(tagBucket); const queueTagUpdate = (tagHash: T.TagHash, delay = 20) => tagQueue.add(tagHash, Date.now() + delay); @@ -251,7 +253,13 @@ export const initSimperium = ( action.meta?.nextNoteToOpen ?? getState().ui.openedNote; - if (noteId) { + // Preload the revisions when opening a note but only do it if no revisions are in memory + if ( + noteId && + !nextState.data.noteRevisions.get(noteId)?.size && + !hasRequestedRevisions.has(noteId) + ) { + hasRequestedRevisions.add(noteId); setTimeout(() => { if (getState().ui.openedNote === noteId) { noteBucket.getRevisions(noteId).then((revisions) => { @@ -269,6 +277,24 @@ export const initSimperium = ( } }, 250); } + return result; + } + + case 'REVISIONS_TOGGLE': { + const showRevisions = nextState.ui.showRevisions; + const noteId = nextState.ui.openedNote; + + if (noteId && showRevisions) { + noteBucket.getRevisions(noteId).then((revisions) => { + dispatch({ + type: 'LOAD_REVISIONS', + noteId: noteId, + revisions: revisions + .map(({ data, version }): [number, T.Note] => [version, data]) + .sort((a, b) => a[0] - b[0]), + }); + }); + } return result; }