diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f6d8b32f..276b4bbee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ New features: New methods: * `MatrixScheduler.getQueueForEvent(event)` * `MatrixScheduler.removeEventFromQueue(event)` + * `$DATA_STORE.setSyncToken(token)` + * `$DATA_STORE.getSyncToken()` Bug fixes: * Fixed an issue which prevented RoomMember.name being disambiguated if there diff --git a/lib/client.js b/lib/client.js index 988b1dc71..2d3b01004 100644 --- a/lib/client.js +++ b/lib/client.js @@ -54,8 +54,6 @@ function MatrixClient(opts) { return _sendEventHttpRequest(self, eventToSend); }); } - // track our position in the overall eventstream - this.fromToken = undefined; this.clientRunning = false; var httpOpts = { @@ -986,7 +984,7 @@ MatrixClient.prototype.startClient = function(historyLen) { // client is already running. return; } - if (this.fromToken) { + if (this.store.getSyncToken()) { // resume from where we left off. _pollForEvents(this); return; @@ -1038,7 +1036,7 @@ MatrixClient.prototype.startClient = function(historyLen) { } if (data) { - self.fromToken = data.end; + self.store.setSyncToken(data.end); var events = []; for (i = 0; i < data.presence.length; i++) { events.push(new MatrixEvent(data.presence[i])); @@ -1087,7 +1085,7 @@ function _pollForEvents(client) { }, 40000); client._http.authedRequest(undefined, "GET", "/events", { - from: client.fromToken, + from: client.store.getSyncToken(), timeout: 30000 }).done(function(data) { if (discardResult) { @@ -1152,7 +1150,7 @@ function _pollForEvents(client) { }); } if (data) { - self.fromToken = data.end; + self.store.setSyncToken(data.end); utils.forEach(events, function(e) { self.emit("event", e); }); diff --git a/lib/store/localstorage.js b/lib/store/localstorage.js index 7c148323c..b138a7180 100644 --- a/lib/store/localstorage.js +++ b/lib/store/localstorage.js @@ -17,6 +17,22 @@ function LocalStorageStore() { LocalStorageStore.prototype = { + /** + * Retrieve the token to stream from. + * @return {string} The token or null. + */ + getSyncToken: function() { + return null; + }, + + /** + * Set the token to stream from. + * @param {string} token The token to stream from. + */ + setSyncToken: function(token) { + + }, + /** * Store a room in local storage. * @param {Room} room diff --git a/lib/store/memory.js b/lib/store/memory.js index 5e7de70df..737c0750a 100644 --- a/lib/store/memory.js +++ b/lib/store/memory.js @@ -16,10 +16,27 @@ module.exports.MatrixInMemoryStore = function MatrixInMemoryStore() { this.users = { // userId: User }; + this.syncToken = null; }; module.exports.MatrixInMemoryStore.prototype = { + /** + * Retrieve the token to stream from. + * @return {string} The token or null. + */ + getSyncToken: function() { + return this.syncToken; + }, + + /** + * Set the token to stream from. + * @param {string} token The token to stream from. + */ + setSyncToken: function(token) { + this.syncToken = token; + }, + /** * Store the given room. * @param {Room} room The room to be stored. All properties must be stored. diff --git a/lib/store/stub.js b/lib/store/stub.js index 86e59939e..b10a02462 100644 --- a/lib/store/stub.js +++ b/lib/store/stub.js @@ -5,15 +5,31 @@ */ /** - * Construct a stub store. This does no-ops on all store methods. + * Construct a stub store. This does no-ops on most store methods. * @constructor */ function StubStore() { - + this.fromToken = null; } StubStore.prototype = { + /** + * Get the sync token. + * @return {string} + */ + getSyncToken: function() { + return this.fromToken; + }, + + /** + * Set the sync token. + * @param {string} token + */ + setSyncToken: function(token) { + this.fromToken = token; + }, + /** * No-op. * @param {Room} room