wxt icon indicating copy to clipboard operation
wxt copied to clipboard

Single watcher for multiple keys

Open avi12 opened this issue 1 year ago • 7 comments

Feature Request

This snippet allows for watching a single item: https://github.com/wxt-dev/wxt/blob/c0aa12089e24c9f0cb01fb3f8546f17096b21341/packages/wxt/src/storage.ts#L434-L447

I also wish to have a watcher setup like this:

const unwatch = storage.watch({
  "local:item1"((newValue, oldValue) => {...}),
  "local:item2"((newValue, oldValue) => {...})
});
unwatch();

Is your feature request related to a bug?

N/A

What are the alternatives?

storage.watch("local:item1", (newValue, oldValue) => {...});
storage.watch("local:item2", (newValue, oldValue) => {...});

Additional context

This functionality is available in Plasmo

avi12 avatar Sep 27 '24 02:09 avi12

We can add something like that!

But for most cases, I would recommend just calling watch multiple times. Seems like a classic case of premature optimization.

aklinker1 avatar Sep 27 '24 13:09 aklinker1

In my case it's not for the sake of premature optimization but rather increased readability and maintainability, having all of the watchers in one place

avi12 avatar Sep 27 '24 13:09 avi12

Since I have a PR open about some storage changes, I'll take this and add that to the watch

Timeraa avatar Sep 28 '24 14:09 Timeraa

@aklinker1 Looks like this has been resolved in #990

krisalcordo avatar Mar 09 '25 02:03 krisalcordo

@krisalcordo What snippet lets me assign a single watcher on multiple items?

avi12 avatar Mar 09 '25 22:03 avi12

@Timeraa can correct me but from looking at the watch function it looks like you can do this?

watch({
  key1: (newValue, oldValue) => console.log('key1 changed:', oldValue, '→', newValue),
  key2: (newValue, oldValue) => console.log('key2 changed:', oldValue, '→', newValue),
});

krisalcordo avatar Mar 10 '25 03:03 krisalcordo

Let me know if that works @avi12, I'll work on getting the docs updated too.

krisalcordo avatar Mar 10 '25 03:03 krisalcordo