
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
discount-store
Advanced tools
Store data globally in your app. Generic library, no dependencies.
npm install discount-store
import { createStore } from 'discount-store'
const initialState = {}
createStore(initialState)
NOTE: Since we observe each field in the store for changes, and we have chosen to support IE11 for now, we cannot use Proxy. Therefore, we must know all the fields that will be defined at the time the store is created. If you try to set a field that did not exist upon creation, it will throw an error.
const { state } = createStore({ count: 0 })
state.foo = 'bar' // TypeError: Cannot add property foo, object is not extensible
You can get a store value either from the get
method, or by accessing the field directly from the state object.
const { state, get } = createStore({ count: 0 })
get('count') // 0
state.count // 0
You can set a store value either from the set
method, or by directly setting the field on the state object.
const { state, set } = createStore({ count: 0 })
state.count += 1 // 1
set('count', 5) // 5
You can subscribe to a field to execute a callback any time that field changes by using the onChange
method. This includes when a field is changed from a reset
or clear
.
const { state, onChange } = createStore({ count: 0 })
onChange('count', newValue => {
// do some stuff
});
state.count += 1;
The method onChange
returns a function, that when invoked, will the unsubsribe callback from those change events.
const { state, onChange } = createStore({ count: 0 })
const offChange = onChange('count', newValue => {
// do some stuff
offChange() // effectively a 'once'
});
state.count += 1;
With the on
method, there are four events that you can subscribe to for the store: set
, get
, clear
, reset
const { state, on } = createStore({ count: 0 })
on('set', (key, value) => {})
on('get', value => {})
on('clear', () => {})
on('reset', () => {})
The on
method returns a method that, when invoked, unsubsribes the callback from that event.
const { state, on } = createStore({ count: 0 })
const offSet = on('set', (key, value) => {
offSet() // effectively a 'once'
})
With the use
method, you can set an event listener for each of the store events: set
, get
, clear
, reset
const { use } = createStore({ count: 0 })
use({
get: value => {},
set: (key, value) => {},
reset: () => {},
clear: () => {}
})
Clearing the store empties it of all it's values. However, the keys are kept.
const { state, clear } = createStore({ count: 0 })
state.count // 0
clear()
state.count // undefined
Reseting the store resets the state back to what it was initially created with.
const { state, reset } = createStore({ count: 0 })
state.count += 1 // 1
reset()
state.count // 0
This package's API is mostly inspired by @stencil/store with a few exceptions. I've excluded dispose
, added clear
, removed any peer dependencies, and kept support for IE11. Their API is simple and great, but I wanted an option for a store that was not tied to any framework or library (Stencil, Svelte, React, Redux) or design pattern (Flux).
FAQs
Store data globally in your app. Generic library, no dependencies.
We found that discount-store 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.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.