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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@ehsaneha/observable-store

v1.0.1

Published

A simple yet powerful state management library inspired by `zustand` which allows you to create observable stores in your JavaScript or TypeScript projects.

Downloads

6

Readme

@ehsaneha/observable-store

A simple yet powerful state management library inspired by zustand which allows you to create observable stores in your JavaScript or TypeScript projects. It provides a reactive store that supports state management with built-in observer pattern capabilities, enabling the registration of observers that will be notified when the store's state changes.

NOTE: ⚠️ If you want to use this package in a react app its better to use the @ehsaneha/react-observable-store package instead due to rerenders of functional components.

Features

  • State management: Easily store and retrieve the state.
  • Observability: Register observers that are notified when the state changes.
  • State mutation: Modify the store's state either directly or through a setter function.
  • Observer dependencies: Notify observers only when the state changes in a way that their dependencies have changed.
  • Custom actions: Easily integrate custom actions into your store with createStore.

Installation

npm install @ehsaneha/observable-store

or if you are using Yarn:

yarn add @ehsaneha/observable-store

Usage

Creating a Store

You can create a store by using the createStore function, which optionally accepts an initial state and custom actions.

import { createStore } from "@ehsaneha/observable-store";

// Simply without initial data and types would be Store<undefined>.
const store = createStore();

// Or again without initial data but this time types would be Store<number | undefined>.
const store = createStore<number>();

// Or with initial data and types would be Store<number>.
const store = createStore(0);

// Or with initial data and type for making the types more clear Store<number | string>.
const store = createStore<number | string>(0);

// Or if you want to add some additional actions.
// Now types would be Store<number, { increment: () => void, decrement: () => void }>
const store = createStore(0, (store) => ({
  increment: () => store.set(store.get() + 1),
  decrement: () => store.set(store.get() - 1),
}));

Accessing and Modifying State

You can retrieve the state of the store using the get() method and set the state using the set() method. You can either provide a direct state value or a function to mutate the state.

// Get the current state
const currentState = store.get();

// Set the new state directly
store.set(5);

// Set the new state using a function
store.set((prevState) => prevState + 1);

Observers

To reactively listen to state changes, you can add an observer to the store using the onChange method. The observer function will be called whenever the state changes.

store.onChange((current, previous) => {
  console.log("State changed from", previous, "to", current);
});

Observers can optionally specify dependencies. This ensures they are only notified if the specified state properties change.

store.onChange(
  (current, previous) => {
    console.log("State changed");
  },
  (current) => current % 2 === 0
);

Removing Observers

If you no longer need an observer, you can remove it by using the removeObserver method.

store.removeObserver(observer);

Example: Counter Store

Here’s a full example of using @ehsaneha/observable-store to create a counter store with increment and decrement actions:

import { createStore } from "@ehsaneha/observable-store";

const store = createStore(0, (store) => ({
  increment: () => store.set(store.get() + 1),
  decrement: () => store.set(store.get() - 1),
}));

// Observing changes
store.onChange((current) => console.log("Current state:", current));

// Triggering actions
store.increment(); // Logs: "Current state: 1"
store.decrement(); // Logs: "Current state: 0"

API

createStore<S, TActions>(initState?, actions?)

  • initState: (optional) The initial state of the store.
  • actions: (optional) A function that defines custom actions. The function is called with the store instance, allowing you to access the state and modify it.

Returns a store object that has:

  • get(): Retrieves the current state.
  • set(newState): Sets a new state or updates it based on a function.
  • onChange(observer, deps?): Registers an observer that will be notified when the state changes.

ObservableStoreClass<S>

This is the core class used in the store. It provides methods to manage state and notify observers and you can inherite it to add more functionalities to it.

Methods:

  • get(): Retrieves the current state.
  • set(newState): Sets the new state or updates it using a function.
  • onChange(observer, deps?): Registers an observer to listen for changes to the state.
  • removeObserver(observer): Removes an observer.
  • notifyObservers(current, previous): Notifies all observers when the state has changed.
  • addObserver(observer): Adds an observer to the store.

Dependencies

The package uses lodash.isequal for deep equality checking of state and observer dependencies and also some utilities from @ehsaneha/utils.

License

This package is licensed under the MIT License. See LICENSE for more information.


Feel free to modify this package or contribute to it!