New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(useStorage): emit a custom event to support custom storage areas #2630
Conversation
anyone i can nudge to move this forward? happy to rework anything if needed, as long as we can fix it |
This PR hasn't fixed these issues yet and calling IMO, the core problem is that after executing |
@GODLiangCY no worries, i hadn't seen that related issue at all so thanks for pointing that out confident the fix in this pr was needed, but does seem like there's still a bug (introduced by this or not). i did try debug this and got a little tangled up in the watcher logic. will take another look soon and see if i can figure it out since the original event changes, it never worked with non-native stores like pinia. so i suspect whatever is broken now was also broken before this PR's change (and introduced by the original event listener change). |
to be clear it also seems the issue you linked has a repro with a version before this particular PR landed (so the extra this PR seems to be shipped in 9.13.0 while the referenced issue is using 9.8.2 so it does indeed look like its a bug that was in the original event changes, before this PR. will still try figure it out though |
Apologize for my carelessness!! I agree with you that this pr is needed and solves problems you mentioned in desciptions. 800f74 commit do not work well with pinia's store, maybe something like |
i think i roughly understand it now if you have a watcher on
the instance which fired the event will also catch its own event, leading to setting it twice. though i'd hope that'd be fine since you would be setting the value to what it already is, i.e. noop. the issue you mentioned before, too, could be a separate thing. hes mutating a key within an object in a store. doing that will dispatch an event to tell other instances the overall object changed, so any watcher watching anything inside that object will fire i imagine. that probably explains the behaviour they saw no solution off the top of my head (yet) but at least we probably now understand the two issues. |
Description
Fixes #2605
Fixes #2567
Basically when you introduced a fix to update instances on the same window using the same store (800f74f), you did this by dispatching a
StorageEvent
(such that the already existent listener would catch it and update the value as needed).This caused a breaking change in that things like pinia stores would no longer work with it.
The reason for that is that you can only construct a
StorageEvent
if the storage area is built-in pretty much (e.g. local storage, session storage, etc). You definitely cannot construct one with a custom storage area such as pinia (anything that conforms to yourStorageLike
interface).All of the
useStorage
tests were throwing an exception because of this, which was being eaten up by theonError
handler (which the tests don't assert wasn't called, probably a good improvement in another PR to check that).To fix, you could just fake a
storage
event (i.e. fire aCustomEvent
with the name'storage'
). However, that will confuse any third party code elsewhere which relies on thestorage
event being an actualStorageEvent
.Even if you somehow create your own event with the same interface (so it can't be a
CustomEvent
, since that'd nest your values indetail
), you'd then be breaking the fact that storage events are only meant to fire when another window mutated the store.Because of all of the above, i opted here to create a custom event specific to vueuse which we listen for. That way we can safely dispatch it without affecting any third-party code and can do so with any
StorageLike
area.What is the purpose of this pull request?
Before submitting the PR, please make sure you do the following
fixes #123
).