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

@carry0987/state-manager

v1.0.0

Published

A lightweight and versatile state management solution for maintaining and transitioning application state with type-safe reducers and event-driven listener subscriptions.

Downloads

57

Readme

StateManager-JS

NPM CI
The @carry0987/state-manager package is a versatile and generic TypeScript-based state management utility designed to simplify managing application state with minimal boilerplate. By employing the power of generic types, @carry0987/state-manager allows developers to define their state structures, ensuring type safety and reducing runtime errors. This package is especially useful for applications that require a centralized state management solution without the complexity of larger libraries like Redux.

At its core, @carry0987/state-manager maintains a single state object and provides a mechanism for updating this state through a functional approach. The dispatch method facilitates state transitions using reducer functions, ensuring immutability and predictable state changes. By leveraging the subscribe method, developers can attach listeners that react to state changes, receiving notifications about both the current and previous state. This allows applications to efficiently update the UI, trigger side effects, or perform other tasks in response to state transitions.

Overall, @carry0987/state-manager provides a powerful yet lightweight solution for state management, making it an ideal choice for small to medium-sized applications or projects where simplicity and efficiency are paramount.

Features

  • Lightweight and easy to integrate into existing projects
  • Type-safe state management
  • Efficient state update mechanism with dispatch
  • Flexible subscription management
  • Notification of state changes to multiple listeners

Installation

To install the StateManager library, use the following command:

npm i @carry0987/state-manager -D

Usage

Importing the Library

First, import the StateManager class into your TypeScript file:

import { StateManager } from '@carry0987/state-manager';

Creating a StateManager Instance

Create an instance of StateManager with your initial state:

interface MyState {
    count: number;
}

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

Accessing State

Use the getState method to access the current state:

const currentState = stateManager.getState();
console.log(currentState);

Subscribing to State Changes

Use the subscribe method to listen for state changes:

stateManager.subscribe((current, previous) => {
    console.log('State changed from', previous, 'to', current);
});

Dispatching State Changes

Update the state using the dispatch method with a reducer function:

stateManager.dispatch((state) => ({ count: state.count + 1 }));

Unsubscribing from State Changes

When you no longer need to listen for changes, unsubscribe the listener:

const unsubscribe = stateManager.subscribe((current, previous) => {
    console.log('State changed from', previous, 'to', current);
});

unsubscribe();

API

getState(): S

Retrieves the current state.

getListeners(): ((current?: S, prev?: S) => void)[]

Retrieves the list of current listeners.

dispatch(reducer: (state: S) => S): S

Dispatches a state change by executing the provided reducer function. It prevents nested dispatches.

subscribe(listener: (current?: S, prev?: S) => void): () => void

Subscribes a listener to state changes. The listener is called whenever the state changes, with both the current and previous state.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

License

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