question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add hook for storage event

See original GitHub issue

Would it be possible to add a way to hook into the HTML5 storage event with this library?

Demo of the functionality I’m looking for (but doesn’t use store.js, obviously).

I’m thinking something like:

// All Windows / Tabs:
store.on('keyName', function(s){
    console.log(s.newValue);
});

// Window / Tab 1:
store.set('keyName', 'testValue');

// All other Windows / Tabs:
// 'testValue' is logged.

I’m implementing my own solution for this with the following code:

// All Windows / Tabs:
// Hook into the storage event
$(window).on('storage', function(e){
    // Keys we actually care about.
    var keys = ['timeStamp', 'key', 'oldValue', 'newValue', 'url'], newEvent = {};
    e = e.originalEvent; // jQuery puts the original event here
    for (var i in keys) {
        i = keys[i];
        if (i.substr(3) === 'Value') {
            // Get the value back just like we put it into store.set()
            newEvent[i] = store.deserialize(e[i]);
        } else {
            newEvent[i] = e[i];
        }
    }

    // Send the event to global listeners that want to know about all storage changes.
    newEvent.type = 'localStorage';
    $(document).trigger(newEvent);
    // Send the event to listeners for this key only.
    newEvent.type = newEvent.key + '.localStorage';
    $(document).trigger(newEvent);
});

$(document).on('keyName.localStorage', function(e){console.log(e.newValue);});

// Window / Tab 1:
store.set('keyName', 'testValue');

// All other Windows / Tabs:
// 'testValue' is logged.

This seems like the perfect addition to this library. It would make cross-window/tab communication with store.js dead-simple. Want all of your web app tabs to know you just got a new message in the current tab? Just call store.set('newMessage', 'hey!');.

Of course, it would only work when localStorage is supported, but it would be easy enough to indicate in the .on() function’s return value whether or not listening is enabled.

I can submit a pull request if you’re interested.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
marcuswestincommented, Feb 28, 2017

Store.js v2.0 has been released with support for storage events and storage value observations: https://github.com/marcuswestin/store.js#user-content-list-of-all-plugins

If you update you will get this functionality plus a bunch more 😃

0reactions
aensleycommented, Jun 26, 2013

Nevermind. I think I figured it out. I just submitted a pull request to add an unbind() method. We should probably continue our conversation there so we’re not cluttering up this repository.

nbubna/store#7

Read more comments on GitHub >

github_iconTop Results From Across the Web

React: Custom hook for accessing storage - DEV Community ‍ ‍
I was using react so had a choice, pass all the data in props and only change storage content from the top level...
Read more >
React Hooks with LocalStorage - Stack Overflow
My idea is once the AddNoteToLocalStorage(note) method is executed, it should detect a change in the localstorage (which is doing now) then ...
Read more >
Window: storage event - Web APIs | MDN
The storage event of the Window interface fires when a storage area (localStorage) has been modified in the context of another document.
Read more >
Using localStorage with React Hooks - LogRocket Blog
Learn how using localStorage with React Hooks can persist user information in browser storage and share logic between multiple components.
Read more >
Send Azure Blob storage events to web endpoint - Azure CLI
Use Azure Event Grid to subscribe to Blob storage events. Send the events to a Webhook. Handle the events in a web application....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found