From c3796c61cd69f240ea0f0f97f2ba95ea9ab2c8e9 Mon Sep 17 00:00:00 2001 From: David Baker Date: Wed, 10 Feb 2016 10:40:42 +0000 Subject: [PATCH] Use a promise for when the connection comes back --- lib/sync.js | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/lib/sync.js b/lib/sync.js index b241c339d..4c36ecf54 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -70,6 +70,7 @@ function SyncApi(client, opts) { this._syncState = null; this._running = false; this._keepAliveTimer = null; + this._connectionReturnedDefer = null; } /** @@ -322,8 +323,9 @@ SyncApi.prototype.sync = function() { client.pushRules = result; getFilter(); // Now get the filter }, function(err) { - self._onConnectionReturned = getPushRules; - self._startKeepAlives(); + self._startKeepAlives().done(() => {; + getPushRules(); + }); self._updateSyncState("ERROR", { error: err }); }); } @@ -338,8 +340,9 @@ SyncApi.prototype.sync = function() { debuglog("Using existing filter ID %s", filterId); self._sync({ filterId: filterId }); }, function(err) { - self._onConnectionReturned = getFilter; - self._startKeepAlives(); + self._startKeepAlives().done(() => { + getFilter(); + }); self._updateSyncState("ERROR", { error: err }); }); } @@ -372,7 +375,7 @@ SyncApi.prototype.stop = function() { * @return {boolean} True if this resulted in a request being retried. */ SyncApi.prototype.retryImmediately = function() { - if (!this._onConnectionReturned) { return false; } + if (!this._connectionReturnedDefer) { return false; } this._startKeepAlives(); return true; }; @@ -389,6 +392,10 @@ SyncApi.prototype._sync = function(syncOptions) { if (!this._running) { debuglog("Sync no longer running: exiting."); + if (self._connectionReturnedDefer) { + self._connectionReturnedDefer.reject(); + self._connectionReturnedDefer = null; + } this._updateSyncState("STOPPED"); } @@ -479,6 +486,10 @@ SyncApi.prototype._sync = function(syncOptions) { }, function(err) { if (!self._running) { debuglog("Sync no longer running: exiting"); + if (self._connectionReturnedDefer) { + self._connectionReturnedDefer.reject(); + self._connectionReturnedDefer = null; + } return; } console.error("/sync error %s", err); @@ -486,8 +497,9 @@ SyncApi.prototype._sync = function(syncOptions) { debuglog("Starting keep-alive"); self._syncConnectionLost = true; - self._onConnectionReturned = self._sync.bind(self, syncOptions); - self._startKeepAlives(); + self._startKeepAlives().done(() => { + self._sync(syncOptions); + }); self._currentSyncRequest = null; self._updateSyncState("ERROR", { error: err }); }); @@ -641,6 +653,10 @@ SyncApi.prototype._startKeepAlives = function() { clearTimeout(this._keepAliveTimer); } this._pokeKeepAlive(); + if (!this._connectionReturnedDefer) { + this._connectionReturnedDefer = q.defer(); + } + return this._connectionReturnedDefer.promise; }; /** @@ -650,9 +666,9 @@ SyncApi.prototype._pokeKeepAlive = function() { var self = this; function success() { clearTimeout(self._keepAliveTimer); - if (self._onConnectionReturned) { - self._onConnectionReturned(); - self._onConnectionReturned = undefined; + if (self._connectionReturnedDefer) { + self._connectionReturnedDefer.resolve(); + self._connectionReturnedDefer = null; } }