Skip to content

Commit

Permalink
♻️ Better local support for WebConfig.load
Browse files Browse the repository at this point in the history
  • Loading branch information
richardfrost committed Jul 16, 2024
1 parent e0d7393 commit 02484e4
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions src/script/webConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,51 +160,46 @@ 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<WebConfig> = {}): Promise<WebConfig> {
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);
throw new Error(`Failed to load items: ${prettyPrintArray(keys)}. [${err.message}]`);
}
}

// Returns list of keys to get from storage.sync
static async loadLargeKeysFromLocalStorage(keys: string[], data: Partial<WebConfig>): Promise<string[]> {
// 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<WebConfig>): Promise<string[]> { // 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<WebConfig>;
const retrievedKeys = [];
const localData = await this.getLocalStorage(keys) as Partial<WebConfig>;
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<WebConfig> = {}) {
Expand Down Expand Up @@ -273,8 +268,12 @@ export default class WebConfig extends Config {
});
}

static requestedkeysForLocalStorage(keys: string[], data: Partial<WebConfig>) {
return this._localConfigKeys.filter((localKey) => keys.includes(localKey));
static requestedkeysForLocalStorage(keys: string[], data: Partial<WebConfig>): 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<WebConfig>) {
Expand Down

0 comments on commit 02484e4

Please sign in to comment.