Skip to content

Commit 693da5b

Browse files
committed
Refactor and add timeout
Since we're opening up more revisions I realize that this could result in delays getting the revisions that could cause pain in the app. This revision sets a new hard timeout at ten seconds so that if for some reason we haven't been able to load all of the requested revisions we can at least push out the ones we did get in that time.
1 parent eb0895a commit 693da5b

1 file changed

Lines changed: 28 additions & 23 deletions

File tree

src/simperium/channel.js

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -643,39 +643,44 @@ LocalQueue.prototype.resendSentChanges = function() {
643643
}
644644

645645
function collectionRevisions( channel, id, callback ) {
646-
var expectedVersions = -1;
647-
var onGhostRetrieved = function( ghost ) {
648-
// the default bucket options allow for storing
649-
// the 60 most-recent revisions of a note plus
650-
// 100 archive versions (these store one out of
651-
// every ten versions). we'll get up to this many
652-
var version = Math.min( ghost.version, 160 );
653-
var i;
654-
expectedVersions = version;
646+
let expectedVersions;
647+
let timeout;
648+
const TIMEOUT = 10000; // arbitrarily chosen delay
655649

656-
// Loop through requested revision count and request each version
657-
for ( i = 0; i < version; i++ ) {
658-
channel.send( 'e:' + id + '.' + ( ghost.version - i ) );
659-
}
650+
const versions = [];
651+
652+
const finish = listener => {
653+
channel.removeListener( 'version.' + id, listener );
654+
callback( null, versions.sort( ( a, b ) => a.version - b.version ) );
655+
clearTimeout( timeout );
660656
};
661657

662-
var versions = [];
663-
var onVersion = function( id, version, data ) {
664-
versions.push( {id: id, version: version, data: data} );
658+
const onVersion = ( id, version, data ) => {
659+
versions.push( { id, version, data } );
665660

666661
// Check if all versions have been collected
667662
if ( expectedVersions === versions.length ) {
668-
channel.removeListener( 'version.' + id, onVersion );
669-
callback( null, versions.sort( function( a, b ) {
670-
return a.version > b.version ? -1 : 1;
671-
} ) );
663+
finish( onVersion );
672664
}
673665
};
674666

675667
channel.on( 'version.' + id, onVersion );
676668

677-
channel.store.get( id ).then( onGhostRetrieved, function( e ) {
678-
callback( e );
679-
} );
669+
channel.store.get( id ).then( ghost => {
670+
// the default bucket options allow for storing
671+
// the 60 most-recent revisions of a note plus
672+
// 100 archive versions (these store one out of
673+
// every ten versions). we'll get up to this many
674+
const version = Math.min( ghost.version, 160 );
675+
expectedVersions = version;
676+
677+
// Loop through requested revision count and request each version
678+
for ( let i = 0; i < version; i++ ) {
679+
channel.send( 'e:' + id + '.' + ( ghost.version - i ) );
680+
}
681+
}, callback );
682+
683+
// give up after a timeout
684+
timeout = setTimeout( () => finish( onVersion ), TIMEOUT );
680685
}
681686

0 commit comments

Comments
 (0)