From a8100d31649de873b6631de2bbe4e5e0a51e31b7 Mon Sep 17 00:00:00 2001 From: Nenad Novakovic Date: Thu, 16 Nov 2023 21:15:16 +0100 Subject: [PATCH 1/2] feat(storage): allow record in watch callback --- src/storage.ts | 8 +++++--- src/types.ts | 5 ++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/storage.ts b/src/storage.ts index d53c755d..f9484116 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -69,13 +69,15 @@ export function createStorage( })); }; - const onChange: WatchCallback = (event, key) => { + const onChange: WatchCallback = (event, resource) => { if (!context.watching) { return; } - key = normalizeKey(key); + resource = normalizeKey( + typeof resource === "string" ? resource : resource.key + ); for (const listener of context.watchListeners) { - listener(event, key); + listener(event, resource); } }; diff --git a/src/types.ts b/src/types.ts index e33a8da5..cfac1408 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,9 @@ export type StorageValue = null | string | number | boolean | object; export type WatchEvent = "update" | "remove"; -export type WatchCallback = (event: WatchEvent, key: string) => any; +export type WatchCallback = ( + event: WatchEvent, + resource: string | Record<"key" | string, any> +) => any; type MaybePromise = T | Promise; From e213fc31073242c931e1d98cf0d99a506569018e Mon Sep 17 00:00:00 2001 From: Nenad Novakovic Date: Thu, 16 Nov 2023 21:29:42 +0100 Subject: [PATCH 2/2] fix: require key as string in watch callback --- src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index cfac1408..1a276371 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,7 +2,7 @@ export type StorageValue = null | string | number | boolean | object; export type WatchEvent = "update" | "remove"; export type WatchCallback = ( event: WatchEvent, - resource: string | Record<"key" | string, any> + resource: string | (Record & { key: string }) ) => any; type MaybePromise = T | Promise;