You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-26 17:03:12 +03:00
Use a promise for when the connection comes back
This commit is contained in:
36
lib/sync.js
36
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user