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._syncState = null;
|
||||||
this._running = false;
|
this._running = false;
|
||||||
this._keepAliveTimer = null;
|
this._keepAliveTimer = null;
|
||||||
|
this._connectionReturnedDefer = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -322,8 +323,9 @@ SyncApi.prototype.sync = function() {
|
|||||||
client.pushRules = result;
|
client.pushRules = result;
|
||||||
getFilter(); // Now get the filter
|
getFilter(); // Now get the filter
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
self._onConnectionReturned = getPushRules;
|
self._startKeepAlives().done(() => {;
|
||||||
self._startKeepAlives();
|
getPushRules();
|
||||||
|
});
|
||||||
self._updateSyncState("ERROR", { error: err });
|
self._updateSyncState("ERROR", { error: err });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -338,8 +340,9 @@ SyncApi.prototype.sync = function() {
|
|||||||
debuglog("Using existing filter ID %s", filterId);
|
debuglog("Using existing filter ID %s", filterId);
|
||||||
self._sync({ filterId: filterId });
|
self._sync({ filterId: filterId });
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
self._onConnectionReturned = getFilter;
|
self._startKeepAlives().done(() => {
|
||||||
self._startKeepAlives();
|
getFilter();
|
||||||
|
});
|
||||||
self._updateSyncState("ERROR", { error: err });
|
self._updateSyncState("ERROR", { error: err });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -372,7 +375,7 @@ SyncApi.prototype.stop = function() {
|
|||||||
* @return {boolean} True if this resulted in a request being retried.
|
* @return {boolean} True if this resulted in a request being retried.
|
||||||
*/
|
*/
|
||||||
SyncApi.prototype.retryImmediately = function() {
|
SyncApi.prototype.retryImmediately = function() {
|
||||||
if (!this._onConnectionReturned) { return false; }
|
if (!this._connectionReturnedDefer) { return false; }
|
||||||
this._startKeepAlives();
|
this._startKeepAlives();
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
@@ -389,6 +392,10 @@ SyncApi.prototype._sync = function(syncOptions) {
|
|||||||
|
|
||||||
if (!this._running) {
|
if (!this._running) {
|
||||||
debuglog("Sync no longer running: exiting.");
|
debuglog("Sync no longer running: exiting.");
|
||||||
|
if (self._connectionReturnedDefer) {
|
||||||
|
self._connectionReturnedDefer.reject();
|
||||||
|
self._connectionReturnedDefer = null;
|
||||||
|
}
|
||||||
this._updateSyncState("STOPPED");
|
this._updateSyncState("STOPPED");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -479,6 +486,10 @@ SyncApi.prototype._sync = function(syncOptions) {
|
|||||||
}, function(err) {
|
}, function(err) {
|
||||||
if (!self._running) {
|
if (!self._running) {
|
||||||
debuglog("Sync no longer running: exiting");
|
debuglog("Sync no longer running: exiting");
|
||||||
|
if (self._connectionReturnedDefer) {
|
||||||
|
self._connectionReturnedDefer.reject();
|
||||||
|
self._connectionReturnedDefer = null;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.error("/sync error %s", err);
|
console.error("/sync error %s", err);
|
||||||
@@ -486,8 +497,9 @@ SyncApi.prototype._sync = function(syncOptions) {
|
|||||||
|
|
||||||
debuglog("Starting keep-alive");
|
debuglog("Starting keep-alive");
|
||||||
self._syncConnectionLost = true;
|
self._syncConnectionLost = true;
|
||||||
self._onConnectionReturned = self._sync.bind(self, syncOptions);
|
self._startKeepAlives().done(() => {
|
||||||
self._startKeepAlives();
|
self._sync(syncOptions);
|
||||||
|
});
|
||||||
self._currentSyncRequest = null;
|
self._currentSyncRequest = null;
|
||||||
self._updateSyncState("ERROR", { error: err });
|
self._updateSyncState("ERROR", { error: err });
|
||||||
});
|
});
|
||||||
@@ -641,6 +653,10 @@ SyncApi.prototype._startKeepAlives = function() {
|
|||||||
clearTimeout(this._keepAliveTimer);
|
clearTimeout(this._keepAliveTimer);
|
||||||
}
|
}
|
||||||
this._pokeKeepAlive();
|
this._pokeKeepAlive();
|
||||||
|
if (!this._connectionReturnedDefer) {
|
||||||
|
this._connectionReturnedDefer = q.defer();
|
||||||
|
}
|
||||||
|
return this._connectionReturnedDefer.promise;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -650,9 +666,9 @@ SyncApi.prototype._pokeKeepAlive = function() {
|
|||||||
var self = this;
|
var self = this;
|
||||||
function success() {
|
function success() {
|
||||||
clearTimeout(self._keepAliveTimer);
|
clearTimeout(self._keepAliveTimer);
|
||||||
if (self._onConnectionReturned) {
|
if (self._connectionReturnedDefer) {
|
||||||
self._onConnectionReturned();
|
self._connectionReturnedDefer.resolve();
|
||||||
self._onConnectionReturned = undefined;
|
self._connectionReturnedDefer = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user