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

vahender

v1.1.0

Published

State machine.

Downloads

7

Readme

Va Hender?!

Va Hender?! is a JavaScript statemachine.

Those are the features:

  • Simple syntax.
  • States are hierarchical.
  • There is a root state. All other states are descendants from the root.
  • Unit tested code.
  • "Va Hender?!" is Swedish for "What's Up?!", spelled like we pronounce it in Stockholm.

Get started

This is kind of how it works:

var
	vh = require('vahender'),
	rootState = new vh.Root();

// Define two child states to the root state:
var alive = rootState.child('alive');
var dead = rootState.child('dead');

// Define four child states to the alive state:
var hungry = alive.child('hungry');
var satisfied = alive.child('satisfied');
var full = alive.child('full');
var sick = alive.child('sick');

// Define events to cause transitions:
hungry.eat = satisfied.enter; // Call hungry.eat() to get to satisfied state.

satisfied.eat = full.enter; // Call satisfied.eat() to get to full state.
satisfied.rest = hungry.enter; // Call satisfied.rest() to get to hungry state.

full.eat = sick.enter; // Call full.eat() to get to sick state.
full.rest = satisfied.enter; // Call satisfied.rest() to get to satisfied state.

sick.eat = dead.enter; // Call sick.eat() to get to dead state.
sick.rest = full.enter; // Call satisfied.rest() to get to full state.

// dead state has no transitions.

// Define event to start it all with:
rootState.birth = hungry.enter;

var transition = rootState.birth(); // Returns a Transition object!
transition.on('started', function () {
	console.log('Transition started.');
});
transition.on('aborted', function (reason) {
	console.log('Transition was aborted: ' + reason);
});
transition.on('finished', function () {
	console.log('Transition finished.');
});

// Add events to states:
full.on('enter', function() {
	console.log('You are full.');
});

alive.update = function (next) {
	// As long as alive (or any of alive's descendants) is the current state,
	// this will be called (often!)

	next(); // Must call the callback, or everything will stop.
};

State events

  • enter - this state got activated.
  • leave - this state got inactivated.
  • newchild - this state got a new child. only emitted for children, not emitted for grandchildren. params: 1. the child state obj.
  • current - this state is now the current state.
  • childcurrent - some child became the current state. First argument is new the state object. Second argument is the old state object.

Note: There can only be one current state, but many activated states. When a state is current, it and all it's ancestors are activated.

Transition events

  • started - this transition just started.
  • aborted - this transition was aborted. First argument is the reason why (a string).
  • finished - this transition just finished (successfully).

State properties

  • .active - True if this state or any children/grandchildren/etc is the current state.
  • .longname - The name of this state including breadcrubs from parent states.
  • .name - The name of this state. (The string given at state creation.)
  • .root - The root state object.
  • .root.current - The current state.
  • .parent - The parent of this state. Undefined if State is the root state.
  • .parents - An array of parents, grand parents, and so on.

State methods

  • .child(name, callback) - Creates a new child state. Returns the new child state object. If defined, the callback function will be called with the new child state object as the only argument.
  • .hasParent(state) - True if this state is descendant to the given state. Otherwise false.

Transition properties

  • .from - State to make transition from.
  • .to - State to make transition to.

Change Log

  • 1.1.0 - New feature: .child() has a callback.
  • 1.0.1 - Bugfix: Only current state can initiate transition.
  • 1.0.0 - Initial version.