From a3a59f4456f3f61dd61ded4d232285a0bdf8067b Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 26 Jun 2015 15:13:04 +0100 Subject: [PATCH 1/3] Add localstorage store module. --- lib/store/localstorage.js | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/store/localstorage.js diff --git a/lib/store/localstorage.js b/lib/store/localstorage.js new file mode 100644 index 000000000..2efe0202e --- /dev/null +++ b/lib/store/localstorage.js @@ -0,0 +1,73 @@ +"use strict"; +/** + * This is an internal module. + * @module store/localstorage + */ + +/** + * Construct a localstorage store. + * @constructor + */ +function LocalStorageStore() { + +} + +LocalStorageStore.prototype = { + + /** + * 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; From 6078100465a1af1cba7d3f0b9383c81a9a4cb6e1 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 26 Jun 2015 15:24:55 +0100 Subject: [PATCH 2/3] Export LocalStorageStore class. Throw if 'localStorage' isn't defined. --- lib/matrix.js | 2 ++ lib/store/localstorage.js | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) 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 index 2efe0202e..7c148323c 100644 --- a/lib/store/localstorage.js +++ b/lib/store/localstorage.js @@ -7,9 +7,12 @@ /** * 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 = { From b7975866fa8fadf5c74f4fb5ef2c7213aadd7dfc Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 26 Jun 2015 15:36:53 +0100 Subject: [PATCH 3/3] Add getSyncToken and setSyncToken to data store interface. This allows local storage to hold onto the token across page refreshes. --- CHANGELOG.md | 2 ++ lib/client.js | 10 ++++------ lib/store/localstorage.js | 16 ++++++++++++++++ lib/store/memory.js | 17 +++++++++++++++++ lib/store/stub.js | 20 ++++++++++++++++++-- 5 files changed, 57 insertions(+), 8 deletions(-) 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