diff --git a/spec/unit/matrix-client.spec.js b/spec/unit/matrix-client.spec.js index a32ee2693..422dc995b 100644 --- a/spec/unit/matrix-client.spec.js +++ b/spec/unit/matrix-client.spec.js @@ -134,6 +134,7 @@ describe("MatrixClient", function() { "getRoom", "getRooms", "getUser", "getSyncToken", "scrollback", "save", "setSyncToken", "storeEvents", "storeRoom", "storeUser", "getFilterIdByName", "setFilterIdByName", "getFilter", "storeFilter", + "getSyncAccumulator", ].reduce((r, k) => { r[k] = expect.createSpy(); return r; }, {}); client = new MatrixClient({ baseUrl: "https://my.home.server", diff --git a/spec/unit/sync-accumulator.spec.js b/spec/unit/sync-accumulator.spec.js index a7864ca50..b5ca1691c 100644 --- a/spec/unit/sync-accumulator.spec.js +++ b/spec/unit/sync-accumulator.spec.js @@ -1,11 +1,27 @@ +/* +Copyright 2017 Vector Creations Ltd + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + "use strict"; import 'source-map-support/register'; -const sdk = require("../.."); -const SyncAccumulator = sdk.SyncAccumulator; -const utils = require("../test-utils"); - +import utils from "../test-utils"; +import sdk from "../.."; import expect from 'expect'; +const SyncAccumulator = sdk.SyncAccumulator; + describe("SyncAccumulator", function() { let sa; diff --git a/src/client.js b/src/client.js index e7207e22e..e459209da 100644 --- a/src/client.js +++ b/src/client.js @@ -38,7 +38,6 @@ const Filter = require("./filter"); const SyncApi = require("./sync"); const MatrixBaseApis = require("./base-apis"); const MatrixError = httpApi.MatrixError; -const IndexedDBStore = require("./store/indexeddb").IndexedDBStore; const SCROLLBACK_DELAY_MS = 3000; let CRYPTO_ENABLED = false; @@ -165,10 +164,7 @@ function MatrixClient(opts) { this.olmVersion = Crypto.getOlmVersion(); } - // Set up a sync accumulator if we can persist room data - if (this.store instanceof IndexedDBStore) { - this._syncAccumulator = this.store.getSyncAccumulator(); - } + this._syncAccumulator = this.store.getSyncAccumulator(); } utils.inherits(MatrixClient, EventEmitter); utils.extend(MatrixClient.prototype, MatrixBaseApis.prototype); diff --git a/src/store/memory.js b/src/store/memory.js index b14b25f7d..d356369ed 100644 --- a/src/store/memory.js +++ b/src/store/memory.js @@ -1,5 +1,6 @@ /* Copyright 2015, 2016 OpenMarket Ltd +Copyright 2017 Vector Creations Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -279,4 +280,12 @@ module.exports.MatrixInMemoryStore.prototype = { * Save does nothing as there is no backing data store. */ save: function() {}, + + /** + * Returns nothing as this store does not accumulate /sync data. + * @return {?SyncAccumulator} null + */ + getSyncAccumulator: function() { + return null; + }, }; diff --git a/src/store/stub.js b/src/store/stub.js index ed9da2ef3..539577f42 100644 --- a/src/store/stub.js +++ b/src/store/stub.js @@ -1,5 +1,6 @@ /* Copyright 2015, 2016 OpenMarket Ltd +Copyright 2017 Vector Creations Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -184,6 +185,14 @@ StubStore.prototype = { * Save does nothing as there is no backing data store. */ save: function() {}, + + /** + * Returns nothing as this store does not accumulate /sync data. + * @return {?SyncAccumulator} null + */ + getSyncAccumulator: function() { + return null; + }, }; /** Stub Store class. */ diff --git a/src/sync-accumulator.js b/src/sync-accumulator.js index ed071c9ff..1a4465555 100644 --- a/src/sync-accumulator.js +++ b/src/sync-accumulator.js @@ -13,7 +13,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -"use strict"; /** * This is an internal module. See {@link SyncAccumulator} for the public class. diff --git a/src/sync.js b/src/sync.js index 0b4c5b578..ccf02b026 100644 --- a/src/sync.js +++ b/src/sync.js @@ -1,5 +1,6 @@ /* Copyright 2015, 2016 OpenMarket Ltd +Copyright 2017 Vector Creations Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -508,9 +509,6 @@ SyncApi.prototype._sync = function(syncOptions) { qps.timeout = 0; } - // normal timeout= plus buffer time - const clientSideTimeoutMs = this.opts.pollTimeout + BUFFER_PERIOD_MS; - let isCachedResponse = false; if (self.opts.syncAccumulator && !syncOptions.hasSyncedBefore) { let data = self.opts.syncAccumulator.getJSON(); @@ -520,7 +518,7 @@ SyncApi.prototype._sync = function(syncOptions) { debuglog("sync(): not doing HTTP hit, instead returning stored /sync data"); // We must deep copy the stored data so that the /sync processing code doesn't // corrupt the internal state of the sync accumulator (it adds non-clonable keys) - data = JSON.parse(JSON.stringify(data)); + data = utils.deepCopy(data); this._currentSyncRequest = q.resolve({ next_batch: data.nextBatch, rooms: data.roomsData, @@ -529,6 +527,9 @@ SyncApi.prototype._sync = function(syncOptions) { } } + // normal timeout= plus buffer time + const clientSideTimeoutMs = this.opts.pollTimeout + BUFFER_PERIOD_MS; + if (!isCachedResponse) { debuglog('Starting sync since=' + syncToken); this._currentSyncRequest = client._http.authedRequest(