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

vanilla-state-manager

v1.0.2

Published

Javascript/Typescript state management library that uses Immer.js behind the scenes to handle state immutability

Downloads

7

Readme

Here is the updated documentation:

Vanilla State Manager

Overview

Vanilla State Manager is a lightweight, type-safe state management solution for JavaScript applications. It provides a simple and intuitive API for managing state, handling updates, and notifying listeners.

Installation

To install Vanilla State Manager, run one of the following commands:

npm install vanilla-state-manager
yarn add vanilla-state-manager
pnpm install vanilla-state-manager

Usage

Here's an example of how to use Vanilla State Manager:

import StateManager from "vanilla-state-manager";

// Create a new state manager with an initial state
const initialState = { count: 0 };
const stateManager = new StateManager(initialState);

// Get the current state
const currentState = stateManager.getState();
console.log(currentState); // Output: { count: 0 }

// Update the state using the setState method
stateManager.setState((draft) => {
  draft.count += 1;
});
console.log(stateManager.getState()); // Output: { count: 1 }

// Subscribe to state changes using the subscribe method
const unsubscribe = stateManager.subscribe((state) => {
  console.log(state);
});
stateManager.setState((draft) => {
  draft.count += 1;
}); // Output: { count: 2 }
unsubscribe();

API

StateManager<T>

  • constructor(initialState: T): Creates a new state manager with the given initial state.
  • getState(): T: Returns the current state.
  • setState(updater: (draft: T) => void): void: Updates the state using the provided updater function.
  • subscribe(listener: Listener<T>): () => void: Subscribes to state changes using the provided listener function.

Listener<T>

  • (state: T) => void: A function that will be called whenever the state changes.

Types

  • T: The type of the state.

Errors

  • State cannot be null or undefined: Thrown when the initial state is null or undefined.
  • Updater function cannot be null or undefined: Thrown when the updater function is null or undefined.
  • Listener function cannot be null or undefined: Thrown when the listener function is null or undefined.

Dependencies

  • immer: Used for state immutability.

License

Vanilla State Manager is licensed under the MIT License.

TypeScript

Usage

Here's an example of how to use Vanilla State Manager with TypeScript:

import StateManager from "vanilla-state-manager";

// Create a new state manager with an initial state
interface State {
  count: number;
}

const initialState: State = { count: 0 };
const stateManager = new StateManager(initialState);

// Get the current state
const currentState: State = stateManager.getState();
console.log(currentState); // Output: { count: 0 }

// Update the state using the setState method
stateManager.setState((draft: State) => {
  draft.count += 1;
});
console.log(stateManager.getState()); // Output: { count: 1 }

// Subscribe to state changes using the subscribe method
const unsubscribe = stateManager.subscribe((state: State) => {
  console.log(state);
});
stateManager.setState((draft: State) => {
  draft.count += 1;
}); // Output: { count: 2 }
unsubscribe();

Type Definitions

The StateManager class is generic, so you can specify the type of the state when creating a new instance. The getState and setState methods will then return and accept the specified type, respectively.

The Listener type is also generic, so you can specify the type of the state when creating a new listener. The listener function will then receive the specified type as an argument.

Note that the immer package is already included in the vanilla-state-manager package, so you don't need to install it separately.