Open ef4 opened 3 years ago
I've wanted to add a storage
listener to the LocalStorageSource
for some time. In this way, the source could just transform browser storage
events into transform
events. You could then use a simple SyncStrategy
from your local
source back to your store
to keep them in sync.
The implementation of the storage
listener would look quite similar to what you have. However, instead of inspecting what's already in the store
, it could look at the newValue
and oldValue
properties on the storage event to decipher what change occurred.
I think the one catch here, which you mention, is that you may wish to maintain transform IDs across browser tabs. I think this could be achieved by storing IDs as some hidden metadata (e.g. __transform_id__
) inside each record. But it gets tricky because you'd need to wait a "tick" to collect all the changes per transform 🤔
You could then use a simple SyncStrategy from your local source back to your store to keep them in sync.
Yeah, that is the first thing I tried.
the source could just transform browser storage events into transform events
Ah, this is what I was missing. That seems like a good simplification, even without also addressing the question of stable transform ids.
I have an
@orbit/local-storage
source and I wrote a strategy for keeping this source updated in response to changes made by other browser tabs (which fires the 'storage' event onwindow
). This is working, but my question is whether there is a better way to do this that's less manual and that preserves operation IDs instead of creating new ones.Here is the relevant bit that we run each time we hear the event about a record:
It seems quite manual and I suspect I'm missing a better solution.
The full strategy: