npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

discount-store

v1.0.2

Published

Store data globally in your app. Generic library, no dependencies.

Downloads

4

Readme

Discount store

Build Status Coverage Status

Store data globally in your app. Generic library, no dependencies.

Installation

npm install discount-store
import { createStore } from 'discount-store'

Usage

Creating a 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

Getting store values

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

Setting store values

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

Listening for state changes to fields

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;

Unsubsribing to state changes

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;

Listening for any state events

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', () => {})

Unsubscribing from state events

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

Setting event listeners in bulk

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

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

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

Inspiration

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).