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

Merge remote-tracking branch 'origin/develop' into dbkr/wasm

This commit is contained in:
David Baker
2018-10-04 11:19:37 +01:00
14 changed files with 262 additions and 47 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';
import Crypto from './crypto';
import { isCryptoAvailable } from './crypto';
@@ -3101,6 +3102,9 @@ MatrixClient.prototype.startClient = async function(opts) {
// shallow-copy the opts dict before modifying and storing it
opts = Object.assign({}, opts);
if (opts.lazyLoadMembers && this.isGuest()) {
opts.lazyLoadMembers = false;
}
if (opts.lazyLoadMembers) {
const supported = await this.doesServerSupportLazyLoading();
if (supported) {
@@ -3111,7 +3115,12 @@ MatrixClient.prototype.startClient = async function(opts) {
opts.lazyLoadMembers = false;
}
}
// need to vape the store when enabling LL and wasn't enabled before
const shouldClear = await this._wasLazyLoadingToggled(opts.lazyLoadMembers);
if (shouldClear) {
const reason = InvalidStoreError.TOGGLED_LAZY_LOADING;
throw new InvalidStoreError(reason, !!opts.lazyLoadMembers);
}
if (opts.lazyLoadMembers && this._crypto) {
this._crypto.enableLazyLoading();
}
@@ -3124,11 +3133,50 @@ MatrixClient.prototype.startClient = async function(opts) {
return this._canResetTimelineCallback(roomId);
};
this._clientOpts = opts;
await this._storeClientOptions(this._clientOpts);
this._syncApi = new SyncApi(this, opts);
this._syncApi.sync();
};
/**
* Is the lazy loading option different than in previous session?
* @param {bool} lazyLoadMembers current options for lazy loading
* @return {bool} whether or not the option has changed compared to the previous session */
MatrixClient.prototype._wasLazyLoadingToggled = 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) {
lazyLoadMembersBefore = !!prevClientOptions.lazyLoadMembers;
}
return lazyLoadMembersBefore !== lazyLoadMembers;
}
return false;
};
/**
* store client options with boolean/string/numeric values
* to know in the next session what flags the sync data was
* created with (e.g. lazy loading)
* @param {object} opts the complete set of client options
* @return {Promise} for store operation */
MatrixClient.prototype._storeClientOptions = function(opts) {
const primTypes = ["boolean", "string", "number"];
const serializableOpts = Object.entries(opts)
.filter(([key, value]) => {
return primTypes.includes(typeof value);
})
.reduce((obj, [key, value]) => {
obj[key] = value;
return obj;
}, {});
return this.store.storeClientOptions(serializableOpts);
};
/**
* High level helper method to stop the client from polling and allow a
* clean shutdown.