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 3b2fbba55..f2848defb 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 = { @@ -1025,7 +1023,7 @@ function doInitialSync(client, historyLen) { } if (data) { - client.fromToken = data.end; + client.store.setSyncToken(data.end); var events = []; for (i = 0; i < data.presence.length; i++) { events.push(new MatrixEvent(data.presence[i])); @@ -1071,7 +1069,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; @@ -1102,7 +1100,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) { @@ -1167,7 +1165,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/matrix.js b/lib/matrix.js index fbe0bc801..384232f84 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -6,6 +6,8 @@ module.exports.MatrixEvent = require("./models/event").MatrixEvent; module.exports.EventStatus = require("./models/event").EventStatus; /** The {@link module:store/memory.MatrixInMemoryStore|MatrixInMemoryStore} class. */ module.exports.MatrixInMemoryStore = require("./store/memory").MatrixInMemoryStore; +/** The {@link module:store/localstorage.LocalStorageStore|LocalStorageStore} class. */ +module.exports.LocalStorageStore = require("./store/localstorage"); /** The {@link module:http-api.MatrixHttpApi|MatrixHttpApi} class. */ module.exports.MatrixHttpApi = require("./http-api").MatrixHttpApi; /** The {@link module:http-api.MatrixError|MatrixError} class. */ diff --git a/lib/store/localstorage.js b/lib/store/localstorage.js new file mode 100644 index 000000000..b138a7180 --- /dev/null +++ b/lib/store/localstorage.js @@ -0,0 +1,92 @@ +"use strict"; +/** + * This is an internal module. + * @module store/localstorage + */ + +/** + * Construct a localstorage store. + * @constructor + * @throws if the global 'localStorage' does not exist. + */ +function LocalStorageStore() { + if (!global.localStorage) { + throw new Error("localStorage not found."); + } +} + +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 + */ + storeRoom: function(room) { + }, + + /** + * Retrieve a room from local storage. + * @param {string} roomId + * @return {null} + */ + getRoom: function(roomId) { + return null; + }, + + /** + * Get a list of all rooms from local storage. + * @return {Array} An empty array. + */ + getRooms: function() { + return []; + }, + + /** + * Get a list of summaries from local storage. + * @return {Array} An empty array. + */ + getRoomSummaries: function() { + return []; + }, + + /** + * Store a user in local storage. + * @param {User} user + */ + storeUser: function(user) { + }, + + /** + * Get a user from local storage. + * @param {string} userId + * @return {null} + */ + getUser: function(userId) { + return null; + } + + // TODO + //setMaxHistoryPerRoom: function(maxHistory) {}, + + // TODO + //reapOldMessages: function() {}, +}; + +/** Local Storage Store class. */ +module.exports = LocalStorageStore; 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