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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ux-state-machine

v0.1.18

Published

Manage Ui state more efficiently

Readme

Ux State Machine

Ux state machine bridges the gap between user flows and app views. It is based on the article The (Switch)-Case for State Machines... by David Khourshid. This library expands on those concepts and adds additional functionality.

import UxStateMachine from 'ux-state-machine';

const states = {
	'start': {
		on: {'EVENT': 'next_state'}
	},
	'next_state': {}
};

let uxStateMachine = new UxStateMachine(states, 'start');

uxStateMachine.emit('EVENT');

uxStateMachine.getState(); // 'next_state'

State change callback

The State Change Callback is triggered when ever a state is changed. It passed three arguments object containing {data, payload}, then 'newState', and 'oldState'.

const states = {
	'start': {
		on: {'EVENT': 'next_state'}
	},
	'next_state': {
		data: {data: 'data'}
	}
};

let uxStateMachine = new UxStateMachine(states, 'start');

let stateChangeCallback = (data, newState, oldState, payload) => {
	console.log(newState); // new_state
	console.log(oldState); // start
	console.log(payload);  //  {payload: 'payload'}  
	console.log(data);  // {data: 'data'}
}
let payload = {payload: 'payload'};
uxStateMachine.onStateChange(stateChangeCallback);
uxStateMachine.emit('EVENT', payload);

Enter and leave methods

Enter and leave methods trigger when move between states. The leave method triggers immediately on leaving the current state and the enter method triggers immedately on entering the next state. But occur before the State Change Callback is called.

const states = {
	'start': {
		on: {'EVENT': 'next_state'},		
		leave: () => { console.log(`leaving 'start'`) },
	},
	'next_state': {
		enter: () => { console.log(`entering 'next_state`) },
	}
};

let uxStateMachine = new UxStateMachine(states, 'start');
uxStateMachine.emit('EVENT');

Code examples

In this example you can see the UxStateMachine in use. On entering the 'enter' function is called on the 'start' state. The attaches a listener to the form and listens for a submit event. On submitting the form it check if the input contains a valid email. If the input does contain a valid email it proceeds to the loading state. If the loading state is successful it emits 'SUCCESS' in then passes on to the complete state, and the complete state removes the event listener from the form. If at any point an error occurs it moves to the 'error' state which passes an error message to the errorNode and stays in that state until the error is resolve.

Working Codepen Example

<form id="form">
	<input id="input" type="email">
	<input type="submit" value="Submit">
</form>
<p id="errorNode"></p>
const form = document.getElementById('form');
const errorNode = document.getElementById('errorNode');
const input = document.getElementById('input');

const states = {
	'start': {
		on: {
			'ERROR': 'error',
			'SUBMIT': 'loading'
		},
		enter: () => {
			form.addEventListener('submit', submitHandler)
		}
	},
	'error': {
		on: {
			'SUBMIT': 'loading',
			'ERROR': 'error',
		},
		enter: () => {		
			errorNode.innerHTML = 'Sorry there was an error'
		},
		leave: () => { 
			errorNode.innerHTML = ''
		},
	},
	loading: {
		on: {
			'SUCCESS': 'complete',
			'ERROR': 'error'
		},
		enter: ({emit}) => {
			errorNode.innerHTML = 'Loading...'
			setTimeout(() => {
        			emit('SUCCESS')
      			}, 2000);
		}
	},
	complete: {
		enter: () => {
      			errorNode.innerHTML = 'Success';
      			input.value = '';
			form.removeEventListener('submit', submitHandler)
		}
	}
};

const uxStateMachine = new UxStateMachine(states, 'start');

function submitHandler (e) {
	e.preventDefault();
	let email = input.value.match(/[^@]+@[^\.]+\..+/g);
	if (email) {
		uxStateMachine.emit('SUBMIT')
	} else {
		uxStateMachine.emit('ERROR')
	}
}