Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add storage.clear #1368

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/storage/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,21 @@ describe('Storage Utils', () => {
});
});

describe('clear', () => {
it('should remove all items', async () => {
const item1 = storage.defineItem(`${storageArea}:one`);
const item2 = storage.defineItem(`${storageArea}:two`);
await fakeBrowser.storage[storageArea].set({
one: 1,
two: 2,
});

await storage.clear(storageArea);
expect(await item1.getValue()).toBeNull();
expect(await item2.getValue()).toBeNull();
chengxilo marked this conversation as resolved.
Show resolved Hide resolved
});
});

describe('removeMeta', () => {
it('should remove all metadata', async () => {
await fakeBrowser.storage[storageArea].set({ count$: { v: 4 } });
Expand Down
14 changes: 14 additions & 0 deletions packages/storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ function createStorage(): WxtStorage {
}),
);
},
clear: async (base) => {
const driver = getDriver(base);
await driver.clear();
},
removeMeta: async (key, properties) => {
const { driver, driverKey } = resolveKey(key);
await removeMeta(driver, driverKey, properties);
Expand Down Expand Up @@ -534,6 +538,9 @@ function createDriver(storageArea: StorageArea): WxtStorageDriver {
removeItems: async (keys) => {
await getStorageArea().remove(keys);
},
clear: async () => {
await getStorageArea().clear();
},
snapshot: async () => {
return await getStorageArea().get();
},
Expand Down Expand Up @@ -674,6 +681,12 @@ export interface WxtStorage {
| { item: WxtStorageItem<any, any>; options?: RemoveItemOptions }
>,
): Promise<void>;

/**
* Removes all items from storage.
chengxilo marked this conversation as resolved.
Show resolved Hide resolved
*/
clear(base: StorageArea): Promise<void>;

/**
* Remove the entire metadata for a key, or specific properties by name.
*
Expand Down Expand Up @@ -730,6 +743,7 @@ interface WxtStorageDriver {
setItems(values: Array<{ key: string; value: any }>): Promise<void>;
removeItem(key: string): Promise<void>;
removeItems(keys: string[]): Promise<void>;
clear(): Promise<void>;
snapshot(): Promise<Record<string, unknown>>;
restoreSnapshot(data: Record<string, unknown>): Promise<void>;
watch<T>(key: string, cb: WatchCallback<T | null>): Unwatch;
Expand Down