svelte-persistent-store icon indicating copy to clipboard operation
svelte-persistent-store copied to clipboard

Implement workaround for infinite recursion with the chrome storage change listener

Open ckiee opened this issue 6 months ago • 3 comments

Fixes #64, albeit in a rather fragile manner. Ideally we'd be able to pass another argument through the Svelte store API or have the concept of a thread local variable but we have neither as things are dispatched over multiple JS ticks

ckiee avatar Jun 28 '25 14:06 ckiee

Converting to draft as it doesn't work if you make multiple updates in a tick

ckiee avatar Jun 28 '25 15:06 ckiee

I think it would be better to add a control to prevent saving the same data again and again.

It can be done on the storage itself or in the persist function.

But we have to watch for the performance, I fear that reading the storage every time can be a bit costly. (and we need to read the storage not the store, because the storage can be changed outside the application)

MacFJA avatar Jun 29 '25 20:06 MacFJA

I ended up abandoning the current patch because if you have this setup it loops when there is more than one edit per tick:

  • tick 0
    • set store.a = 5
    • set store.b = 2
  • tick 1
    • event onChanged a=5
    • => store a=5, infinite loop.
    • event onChanged b=2
    • => noop

I realized all I need is one way updates so I use the extension messaging now from the options page to the background script, this could be added to this library too by making the argument type boolean | "push" | "pull".

https://github.com/ckiee/distract-o-window/blob/9aa101699b37f28c72d7d7028ea1753ed899d31a/src/background/main.ts#L7-L11

https://github.com/ckiee/distract-o-window/blob/9aa101699b37f28c72d7d7028ea1753ed899d31a/src/options/main.ts#L9-L13

ckiee avatar Jul 02 '25 07:07 ckiee