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

pretty-state-machine

v0.0.10

Published

A pretty simple state store for React/Javascript

Downloads

13

Readme

pretty-state-machine

A pretty simple state store for React/Javascript

Background

When I started to code with React, I found redux too unwieldy and clunky to use. Especially in combination with classes.

Instead, I wanted to use a state store that was easy to use, easy to understand, and easy to extend.

For example, I wanted to be able to add a new state to the store, and have it be accessible to all components.

Pretty State Machine (PSM) is a caching event-emitting state store that allows you to do that.

Due to the fact that PSM uses an eventemitter, PSM is complimentary to the state patterns in React, so you can use it as a base for your React components to synchronize states.

PSM is also conducive to handling decoupled API implementations. For example, you can use PSM to handle the state of the current user, have it be accessible to all components, and even preload it while creating new components.

Usage

To start using Pretty State Machine, you need to import it into your project:

import { stateMachine } from 'pretty-state-machine';

React

Frontend components can use Pretty State Machine to synchronize their state with the state of the application.

import React, { Component } from "react";
import ReactDOM from "react-dom"

import { stateMachine } from "pretty-state-machine";

stateMachine.sub(({foo}) => console.warn)

class Example extends Component {
    constructor(props) {
        super(props);

        this.state = {
            foo: stateMachine.get('foo', 'bar')
        }
    }

    componentDidMount() {
        stateMachine.sub('foo', this.setState.bind(this));
    }

    componentWillUnmount() {
        stateMachine.sub('foo', this.setState.bind(this));
    }

    render() {
        return (
            <div>
                Foo: {this.state.foo}
            </div>
        );
    }
}

const Updater = function() {
  const bump = () => {
    const foo = Math.round(Math.random()*1000)
    console.log({foo})
    stateMachine.pub({foo})
  }
  return (
    <button onClick={bump}>Change to random number</button>
  )
}

const App = function(props) {
  return (
    <>
      <Example />
      <Updater />
      </>
  )
}

ReactDOM.render(<App />, document.getElementById('root'))

Example Pen

Caveats

In OOP, it will be needed to bind the update function to the component.

Persistence

In frontend environments, it will persist through the use of localStorage, allowing for caching of states.

API

constructor

Arguments:

  • name: string, optional, default: '', the name of the state

Returns: PrettyStateMachine instance

delete

Arguments:

  • topic: string, the name of the state

fetch

Arguments:

  • topic: string, the name of the state
  • defaultVal: any, the default value to return if the state is not found

Returns: an object value of the state

get

Arguments:

  • topic: string, the name of the state
  • defaultVal: any, the default value to return if the state is not found

Returns: the value of the state

pub

Receives and merges an object of state changes to a particular topic (state key), and then emits it to both consumers of the specific and default topics.

Arguments:

  • topic: string, optional, default: 'state', the name of the state
  • value: any, the value of the state

sub

Arguments:

  • topic: string, optional, default: 'state', the name of the state
  • callback: function, the callback to be called when the state changes

unsub

Arguments:

  • topic: string, optional, default: 'state', the name of the state
  • callback: function, the callback to be called when the state changes

attach

Alias for: sub

unattach

Alias for: unsub

detach

Alias for: unsub

shutdown

Removes all listeners