Skip to content

Commit f625157

Browse files
committed
Cache revisions
Revisions are "basically" immutable. I think there are cases they can change, but for now, we can prevent re-fetching ones we have already requested. This will balloon memory but it's a fair tradeoff to refetching the same big list of revisions every time the history selector is opened.
1 parent ebbf31a commit f625157

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

src/simperium/channel.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,16 @@ LocalQueue.prototype.resendSentChanges = function() {
647647
}
648648
}
649649

650+
/**
651+
* Since revision data is basically immutable we can prevent the
652+
* need to refetch it after it has been loaded once.
653+
*
654+
* E.g. key could be `${ entityId }.${ versionNumber }`
655+
*
656+
* @type {Map<String,Object>} stores specific revisions as a cache
657+
*/
658+
export const revisionCache = new Map();
659+
650660
/**
651661
* Attempts to fetch an entity's revisions
652662
*
@@ -692,6 +702,7 @@ function collectionRevisions( channel, id, callback ) {
692702
* @param {Object} data value of entity at revision
693703
*/
694704
function onVersion( id, version, data ) {
705+
revisionCache.set( `${ id }.${ version }`, data );
695706
versions.push( { id, version, data } );
696707

697708
// if we have every possible revision already, finish it!
@@ -726,7 +737,7 @@ function collectionRevisions( channel, id, callback ) {
726737
* @param {Number} prevVersion starting point for finding next version
727738
*/
728739
function fetchNextVersion( prevVersion ) {
729-
let version = prevVersion - 1;
740+
let version = prevVersion;
730741

731742
// find the next version to request
732743
// some could have come back already
@@ -735,8 +746,19 @@ function collectionRevisions( channel, id, callback ) {
735746
version -= 1;
736747
}
737748

738-
channel.send( `e:${ id }.${ version }` );
749+
// we have them all
750+
if ( ! version ) {
751+
return;
752+
}
753+
739754
requestedVersions.add( version );
755+
756+
// fetch from server or local cache
757+
if ( revisionCache.has( `${ id }.${ version }` ) ) {
758+
onVersion( id, version, revisionCache.get( `${ id }.${ version }` ) );
759+
} else {
760+
channel.send( `e:${ id }.${ version }` );
761+
}
740762
}
741763

742764
// start listening for the responses

0 commit comments

Comments
 (0)