Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
discount-store
Advanced tools
Readme
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.
The npm package discount-store receives a total of 9 weekly downloads. As such, discount-store popularity was classified as not popular.
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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.