Implement workaround for infinite recursion with the chrome storage change listener
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
Converting to draft as it doesn't work if you make multiple updates in a tick
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)
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