From 67b02fa5b110b3a5e8619ed82c8c380b81a8e436 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein <leroux@gitter.im> Date: Tue, 14 Jun 2016 14:46:03 +0200 Subject: [PATCH 1/8] onSnapshotReceived modelled after onMongoConnect --- lib/live-collection.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/live-collection.js b/lib/live-collection.js index f08e8ee..2e9d168 100644 --- a/lib/live-collection.js +++ b/lib/live-collection.js @@ -27,6 +27,9 @@ module.exports = Backbone.Collection.extend({ var defaults = { snapshot: true }; options = _.extend(defaults, options); + // indicates if this LiveCollection has received at least one snapshot ever + this.snapshotReceived = false; + if (options.client) { this.client = options.client; } else { @@ -191,10 +194,27 @@ module.exports = Backbone.Collection.extend({ this.reset(snapshot, options); } + this.snapshotReceived = true; + this.trigger('sync'); this.trigger('snapshot'); }, + onSnapshotReceived: function() { + if (this.snapshotReceived) { + debug('Snapshot already received, resolving immediately'); + return Promise.resolve(); + } else { + debug('Awaiting snapshot'); + return new Promise(function(resolve) { + this.once('snapshot', function() { + debug('Snapshot received'); + resolve(); + }); + }); + } + }, + findExistingModel: function(id, newModel) { var existing = this.get(id); if(existing) return existing; From 41efe83ac45a574cf92fbd0307ac02861cdc3916 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein <leroux@gitter.im> Date: Tue, 14 Jun 2016 14:51:35 +0200 Subject: [PATCH 2/8] Rename this.snapshotReceived to this._snapshotReceived --- lib/live-collection.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/live-collection.js b/lib/live-collection.js index 2e9d168..c1927d9 100644 --- a/lib/live-collection.js +++ b/lib/live-collection.js @@ -28,7 +28,7 @@ module.exports = Backbone.Collection.extend({ options = _.extend(defaults, options); // indicates if this LiveCollection has received at least one snapshot ever - this.snapshotReceived = false; + this._snapshotReceived = false; if (options.client) { this.client = options.client; @@ -194,14 +194,14 @@ module.exports = Backbone.Collection.extend({ this.reset(snapshot, options); } - this.snapshotReceived = true; + this._snapshotReceived = true; this.trigger('sync'); this.trigger('snapshot'); }, onSnapshotReceived: function() { - if (this.snapshotReceived) { + if (this._snapshotReceived) { debug('Snapshot already received, resolving immediately'); return Promise.resolve(); } else { From 0bb478ea4c9124782314272471856fede4a38f6c Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein <leroux@gitter.im> Date: Tue, 14 Jun 2016 14:52:55 +0200 Subject: [PATCH 3/8] Aargh this binding. How embarrasssing. --- lib/live-collection.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/live-collection.js b/lib/live-collection.js index c1927d9..aa17007 100644 --- a/lib/live-collection.js +++ b/lib/live-collection.js @@ -201,13 +201,14 @@ module.exports = Backbone.Collection.extend({ }, onSnapshotReceived: function() { + var self = this; if (this._snapshotReceived) { debug('Snapshot already received, resolving immediately'); return Promise.resolve(); } else { debug('Awaiting snapshot'); return new Promise(function(resolve) { - this.once('snapshot', function() { + self.once('snapshot', function() { debug('Snapshot received'); resolve(); }); From 89ac26197e19fe25bb84aa54948e4fda782da0fb Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein <leroux@gitter.im> Date: Tue, 14 Jun 2016 14:58:18 +0200 Subject: [PATCH 4/8] Clear this._snapshotReceived during a bayeaux reset. --- lib/live-collection.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/live-collection.js b/lib/live-collection.js index aa17007..ab38035 100644 --- a/lib/live-collection.js +++ b/lib/live-collection.js @@ -161,6 +161,8 @@ module.exports = Backbone.Collection.extend({ }, _resetOptional: function() { + this._snapshotReceived = false; + if (!this.length) return; // Performance tweak From d49461cbcec820acd1e0fe361411bf72949fc7fe Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein <leroux@gitter.im> Date: Tue, 14 Jun 2016 15:21:21 +0200 Subject: [PATCH 5/8] Reject on subscription error. --- lib/live-collection.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/live-collection.js b/lib/live-collection.js index ab38035..e2846df 100644 --- a/lib/live-collection.js +++ b/lib/live-collection.js @@ -209,11 +209,16 @@ module.exports = Backbone.Collection.extend({ return Promise.resolve(); } else { debug('Awaiting snapshot'); - return new Promise(function(resolve) { + return new Promise(function(resolve, reject) { self.once('snapshot', function() { + var resolved = true; debug('Snapshot received'); resolve(); }); + self.once('subscriptionError', function() { + debug('Error received before promise could get resolved.'); + reject(); + }); }); } }, From 2e5a203e4e95aa8e277a18dcc3bd5679c7ebbbe9 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein <leroux@gitter.im> Date: Wed, 15 Jun 2016 15:32:04 +0200 Subject: [PATCH 6/8] Import bluebird promise. --- lib/live-collection.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/live-collection.js b/lib/live-collection.js index e2846df..c5fadd9 100644 --- a/lib/live-collection.js +++ b/lib/live-collection.js @@ -1,5 +1,7 @@ "use strict"; +var Halley = require('halley/backbone'); +var Promise = Halley.Promise; var _ = require('underscore'); var Backbone = require('backbone'); var backboneUrlResolver = require('backbone-url-resolver'); From 5d15dc08c27f4a8b32b053e5c1c6cae89bacaa9f Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein <leroux@gitter.im> Date: Wed, 15 Jun 2016 15:44:22 +0200 Subject: [PATCH 7/8] use forwarded event --- lib/live-collection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/live-collection.js b/lib/live-collection.js index c5fadd9..718785b 100644 --- a/lib/live-collection.js +++ b/lib/live-collection.js @@ -217,7 +217,7 @@ module.exports = Backbone.Collection.extend({ debug('Snapshot received'); resolve(); }); - self.once('subscriptionError', function() { + self.once('error', function() { debug('Error received before promise could get resolved.'); reject(); }); From 5d07a0a5dd777e63ae138264295c76b50db92751 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein <leroux@gitter.im> Date: Tue, 21 Jun 2016 11:08:29 +0200 Subject: [PATCH 8/8] Pass the error on to reject() --- lib/live-collection.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/live-collection.js b/lib/live-collection.js index 718785b..0330d1a 100644 --- a/lib/live-collection.js +++ b/lib/live-collection.js @@ -217,9 +217,9 @@ module.exports = Backbone.Collection.extend({ debug('Snapshot received'); resolve(); }); - self.once('error', function() { + self.once('error', function(err) { debug('Error received before promise could get resolved.'); - reject(); + reject(err); }); }); }