1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +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 RoomList from './crypto/RoomList';
import {InvalidStoreError} from './errors';
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
if (opts.lazyLoadMembers) {
const shouldClear = await this._shouldClearSyncDataIfLL();
if (shouldClear) {
await this.store.deleteAllData();
throw new Error("vaped the store, you need to resync");
}
const shouldClear = await this._shouldClearSyncDataIfLLToggled(opts.lazyLoadMembers);
if (shouldClear) {
const reason = InvalidStoreError.TOGGLED_LAZY_LOADING;
throw new InvalidStoreError(reason);
}
if (opts.lazyLoadMembers && this._crypto) {
this._crypto.enableLazyLoading();
@@ -3140,18 +3139,19 @@ MatrixClient.prototype.startClient = async function(opts) {
this._syncApi.sync();
};
/** @return {bool} need to clear the store when enabling LL and wasn't enabled before? */
MatrixClient.prototype._shouldClearSyncDataIfLL = async function() {
let hadLLEnabledBefore = false;
/** @return {bool} need to clear the store when toggling LL compared to previous session? */
MatrixClient.prototype._shouldClearSyncDataIfLLToggled = async function(lazyLoadMembers) {
lazyLoadMembers = !!lazyLoadMembers;
// assume it was turned off before
// if we don't know any better
let lazyLoadMembersBefore = false;
const isStoreNewlyCreated = await this.store.isNewlyCreated();
if (!isStoreNewlyCreated) {
const prevClientOptions = await this.store.getClientOptions();
if (prevClientOptions) {
hadLLEnabledBefore = !!prevClientOptions.lazyLoadMembers;
}
if (!hadLLEnabledBefore) {
return true;
lazyLoadMembersBefore = !!prevClientOptions.lazyLoadMembers;
}
return lazyLoadMembersBefore !== lazyLoadMembers;
}
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;
/** The {@link module:http-api.MatrixError|MatrixError} class. */
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. */
module.exports.MatrixClient = require("./client").MatrixClient;
/** The {@link module:models/room|Room} class. */