1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-28 05:03:59 +03:00

clear sync data on toggling LL,also throw spec. error and delegate clear

the sync data needs to be cleared toggling in both directions:
 not LL -> LL: you want to get rid of all the excess state in the
               sync data to get the RAM benefits
 LL -> not LL: you want to fill the sync data state again
               because getOutOfBandMembers won't be called
This commit is contained in:
Bruno Windels
2018-09-26 12:39:56 +01:00
parent 58e3c72446
commit 54bff81470
3 changed files with 38 additions and 13 deletions

View File

@@ -44,6 +44,7 @@ const ContentHelpers = require("./content-helpers");
import ReEmitter from './ReEmitter'; import ReEmitter from './ReEmitter';
import RoomList from './crypto/RoomList'; import RoomList from './crypto/RoomList';
import {InvalidStoreError} from './errors';
const LAZY_LOADING_MESSAGES_FILTER = { const LAZY_LOADING_MESSAGES_FILTER = {
@@ -3116,12 +3117,10 @@ MatrixClient.prototype.startClient = async function(opts) {
} }
} }
// need to vape the store when enabling LL and wasn't enabled before // need to vape the store when enabling LL and wasn't enabled before
if (opts.lazyLoadMembers) { const shouldClear = await this._shouldClearSyncDataIfLLToggled(opts.lazyLoadMembers);
const shouldClear = await this._shouldClearSyncDataIfLL(); if (shouldClear) {
if (shouldClear) { const reason = InvalidStoreError.TOGGLED_LAZY_LOADING;
await this.store.deleteAllData(); throw new InvalidStoreError(reason);
throw new Error("vaped the store, you need to resync");
}
} }
if (opts.lazyLoadMembers && this._crypto) { if (opts.lazyLoadMembers && this._crypto) {
this._crypto.enableLazyLoading(); this._crypto.enableLazyLoading();
@@ -3140,18 +3139,19 @@ MatrixClient.prototype.startClient = async function(opts) {
this._syncApi.sync(); this._syncApi.sync();
}; };
/** @return {bool} need to clear the store when enabling LL and wasn't enabled before? */ /** @return {bool} need to clear the store when toggling LL compared to previous session? */
MatrixClient.prototype._shouldClearSyncDataIfLL = async function() { MatrixClient.prototype._shouldClearSyncDataIfLLToggled = async function(lazyLoadMembers) {
let hadLLEnabledBefore = false; lazyLoadMembers = !!lazyLoadMembers;
// assume it was turned off before
// if we don't know any better
let lazyLoadMembersBefore = false;
const isStoreNewlyCreated = await this.store.isNewlyCreated(); const isStoreNewlyCreated = await this.store.isNewlyCreated();
if (!isStoreNewlyCreated) { if (!isStoreNewlyCreated) {
const prevClientOptions = await this.store.getClientOptions(); const prevClientOptions = await this.store.getClientOptions();
if (prevClientOptions) { if (prevClientOptions) {
hadLLEnabledBefore = !!prevClientOptions.lazyLoadMembers; lazyLoadMembersBefore = !!prevClientOptions.lazyLoadMembers;
}
if (!hadLLEnabledBefore) {
return true;
} }
return lazyLoadMembersBefore !== lazyLoadMembers;
} }
return false; return false;
}; };

23
src/errors.js Normal file
View File

@@ -0,0 +1,23 @@
// can't just do InvalidStoreError extends Error
// because of http://babeljs.io/docs/usage/caveats/#classes
function InvalidStoreError(reason) {
const message = `Store is invalid because ${reason}, please delete all data and retry`;
const instance = Reflect.construct(Error, [message]);
Reflect.setPrototypeOf(instance, Reflect.getPrototypeOf(this));
instance.reason = reason;
return instance;
}
InvalidStoreError.TOGGLED_LAZY_LOADING = "TOGGLED_LAZY_LOADING";
InvalidStoreError.prototype = Object.create(Error.prototype, {
constructor: {
value: Error,
enumerable: false,
writable: true,
configurable: true
}
});
Reflect.setPrototypeOf(InvalidStoreError, Error);
module.exports.InvalidStoreError = InvalidStoreError;

View File

@@ -34,6 +34,8 @@ module.exports.SyncAccumulator = require("./sync-accumulator");
module.exports.MatrixHttpApi = require("./http-api").MatrixHttpApi; module.exports.MatrixHttpApi = require("./http-api").MatrixHttpApi;
/** The {@link module:http-api.MatrixError|MatrixError} class. */ /** The {@link module:http-api.MatrixError|MatrixError} class. */
module.exports.MatrixError = require("./http-api").MatrixError; module.exports.MatrixError = require("./http-api").MatrixError;
/** The {@link module:errors.InvalidStoreError|InvalidStoreError} class. */
module.exports.InvalidStoreError = require("./errors").InvalidStoreError;
/** The {@link module:client.MatrixClient|MatrixClient} class. */ /** The {@link module:client.MatrixClient|MatrixClient} class. */
module.exports.MatrixClient = require("./client").MatrixClient; module.exports.MatrixClient = require("./client").MatrixClient;
/** The {@link module:models/room|Room} class. */ /** The {@link module:models/room|Room} class. */