1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-08-19 16:42:09 +03:00

Add getSyncToken and setSyncToken to data store interface.

This allows local storage to hold onto the token across page refreshes.
This commit is contained in:
Kegan Dougal
2015-06-26 15:36:53 +01:00
parent 6078100465
commit b7975866fa
5 changed files with 57 additions and 8 deletions

View File

@@ -8,6 +8,8 @@ New features:
New methods: New methods:
* `MatrixScheduler.getQueueForEvent(event)` * `MatrixScheduler.getQueueForEvent(event)`
* `MatrixScheduler.removeEventFromQueue(event)` * `MatrixScheduler.removeEventFromQueue(event)`
* `$DATA_STORE.setSyncToken(token)`
* `$DATA_STORE.getSyncToken()`
Bug fixes: Bug fixes:
* Fixed an issue which prevented RoomMember.name being disambiguated if there * Fixed an issue which prevented RoomMember.name being disambiguated if there

View File

@@ -54,8 +54,6 @@ function MatrixClient(opts) {
return _sendEventHttpRequest(self, eventToSend); return _sendEventHttpRequest(self, eventToSend);
}); });
} }
// track our position in the overall eventstream
this.fromToken = undefined;
this.clientRunning = false; this.clientRunning = false;
var httpOpts = { var httpOpts = {
@@ -986,7 +984,7 @@ MatrixClient.prototype.startClient = function(historyLen) {
// client is already running. // client is already running.
return; return;
} }
if (this.fromToken) { if (this.store.getSyncToken()) {
// resume from where we left off. // resume from where we left off.
_pollForEvents(this); _pollForEvents(this);
return; return;
@@ -1038,7 +1036,7 @@ MatrixClient.prototype.startClient = function(historyLen) {
} }
if (data) { if (data) {
self.fromToken = data.end; self.store.setSyncToken(data.end);
var events = []; var events = [];
for (i = 0; i < data.presence.length; i++) { for (i = 0; i < data.presence.length; i++) {
events.push(new MatrixEvent(data.presence[i])); events.push(new MatrixEvent(data.presence[i]));
@@ -1087,7 +1085,7 @@ function _pollForEvents(client) {
}, 40000); }, 40000);
client._http.authedRequest(undefined, "GET", "/events", { client._http.authedRequest(undefined, "GET", "/events", {
from: client.fromToken, from: client.store.getSyncToken(),
timeout: 30000 timeout: 30000
}).done(function(data) { }).done(function(data) {
if (discardResult) { if (discardResult) {
@@ -1152,7 +1150,7 @@ function _pollForEvents(client) {
}); });
} }
if (data) { if (data) {
self.fromToken = data.end; self.store.setSyncToken(data.end);
utils.forEach(events, function(e) { utils.forEach(events, function(e) {
self.emit("event", e); self.emit("event", e);
}); });

View File

@@ -17,6 +17,22 @@ function LocalStorageStore() {
LocalStorageStore.prototype = { 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. * Store a room in local storage.
* @param {Room} room * @param {Room} room

View File

@@ -16,10 +16,27 @@ module.exports.MatrixInMemoryStore = function MatrixInMemoryStore() {
this.users = { this.users = {
// userId: User // userId: User
}; };
this.syncToken = null;
}; };
module.exports.MatrixInMemoryStore.prototype = { 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. * Store the given room.
* @param {Room} room The room to be stored. All properties must be stored. * @param {Room} room The room to be stored. All properties must be stored.

View File

@@ -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 * @constructor
*/ */
function StubStore() { function StubStore() {
this.fromToken = null;
} }
StubStore.prototype = { 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. * No-op.
* @param {Room} room * @param {Room} room