zendesk / cross-storage

Cross domain local storage, with permissions
Apache License 2.0
2.22k stars 180 forks source link

Add localstorage event handler #32

Closed alvinlindstam closed 7 years ago

alvinlindstam commented 8 years ago

We needed to be updated when another window changed a value, so this PR implements forwarding storage events from the hub to the client.

Resolves initial request in https://github.com/zendesk/cross-storage/issues/19. It tries to behave like the storage event as much as possible, rather than implementing own event for different types of requests. This also means that no events will be sent from hubs that actually make the changes.

Feedback appreciated, I'm not sure if I've followed all conventions of the library or if the API is the best available.

alvinlindstam commented 8 years ago

Looking at the travis log, it seems that there is some credential issue preventing the tests from running rather than test failures in this branch.

danielstjules commented 7 years ago

Thanks again! Will try to look at the PR this week :)

alvinlindstam commented 7 years ago

Have you had a chance to look at this, @danielstjules?

JasonLi914 commented 7 years ago

I would also like to see this or a similar feature if possible. Thank you for your time!

danielstjules commented 7 years ago

Unlike localStorage events, it looks like these would fire to all clients, including the source of the change

https://html.spec.whatwg.org/multipage/webstorage.html#the-localstorage-attribute

When the setItem(), removeItem(), and clear() methods are called on a Storage object x that is associated with a local storage area, if the methods did not throw an exception or "do nothing" as defined above, then for every Document object whose Window object's localStorage attribute's Storage object is associated with the same storage area, other than x, send a storage notification.
alvinlindstam commented 7 years ago

@danielstjules: The Hub uses the localStorage event, so the client that initiates the change will not get an event since the hub does not. Also, if multiple clients somehow use the same hub window, they won't get any updates when the other clients update the data.

danielstjules commented 7 years ago

Since this was a pretty old PR, I didn't want to bother you with changes. I've squashed it and am taking it over in another branch. Thank you so much! :)