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

pinkistate

v0.2.0

Published

Stand-alone & un-opinionated/framework-agnostic micro-state management tool

Downloads

1

Readme

Pinkistate

Stand-alone & un-opinionated/framework-agnostic micro-state management tool

Pinkistate is great for managing application state(s) in a generic manner therefore it makes it easy to migrate/refactor your state-management. You have the freedom to implement a redux action-like transformer method or a more generic, event-based based one with custom fields.

A good example/case for micro-states is a system where a user can be logged in from multiple devices at once that need to share the user state - via WebSockets maybe.
In this case a user client entity can aggregate the state changes into a micro-state and broadcast them to all the client connections. Of course this all depends on software design, this is just one possible example. :sunglasses:

Concept

One minimal source file that is easily understandable, debuggable and includable in the source code. Transpilation is completely up to you.

Minimalistic API with 4 methods:

  • Trigger - trigger state transform with payload
  • Transform - merge payload into state
  • Change - callback for when the state changes
  • Read - return actual state

!!! IMPORTANT !!! Pinkistate is - currently - a Node module so you must include the source file in your project's build if you're using it for the browser environment!

Micro state

In order to create a micro state you need 2 things

  1. A default state (optional)
  2. A change handler - this gets invoked every time the state changes
const pinkistate = require('pinkistate');

// create micro-state with default state
const defaultState = {};
const myState = pinkistate(defaultState);

// micro-state change handler
const onchange = (newState, oldState, triggerPayload) => {
    // do something when state changes
};

// register onchange handler
myState.onchange(onchange);

You can also deconstruct the state API if need be

const { trigger, transform, read, onchange } = ps(defaultState, onChange);

defaultState

Default state can be any value except undefined.

undefined will default to an empty object ({});

const defaultState = {};

onchange

Change handler is used to notify your View/Component tree to re-render itself or to distribute a state via a socket connection, etc.

The state change handler receives 3 arguments

  1. The new state
  2. The old state
  3. The payload that triggered the change

Triggers

Triggers are methods that take a payload and invoke all registered transformers. Like actions in redux except in a more generic way - again, you can implement your own logic around this.

Each trigger is registered and batched to be executed in the next frame.

myState.trigger({ hello: "World!" });

Triggers can be asynchronous as well.

const myTrigger = (trigger) => {
    const sayHi = "Hello World!";
    setTimeout(() => trigger({ sayHi }), 1000);
};

myState.trigger(myTrigger);

Transformers

Transformers are methods that get invoked with the latest state, the old state and payload that triggered them. Think of them as redux reducers - but in a more generic way. There's nothing stopping you from using a type field in your payload and use the tool similar to how a redux reducer works or to implement your own logic around this.

Note: in order to invoke the onchange callback method, the state must be a different value or object reference from the old one!

// this will work
myState.transform((state, payload) => ({ ...state, ...payload }));
myState.trigger({ sayHi: "Hello World!" });

// this will work as well
myState.transform((state, payload) => ({ ...payload }));
myState.trigger({ sayHi: "Hello World!" });

// this WON'T work!
myState.transform((state, payload) => state);
myState.trigger({ sayHi: "Hello World!" });

License

MIT licensed