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

signals-store

v0.1.5

Published

A simple store for state management using [deepSignals](https://github.com/luisherranz/deepsignal). Can be used in all frameworks that support Signals When providing a ```dataApi``` implentation you can automatically sync stata to for example firebase [(

Downloads

23

Readme

signals-store

A simple store for state management using deepSignals. Can be used in all frameworks that support Signals When providing a dataApi implentation you can automatically sync stata to for example firebase (example) or localStorage (example).

Created to seperate state management from the UI components without a lot of boilerplate code.

Examples

Here a demo and the code

create

// creates a new store
const store = new Store<ActivityStateModel>(initialState, !environment.production);

In reactJs you can add the store in a useRef if you use the store in a single component or an useContext when using it is multiple components. The store can be mapped to the component state like this:

use

import { Signal, computed } from '@preact/signals-react';
import { StateModel, initialState } from './store';
import { Store } from 'signals-store';

export const store = new Store<StateModel>({ ...initialState });
export const $state = computed(() => store.signal).value;

actions

Create an action that implements StoreAction<T, M>

export class LoadAction implements StoreAction<StateModel, never> {
    type = "LOAD";
    async execute(ctx: StateContext<StateModel>): Promise<StateModel> {
        if (ctx.getState().users.length === 0) {
            ctx.patchState({ loading: true });
            const users = (await axios.get<ApiResponse>('https://randomuser.me/api/?results=20')).data.results;
            return ctx.patchState({ loading: false, users });
        }
    }
}

API

Store:

  • constructor: (initialState: T = The initial state, devTools: boolean (connect to redux devTools), dataApi? =
  • addCallback: (callback: (action: ActionType<T, unknown>, oldState: T, newState: T, context: Map<string, unknown>) => void) => void can be to add a callback function that captures all actions. For example to log all actions to the console or database.
  • dispatch: (action: StoreAction<T, unknown>) => Promise<T>: dispatches an action and return a promise with the new state
  • currentState: returns the current state.
  • asObservable: return an observable of T

ctx: StateContext

  • getContext<T2>(name: string): gets the context that is added while creating the store. E.g. use to access History *
  • dispatch: (action: StoreAction<T, unknown>) => Promise<T>: dispatches an action and return a promise with the new state
  • getState: gets the current state.
  • setState: set the entire new state.
  • patchState: set only the changed properties of the state, these will be merged with the current state.
  • dataApi: the data api that is passed. Can be casted to the used implementation.
  • ```storeAction``: default: true, if a specific action should not be logged it can be overridden by setting this property to false
  • ```storeState``: default: true, if a specific action should not result in a stored state it can be overridden by setting this property to false

dataApi: Optionally you can pass a dataApi implementation to automatically store the state. Examples can be found here.

  • syncOptions
  • state: state can be stored automatically:
    • sync: boolean: indicates if the state has to be stored
    • collectionName: name of the collection or table where the data will be stored. (default: state)
    • addUserId: add a createdBy field in the state
    • excludedFields: exclude fields from the state that you don't want to store in the database.
  • action: all actions including payload can be stored too e.g. to analyse the use of your application.
    • sync: indicates if actions should ben stored
    • collectionName: name of the collection or table where the data will be stored. (default: actions)
    • addUserId: add a createdBy field in the action
    • excludedFields: exclude fields from the action payload that you don't want to store in the database.
  • getUserId: () => string: get the userId of the logged in user.
  • getState: () => Promise<T>: returns the stored state
  • setState: (doc: T) => Promise<void>: stores the state
  • storeAction<P>(action: ActionType<T, P>): void; stores an action
  • To use getContext() you have to set the dependency somewhere where it is available:
  setStoreContext([
    { name: 'api', dependency: new Api() }
  ])

In the action you can use:

  const api = ctx.getContext<IApi>('api');