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

@propero/easy-store

v2.0.0

Published

Simple, intuitive, functional reactive stores for typescript

Downloads

11

Readme

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 });