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

store2state

v0.1.0

Published

This library provides a robust and flexible state management solution for JavaScript and TypeScript applications, with a focus on React integration. It includes a powerful Store class, custom hooks for React, and an AsyncAction utility for handling asynch

Downloads

6

Readme

store2state

This library provides a robust and flexible state management solution for JavaScript and TypeScript applications, with a focus on React integration. It includes a powerful Store class, custom hooks for React, and an AsyncAction utility for handling asynchronous operations.

Features

  • De-centralized state management with subscriptions
  • Efficient state updates with shallow comparison
  • React hooks for easy integration (useStore and useStoreSelector)
  • Asynchronous action handling with status tracking and cancelation
  • TypeScript support for type-safe state management

Installation

npm install store2state

Usage

Creating a Store

import { createStore } from 'store2state';

const initialState = { count: 0 };
const store = createStore(initialState);

Using the Store in React

import { useStore } from 'storee2state';

function Counter() {
  const { get, set } = useStore(store);
  
  return (
    <div>
      <p>Count: {get().count}</p>
      <button onClick={() => set(state => ({ count: state.count + 1 }))}>
        Increment
      </button>
    </div>
  );
}

Using Selectors

import { useStoreSelector } from 'store2state';

function CountDisplay() {
  const count = useStoreSelector(store, state => state.count);
  
  return <p>Count: {count}</p>;
}

Async Actions

import { createAsyncAction, Status } from 'store2state';

const fetchUserAction = createAsyncAction(store, async (store, setStatus, userId) => {
  setStatus(Status.LOADING);
  try {
    const response = await fetch(`/api/users/${userId}`);
    const user = await response.json();
    setStatus(Status.SUCCESS, user);
    return user;
  } catch (error) {
    setStatus(Status.ERROR, error);
    throw error;
  }
});

// Using the async action
fetchUserAction.call(123)
  .then(user => console.log(user))
  .catch(error => console.error(error));

// Checking the status
console.log(fetchUserAction.isLoading);

API Reference

Store<State>

The main class for state management.

  • get(): Get the current state
  • set(newValue: SetterFunc<State>): Update the state
  • subscribe(eventName: string, subscriber: Subscriber<State>): Subscribe to state changes
  • unsubscribe(eventName: string, id: string): Unsubscribe from state changes
  • dispatch(eventName: string): Dispatch an event to subscribers

React Hooks

  • useStore<State>(store: Store<State>): Hook for using the store in React components
  • useStoreSelector<State, Selected>(store: Store<State>, selector: (state: State) => Selected): Hook for selecting specific parts of the state

AsyncAction

Utility for handling asynchronous operations with status tracking.

  • call(...args: Args): Execute the async action
  • cancel(): Cancel the ongoing async action
  • onError(effect: AsyncActionEffect<State, ErrorPayload>): Add an error effect
  • onSuccess(effect: AsyncActionEffect<State, SuccessPayload>): Add a success effect
  • onLoading(effect: AsyncActionEffect<State, void>): Add a loading effect

License

store2state is licensed under the MIT License. See the LICENSE file for details.

Todo

  • Completely decouple the library with react integration and use separate npm library to provide support to various different frameworks.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.