1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

Separate the actual processing of the sync response from the loop doing the requests.

This commit is contained in:
David Baker
2016-01-29 11:05:32 +00:00
parent a221674680
commit 5297855ad3

View File

@@ -426,6 +426,59 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
// - The isBrandNewRoom boilerplate is boilerplatey. // - The isBrandNewRoom boilerplate is boilerplatey.
try { try {
self._processSyncResponse(data);
}
catch (e) {
console.error("Caught /sync error:");
console.error(e);
}
// emit synced events
if (!syncOptions.hasSyncedBefore) {
updateSyncState(client, "PREPARED");
syncOptions.hasSyncedBefore = true;
}
// keep emitting SYNCING -> SYNCING for clients who want to do bulk updates
updateSyncState(client, "SYNCING");
self._sync(syncOptions);
}, function(err) {
if (!self._syncConnectionLost) {
debuglog("Starting keep-alive");
self._syncConnectionLost = true;
retryPromise(self._pokeKeepAlive.bind(self), 2000).done(function() {
debuglog("Keep-alive successful.");
// blow away the current /sync request if the connection is still
// dead. It may be black-holed.
if (!self._syncConnectionLost) {
return;
}
if (self._currentSyncRequest.abort) {
// kill the current sync request
debuglog("Aborting current /sync.");
self._currentSyncRequest.abort();
}
// immediately retry if we were waiting
debuglog(
"Interrupted /sync backoff: %s", self.client.retryImmediately()
);
});
}
console.error("/sync error (%s attempts): %s", attempt, err);
console.error(err);
attempt += 1;
startSyncingRetryTimer(client, attempt, function(newAttempt) {
self._sync(syncOptions, newAttempt);
});
updateSyncState(client, "ERROR", { error: err });
});
};
SyncApi.prototype._processSyncResponse = function(data) {
var client = this.client;
var self = this;
// handle presence events (User objects) // handle presence events (User objects)
if (data.presence && utils.isArray(data.presence.events)) { if (data.presence && utils.isArray(data.presence.events)) {
data.presence.events.map(client.getEventMapper()).forEach( data.presence.events.map(client.getEventMapper()).forEach(
@@ -452,13 +505,13 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
if (data.rooms) { if (data.rooms) {
if (data.rooms.invite) { if (data.rooms.invite) {
inviteRooms = self._mapSyncResponseToRoomArray(data.rooms.invite); inviteRooms = this._mapSyncResponseToRoomArray(data.rooms.invite);
} }
if (data.rooms.join) { if (data.rooms.join) {
joinRooms = self._mapSyncResponseToRoomArray(data.rooms.join); joinRooms = this._mapSyncResponseToRoomArray(data.rooms.join);
} }
if (data.rooms.leave) { if (data.rooms.leave) {
leaveRooms = self._mapSyncResponseToRoomArray(data.rooms.leave); leaveRooms = this._mapSyncResponseToRoomArray(data.rooms.leave);
} }
} }
@@ -542,52 +595,6 @@ SyncApi.prototype._sync = function(syncOptions, attempt) {
room.addEvents(timelineEvents); room.addEvents(timelineEvents);
timelineEvents.forEach(function(e) { client.emit("event", e); }); timelineEvents.forEach(function(e) { client.emit("event", e); });
}); });
}
catch (e) {
console.error("Caught /sync error:");
console.error(e);
}
// emit synced events
if (!syncOptions.hasSyncedBefore) {
updateSyncState(client, "PREPARED");
syncOptions.hasSyncedBefore = true;
}
// keep emitting SYNCING -> SYNCING for clients who want to do bulk updates
updateSyncState(client, "SYNCING");
self._sync(syncOptions);
}, function(err) {
if (!self._syncConnectionLost) {
debuglog("Starting keep-alive");
self._syncConnectionLost = true;
retryPromise(self._pokeKeepAlive.bind(self), 2000).done(function() {
debuglog("Keep-alive successful.");
// blow away the current /sync request if the connection is still
// dead. It may be black-holed.
if (!self._syncConnectionLost) {
return;
}
if (self._currentSyncRequest.abort) {
// kill the current sync request
debuglog("Aborting current /sync.");
self._currentSyncRequest.abort();
}
// immediately retry if we were waiting
debuglog(
"Interrupted /sync backoff: %s", self.client.retryImmediately()
);
});
}
console.error("/sync error (%s attempts): %s", attempt, err);
console.error(err);
attempt += 1;
startSyncingRetryTimer(client, attempt, function(newAttempt) {
self._sync(syncOptions, newAttempt);
});
updateSyncState(client, "ERROR", { error: err });
});
}; };
/** /**