Skip to content

Commit

Permalink
Changes events to provide a set of keys
Browse files Browse the repository at this point in the history
Avoids firing events in a loop
  • Loading branch information
eamodio committed Dec 18, 2024
1 parent c377fef commit 3135d35
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/system/vscode/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export type StorageChangeEvent =
/**
* The key of the stored value that has changed.
*/
readonly key: GlobalStorageKeys;
readonly keys: GlobalStorageKeys[];
readonly workspace: false;
}
| {
/**
* The key of the stored value that has changed.
*/
readonly key: WorkspaceStorageKeys;
readonly keys: WorkspaceStorageKeys[];
readonly workspace: true;
};

Expand Down Expand Up @@ -61,7 +61,7 @@ export class Storage implements Disposable {
@debug({ logThreshold: 250 })
async delete(key: GlobalStorageKeys): Promise<void> {
await this.context.globalState.update(`${extensionPrefix}:${key}`, undefined);
this._onDidChange.fire({ key: key, workspace: false });
this._onDidChange.fire({ keys: [key], workspace: false });
}

@debug({ logThreshold: 250 })
Expand All @@ -72,17 +72,23 @@ export class Storage implements Disposable {
async deleteWithPrefixCore(prefix?: ExtractPrefixes<GlobalStorageKeys, ':'>, exclude?: RegExp): Promise<void> {
const qualifiedKeyPrefix = `${extensionPrefix}:`;

const keys: GlobalStorageKeys[] = [];

for (const qualifiedKey of this.context.globalState.keys() as `${typeof extensionPrefix}:${GlobalStorageKeys}`[]) {
if (!qualifiedKey.startsWith(qualifiedKeyPrefix)) continue;

const key = qualifiedKey.substring(qualifiedKeyPrefix.length) as GlobalStorageKeys;
if (prefix == null || key === prefix || key.startsWith(`${prefix}:`)) {
if (exclude?.test(key)) continue;

keys.push(key);
await this.context.globalState.update(qualifiedKey, undefined);
this._onDidChange.fire({ key: key, workspace: false });
}
}

if (keys.length) {
this._onDidChange.fire({ keys: keys, workspace: false });
}
}

@debug({ logThreshold: 250 })
Expand All @@ -93,7 +99,7 @@ export class Storage implements Disposable {
@debug({ args: { 1: false }, logThreshold: 250 })
async store<T extends keyof GlobalStorage>(key: T, value: GlobalStorage[T] | undefined): Promise<void> {
await this.context.globalState.update(`${extensionPrefix}:${key}`, value);
this._onDidChange.fire({ key: key, workspace: false });
this._onDidChange.fire({ keys: [key], workspace: false });
}

@debug({ args: false, logThreshold: 250 })
Expand Down Expand Up @@ -123,7 +129,7 @@ export class Storage implements Disposable {
@debug({ logThreshold: 250 })
async deleteWorkspace(key: WorkspaceStorageKeys): Promise<void> {
await this.context.workspaceState.update(`${extensionPrefix}:${key}`, undefined);
this._onDidChange.fire({ key: key, workspace: true });
this._onDidChange.fire({ keys: [key], workspace: true });
}

@debug({ logThreshold: 250 })
Expand All @@ -137,17 +143,23 @@ export class Storage implements Disposable {
): Promise<void> {
const qualifiedKeyPrefix = `${extensionPrefix}:`;

const keys: WorkspaceStorageKeys[] = [];

for (const qualifiedKey of this.context.workspaceState.keys() as `${typeof extensionPrefix}:${WorkspaceStorageKeys}`[]) {
if (!qualifiedKey.startsWith(qualifiedKeyPrefix)) continue;

const key = qualifiedKey.substring(qualifiedKeyPrefix.length) as WorkspaceStorageKeys;
if (prefix == null || key === prefix || key.startsWith(`${prefix}:`)) {
if (exclude?.includes(key)) continue;

keys.push(key);
await this.context.workspaceState.update(qualifiedKey, undefined);
this._onDidChange.fire({ key: key, workspace: true });
}
}

if (keys.length) {
this._onDidChange.fire({ keys: keys, workspace: true });
}
}

@debug({ logThreshold: 250 })
Expand All @@ -161,6 +173,6 @@ export class Storage implements Disposable {
value: WorkspaceStorage[T] | undefined,
): Promise<void> {
await this.context.workspaceState.update(`${extensionPrefix}:${key}`, value);
this._onDidChange.fire({ key: key, workspace: true });
this._onDidChange.fire({ keys: [key], workspace: true });
}
}

0 comments on commit 3135d35

Please sign in to comment.