
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
@nicholasadamou/storage
Advanced tools
A small function that emits events in the same page and tab that sessionStorage and localStorage are changed in.
Have you ever needed to listen to localStorage or sessionStorage to change from within the same tab? Well, you've come to the right place my friend!
storage
is a small function that emits events in the same page and tab that sessionStorage
and localStorage
are changed in.
No dependencies!
Much š„!
Only ~800b!
You can download the package using npm
with:
npm install @nicholasadamou/storage --save
When localStorage
or sessionStorage
are changed, a storage
event is emitted, but it can only be picked up by other open tabs of the same origin. Sometimes you need access to that event in the tab that it was dispatched from, as well.
// Does not work.
window.addEventListener('storage', baz)
localStorage.setItem('foo', 'bar')
The solution is storage
, obviously. šš¼
import storage from '@nicholasadamou/storage'
storage('local')
// Like š©, it works!
window.addEventListener('local-storage', foo)
The first argument must be a string, one of: "local"
or "session"
to determine which storage you wish to emit events for.
You can pass an options object as the second argument to storage
. This options object currently supports one property: eventName
. Setting the eventName
will allow you flexibility to have different names for different targets, such as localStorage
and sessionStorage
.
import storage from '@nicholasadamou/storage'
storage('session', {
eventName: 'DonaldTrump'
})
window.addEventListener('DonaldTrump', maga)
As with any event listener, an event is provided to your callback. In this instance, event.detail
will have two properties: key
, and value
. These values represent the storage key that changed and the new value that accompanies the change.
import storage from '@nicholasadamou/storage'
storage('local', {
eventName: 'DonaldTrump'
})
window.addEventListener('DonaldTrump', (event) => {
console.log(event)
})
localStorage.setItem('Election 2020 Quote', 'We WON by a lot!!')
// { key: 'Election 2020 Quote', value: 'We WON by a lot!!' }
Ā© Nicholas Adamou.
It is free software, and may be redistributed under the terms specified in the LICENSE file.
FAQs
A small function that emits events in the same page and tab that sessionStorage and localStorage are changed in.
The npm package @nicholasadamou/storage receives a total of 3 weekly downloads. As such, @nicholasadamou/storage popularity was classified as not popular.
We found that @nicholasadamou/storage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago.Ā It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.