šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Book a DemoInstallSign in
Socket

@nicholasadamou/storage

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nicholasadamou/storage

A small function that emits events in the same page and tab that sessionStorage and localStorage are changed in.

1.0.5
latest
Source
npm
Version published
Weekly downloads
9
200%
Maintainers
1
Weekly downloads
Ā 
Created
Source

storage Build Status

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!

Getting Started

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!

Installation

You can download the package using npm with:

npm install @nicholasadamou/storage --save

The Problem

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

The solution is storage, obviously. šŸ’šŸ¼

import storage from '@nicholasadamou/storage'

storage('local')

// Like šŸŽ©, it works!
window.addEventListener('local-storage', foo)

The Options

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)

The Event

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!!' }

License

Ā© Nicholas Adamou.

It is free software, and may be redistributed under the terms specified in the LICENSE file.

Keywords

localStorage

FAQs

Package last updated on 08 Dec 2020

Did you know?

Socket

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.

Install

Related posts