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

@olefjaerestad/store

v1.0.1

Published

Lightweight JavaScript store to store state and subscribe to state changes.

Readme

Store

Lightweight JavaScript store to store state and subscribe to state changes.

Installation

npm i @olefjaerestad/store

API/examples

Create a state store

The Store class takes two parameters:

  • The initial state: An object containing state that can be subscribed to. Must contain all properties up front (values can be undefined).
  • The actions: An object containing functions that can perform CRUD operations on the state. You decide for yourself whether you want to use this or modify the state directly. Can contain both sync and async functions, but to be able to access this within, they must not be arrow functions.
import { Store } from '@olefjaerestad/store';

const store = new Store(
  {
    foo: 'bar',
    lorem: 'ipsum',
    myObj: {
      baz: 'Hello',
      foo: 'bar',
    },
    myArr: [1, 2, 3],
    someAsyncVal: 'baz',
  },
  {
    setFoo: function(val) { // To have access to 'this', it must not be arrow function.
      this.state.foo = val
    },
    setSomeAsyncVal: async function(val) {
      const newValue = await somePromise(val);
      this.state.someAsyncVal = newValue;
    }
  }
);

Update state

Using actions:

store.actions.setFoo('Hello World');

Or directly:

store.state.foo = 'Hello World';

Access state

const myObj = store.state.myObj;

Access actions

store.actions.setFoo('Hello World');

Subscribe to state changes

Subscribe to all state changes:

const myCallback = (prop, value, prevValue, obj, state) => console.log(prop, value, prevValue, obj, state);
store.subscribe(myCallback); // myCallback will fire whenever a property of store.state changes.
store.state.foo = 'I\'m a new value'; // myCallback is fired.

Subscribe to changes to specific state properties (could be useful for avoiding firing unnecessarily many callbacks = performance increase):

const myCallback = (prop, value, prevValue, obj, state) => console.log(prop, value, prevValue, obj, state);
store.subscribe(['foo'], myCallback); // myCallback will fire whenever store.state.foo changes.
store.state.foo = 'I\'m a new value'; // myCallback is fired.
store.state.lorem = 'I\'m a new value'; // myCallback is not fired.

Note: Subscribed callbacks will fire on changes only to direct properties of store.state, i.e. store.state.myObj, but not store.state.myObj.baz.

Note 2: Subscribed callbacks will fire after the new value is set.

Unsubscribe from state changes

const myCallback = (prop, value, prevValue, obj, state) => console.log('myCallback is fired');
store.subscribe(myCallback); // myCallback will fire whenever a property of store.state changes.
store.state.foo = 'I\'m a new value'; // 'myCallback is fired' is logged.
store.unsubscribe(myCallback);
store.state.foo = 'I\'m another new value'; // 'myCallback is fired' is no longer logged, since we unsubscribed.

Changes to nested properties (objects, arrays)

Wrong:

// Changing the value of an object property.
const myCallback = (prop, value, prevValue, obj, state) => console.log('myCallback is fired');
store.subscribe(myCallback); // myCallback will fire whenever a property of store.state changes.
store.state.myObj.baz = 'I\'m a new value'; // 'myCallback is fired' won't be logged. Only changes to direct properties of store.state will trigger subscribe callbacks.
// Adding a value to an array.
const myCallback = (prop, value, prevValue, obj, state) => console.log('myCallback is fired');
store.subscribe(myCallback); // myCallback will fire whenever a property of store.state changes.
store.state.myArr.push(4); // 'myCallback is fired' won't be logged. Only changes to direct properties of store.state will trigger subscribe callbacks.

Correct:

// Changing the value of an object property.
const myCallback = (prop, value, prevValue, obj, state) => console.log('myCallback is fired');
store.subscribe(myCallback); // myCallback will fire whenever a property of store.state changes.
store.state.myObj = {...store.state.myObj, baz: 'I\'m a new value'}; // 'myCallback is fired' is logged.
// Adding a value to an array.
const myCallback = (prop, value, prevValue, obj, state) => console.log('myCallback is fired');
store.subscribe(myCallback); // myCallback will fire whenever a property of store.state changes.
store.state.myArr = [...store.state.myArr, 4]; // 'myCallback is fired' is logged.

Typescript

The package supports typings through a .d.ts file. The following named exports are exported from the package:

  • Store: class that creates a store.
  • ISubscribeCallback: interface for the subscribe callbacks.

Browser support

| Browser | Supported? | | :-- | :-- | | Chrome >= 60 | ✅ | | Firefox >= 55 | ✅ | | Safari >= 11.1 | ✅ | | Opera >= 47 | ✅ | | Edge >= 79 | ✅ | | Internet Explorer | ❌ | | Chrome for Android > 60 | ✅ | | Firefox for Android > 55 | ✅ | | Opera for Android > 44 | ✅ | | Safari for iOS > 11.3 | ✅ | | Node.js > 8.3.0 | ✅ |

Browser support is mainly affected by use of the following:

Developing

npm i

&

npm run dev

Then just start editing the code in /src. If using an editor with good TypeScript support (e.g. VS Code), any errors will be inlined in the code as well.

Note: Dev produces no output files.

Testing

Unit testing:

npm run test

Note: Unit tests will fail to run if there are errors in the tests themselves.

Integration testing:

npm run test:browsers

This will open a separate window where you can choose which browser to test in. Note that browsers are limited to browsers that are installed on your system and supported by Cypress.

Building

npm run build

Note: Build will fail and report if there are errors.

Publishing

Publish to npm:

npm run publish:npm

Note: requires being logged in to npm locally.