@propero/easy-store
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

Maintainability Test Coverage

easy-store

Simple, intuitive, functional reactive stores for typescript

npm i @propero/easy-store

Getting started

Create a simple store with createStore:

import { createStore } from "@propero/easy-store";

// The typings for the store are deferred from initialValue
const myStore = createStore(initialValue);

// If you want to broaden the type of the store, you can explicitly
// define it like this
const myTypedStore = createStore<undefined | number>(undefined);

Accessing or updating the current value of a store is as easy as calling getValue and setValue.

console.log(myStore.getValue());
myStore.set(newValue);

Additionally, you can use the update function to change the value of a store based on its current value.

myStore.update(value => newValue);
// myStore.getValue() is now newValue

// also works with promises
myStore.update(async value => {
  await sleep(100);
  return newValue;
});
// myStore.getValue() is newValue when the promise resolves

You can subscribe or unsubscribe to change in a store using the sub and unsub functions.

myStore.set(0);
myStore.subscribe((newVal, oldVal) => console.log(`Change from ${oldVal} to ${newVal}`));

myStore.set(10);
// => Change from 0 to 10

myStore.set(5);
// => Change from 10 to 5

If you have calculated state depending on multiple stores, you can derive a read-only store from them using deriveStore. The first argument of deriveStore is its dependent stores, the second is a function that takes the store values as arguments and returns the store value.

const firstStore = createStore(10);
const secondStore = createStore(20);
const maxStore = deriveStore([firstStore, secondStore], Math.max);
// maxStore.get() => 20

secondStore.set(5);
// maxStore.get() => 10

This module also exposes a createSubscribable method to create simple pub-subs.

const { sub, unsub, notify } = createSubscribable();
sub(console.log);
notify("Hello World");
// => Hello World

All stores and subscribables have a limit to the number of listeners that can be subscribed and will throw once this limit is exceeded. By default, this limit is set to 100 listeners. This is, so you don't accidentally subscribe to a store in a loop, event listener or render cycle. If you need the limit to be higher, you can provide an options object as the last parameter to creation methods like this:

const unlimitedStore = createStore(5, { limit: Infinity });
const almostUnlimitedStore = createStore(2, { limit: 10000 });
const unlimitedSubscribable = createSubscribable({ limit: Infinity });

Package Sidebar

Install

npm i @propero/easy-store

Weekly Downloads

6

Version

2.0.0

License

MIT

Unpacked Size

32.8 kB

Total Files

7

Last publish

Collaborators

  • eyyy-nora
  • nschad