Skip to content
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

Syncing localStorage across instances #19

Open
bedeoverend opened this issue Sep 11, 2015 · 3 comments
Open

Syncing localStorage across instances #19

bedeoverend opened this issue Sep 11, 2015 · 3 comments

Comments

@bedeoverend
Copy link

Are there any plans to have this sync changes to localStorage that occur on the same page, is this already implemented? Or at least for changes made through other instances of iron-localstorage - similar to iron-meta? I have multiple instances of iron-localstorage referencing the same key, and seems - at least for me - like a common use case to want them all to update when any one of them update.

@atotic
Copy link
Contributor

atotic commented Sep 11, 2015

We've decided not to do it in order to keep the code clean, it was just a wrapper for localstorage API.

There are no current plans to extend it. I see the need, so this might change. We've created many elements with global state.

@43081j
Copy link

43081j commented Jun 9, 2016

For anyone wondering why this is the case, iron-localstorage actually does implement this correctly. It is browser behaviour/implementation which prevents this.

See here.

When you call setItem (which iron-localstorage does), the storage event will fire only if window is a different window to the one in which setItem was called (so iron-localstorage is never notified).

@bamartin-ua
Copy link

For those looking for a work-around, any time you change value, you can call window.setTimeout(e => window.dispatchEvent(new StorageEvent('storage',{key:<<name here>>,newValue:<<value here>>}))).
this is a setTimeout so it obviously creates a race condition, but it works for the general case.
I think localstorage actually ignores the newValue but that could change in the future?

I also played around with the iron-localstorage code, and adding the appropriate dispatchEvent call in the save() function works great, but I could see some app expecting that storage events wouldn't fire on-page, given that's kindof in the spec. perhaps a better solution would be to have iron-localstorage fire and listen for a custom ls-storage event or somesuch?

johnlim added a commit to johnlim/auth0-element that referenced this issue Jul 14, 2017
…nchrnous mainly to reduce complexity as well as avoiding issues related to iron-localstorage where events are not being triggered as expected. See PolymerElements/iron-localstorage#19.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants