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

emstore

v0.4.1

Published

Event Manager for React

Downloads

13

Readme

alt emstore

The emstore is a minimalistic and performant library for managing the state inside React app. It is easy to track which function changed the state inside your app.

Install

Install emstore library with immer.

npm i emstore immer

Intro

We will build an app counter that increases and decrease value and on every action, it logs the event into the console, and when you refresh it loads the state from local storage.

alt emstore

First define just one state in the beginning, let's call this new state value and let initial value be 0. Each property in the object is state.

const { Provider, useState, eventState } = createStore({ value: 0 });

Wrap your application in Provider to share states across components.

<Provider>
  <App/>
</Provider>

Use hook useState to notify the component on state change.

function App() {
  const [value] = useState("value");

  return (
    <span>{value}</span>
  );
}

Create event called increase.

const state = eventState("increase");

function increase() {
  const [, setValue] = state("value");

  setValue((value) => value + 1);
};

Add a button to fire the increase event.

<button onClick={increase}>+</button>

Create event called decrease.

const state = eventState("decrease");

function decrease() {
  const [, setValue] = state("value");

  setValue((value) => value - 1);
};

Add a button to fire the decrease event.

<button onClick={decrease}>-</button>

Turn log for events with consoleLog.

const { Provider, useState, eventState } = createStore({ value: 0 }, {
  consoleLog: true,
});

When you fire an event you can see the time, the event's name, the state name that you used in a function, and the previous value with a new value logged in the console.

To make a persistent app on browser refresh use the persist.

const { Provider, useState, eventState } = createStore({ value: 0 }, {
  consoleLog: true,
  persist: true,
});

You can check the console and you'll see that emstore is saving every state change in local storage.

alt emstore

If your state is a Map or Set you need to enable in immer with enableMapAndSet.

const { Provider, useState, eventState } = createStore({ value: 0 }, {
  consoleLog: true,
  persist: true,
  enableMapAndSet: true,
});

You could hook up to on onSetState.

const { Provider, useState, eventState } = createStore({ value: 0 }, {
  consoleLog: true,
  persist: true,
  enableMapAndSet: true,
  onSetState: onSetState, // your function
});

If for some reason your app can't persist on browser refresh for example if you use Set or Map you have to handle yourself with handleLocalStorageDataSave and handle handleLocalStorageDataLoad.

const { Provider, useState, eventState } = createStore({ value: 0 }, {
  consoleLog: true,
  persist: true,
  enableMapAndSet: true,
  onSetState: onSetState, // your function
  handleLocalStorageDataSave: handleLocalStorageDataSave, // your function
  handleLocalStorageDataLoad: handleLocalStorageDataLoad, // your function
});

If for some reason you need to revert the state after some time to the previous state, something like time to live (TTL) this is how you are going to use it.

const { Provider, useState, eventState } = createStore({ value: 0 }, {
  consoleLog: true,
  persist: true,
  enableMapAndSet: true,
  onSetState: onSetState, // your function
  handleLocalStorageDataSave: handleLocalStorageDataSave, // your function
  handleLocalStorageDataLoad: handleLocalStorageDataLoad, // your function
  ttl: {
    value: 3 * 1000 // 3 seconds
  },
});

That's it. 🎉 Have a nice day and keep smiling! 😊

Examples