Skip to content

Commit

Permalink
Only request revisions when the revisions panel is opened (#2305)
Browse files Browse the repository at this point in the history
Change how revisions are requested to not spam Simperium on every note switch.
  • Loading branch information
belcherj authored Aug 27, 2020
1 parent 8b17326 commit 34d023e
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion lib/state/simperium/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ export const initSimperium = (
const queueNoteUpdate = (noteId: T.EntityId, delay = 2000) =>
noteQueue.add(noteId, Date.now() + delay);

const hasRequestedRevisions = new Set<T.EntityId>();

const tagQueue = new BucketQueue(tagBucket);
const queueTagUpdate = (tagHash: T.TagHash, delay = 20) =>
tagQueue.add(tagHash, Date.now() + delay);
Expand Down Expand Up @@ -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) => {
Expand All @@ -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;
}
Expand Down

0 comments on commit 34d023e

Please sign in to comment.