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

@citolab/preact-store

v2.4.8

Published

simple preact store

Downloads

35

Readme

@citolab/preact-store

Simple store to manage state using preact.

Store

The Store class is a simple implementation of a Redux-like store. It provides a way to manage the state of an application, and to dispatch actions that modify the state.

Usage

To use the Store class, you first need to create an instance of it with an initial state:

const store = new Store(initialState);

You can then dispatch actions to modify the state:

store.dispatch({ type: 'INCREMENT' });

You can also subscribe to changes in the state:

store.subscribe(state => {
  console.log('New state:', state);
});

API

Store

constructor(initialState: T, restoreData?: { state: T; actions: Action<unknown>[]; })

Creates a new Store instance with the given initial state. If restoreData is provided, it will restore the state and actions from the given data.

getState(): T

Returns the current state of the store.

getActions(): Action<unknown>[]

Returns an array of all the actions that have been dispatched to the store.

dispatch<P>(action: Action<P>): void

Dispatches an action to the store, which will modify the state.

reset(): void

Resets the store to its initial state and clears all actions.

restoreState(state: T, actions: Action<unknown>[]): void

Restores the state and actions of the store from the given data.

subscribe(listener: Listener<T>): void

Subscribes to changes in the state of the store. The listener function will be called whenever the state changes.

subscribeActions(listener: Listener<Action<unknown>>): void

Subscribes to new actions that are dispatched to the store. The listener function will be called whenever a new action is dispatched.

unsubscribe(listener: Listener<T>): void

Unsubscribes a listener from changes in the state of the store.

unsubscribeAll(): void

Unsubscribes all listeners from changes in the state of the store.

addReducer<P>(type: string, reducer: Reducer<T, P>): void

Adds a reducer function to the store. The reducer function will be called whenever an action with the given type is dispatched to the store.

useStore

A hook that can be used to subscribe to changes in the state of a store.

Types

Action<P>

A type that represents an action that can be dispatched to the store. It has a type property that identifies the type of the action, and a payload property that contains the data for the action.

Listener<T>

A type that represents a listener function that can be subscribed to changes in the state of a store.

Reducer<T, P>

A type that represents a reducer function that can be added to a store. It takes the current state of the store and an action payload, and returns a new state.

IStore<T>

An interface that defines the methods and properties of a store. It extends the IStore interface, which is a more generic interface that defines the methods and properties of any store.