diff --git a/src/script/webConfig.ts b/src/script/webConfig.ts index fc2daa6d..0015e4d8 100644 --- a/src/script/webConfig.ts +++ b/src/script/webConfig.ts @@ -160,18 +160,17 @@ export default class WebConfig extends Config { // keys: Requested keys (defaults to all) // Note: syncLargeKeys will be returned when required static async load(keys: string | string[] = [], data: Partial = {}): Promise { - let syncKeys; keys = this.keysToLoad(keys); try { - if (this.includesLargeKeys(keys)) { - syncKeys = await this.loadLargeKeysFromLocalStorage(keys, data); - } else { - // Get all keys from sync storage (except syncLargeKeys) - syncKeys = this.requestedkeysForSyncStorage(keys, 'syncLargeKeys', data); - } + // Local storage + const localKeys = this.requestedkeysForLocalStorage(keys, data); + const appliedLocalKeys = await this.loadKeysFromLocalStorage(localKeys, data); + // Sync storage + const syncKeys = this.requestedkeysForSyncStorage(keys, appliedLocalKeys, data); await this.loadFromSyncStorage(syncKeys, data); + return new this(data); } catch (err) { logger.error('Failed to load items.', keys, err); @@ -179,32 +178,28 @@ export default class WebConfig extends Config { } } - // Returns list of keys to get from storage.sync - static async loadLargeKeysFromLocalStorage(keys: string[], data: Partial): Promise { - // Add syncLargeKeys if any largeKeys were requested - if (!keys.includes('syncLargeKeys')) keys.push('syncLargeKeys'); + // Returns list of keys applied to data + static async loadKeysFromLocalStorage(keys: string[], data: Partial): Promise { // Rename + if (!keys.length) return []; // Load large keys from LocalStorage if necessary - const localKeys = this.requestedkeysForLocalStorage(keys, data); - const localData = await this.getLocalStorage(localKeys) as Partial; + const retrievedKeys = []; + const localData = await this.getLocalStorage(keys) as Partial; data.syncLargeKeys = localData.syncLargeKeys === false ? localData.syncLargeKeys : this._defaults.syncLargeKeys; - if (data.syncLargeKeys === false) { // Use local storage for large keys - // Add large keys from local storage to data - for (const localKey of localKeys) { - if (localData.hasOwnProperty(localKey)) { - data[localKey] = localData[localKey]; - } else { - this.assignDefaultValue(localKey, data); - } - } + for (const key of keys) { + if (key === 'syncLargeKeys') continue; + if (data.syncLargeKeys && this._largeKeys.includes(key)) continue; - // Get all keys from sync storage except the ones found in local storage - return this.requestedkeysForSyncStorage(keys, this._localConfigKeys, data); - } else { // Use sync storage for large keys - // Get all keys from sync storage (except syncLargeKeys) - return this.requestedkeysForSyncStorage(keys, 'syncLargeKeys', data); + if (localData.hasOwnProperty(key)) { + data[key] = localData[key]; + } else { + this.assignDefaultValue(key, data); + } + retrievedKeys.push(key); } + + return retrievedKeys; } static async loadFromSyncStorage(syncKeys: string[], data: Partial = {}) { @@ -273,8 +268,12 @@ export default class WebConfig extends Config { }); } - static requestedkeysForLocalStorage(keys: string[], data: Partial) { - return this._localConfigKeys.filter((localKey) => keys.includes(localKey)); + static requestedkeysForLocalStorage(keys: string[], data: Partial): string[] { + const largeKeys = this._largeKeys.filter((key) => keys.includes(key)); + const localOnlyKeys = this._localOnlyKeys.filter((key) => keys.includes(key)); + const localKeys = largeKeys.concat(localOnlyKeys); + if (largeKeys.length && !keys.includes('syncLargeKeys')) localKeys.push('syncLargeKeys'); + return localKeys; } static requestedkeysForSyncStorage(keys: string[], toRemove: string | string[], data: Partial) {