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

y-state-machine

v0.1.5

Published

simple impletation of state machine

Downloads

1

Readme

Intro

This is a simplified version of state machine in javascript.

Installation

$ npm i --save-dev y-state-machine
# or
$ yarn add --dev y-state-machine

Usage

In a nutshell:

import {StateMachine} from 'y-state-machine'

class YourStateMachine extends StateMachine {
  edit() {}
  onedit() {
    console.log('you are editing')
  }
}

const sm = new YourStateMachine({
  name: 'any_name_you_like',
  init: 'view',
  transitions: [
    {
      name: 'edit',
      from: 'view',
      to: 'editing'
    }
  ]
})

document.addEventListener('click', function handler(e) {
  sm.edit() // 'you are editing'
  sm.edit() // nothing happens
})

In the example above, you declare a simple state machine called YourStateMachine.

It extends the base class of StateMachine and has two methods: edit and onedit.

The edit method corresponds to the name field in the only element of transitions,

which is one of the properties of the option that you use to instantiate YourStateMachine

When invoke sm.edit(), behind the scene, sm checks whether the current state match the need for such activity

In this example, the current state has to be view in order to invoke edit method successfully.

By setting init: 'view', the initial state of sm will be view.

The real codes executed would be the ones within onedit method.

There should always be a action and onaction pair

sm will call onedit internally and the state will become editing.

The second time you try to invoke edit, the state would be editing and won't match the requirement.

In other words, nothing happens.

About from field

from in transitions could be String or Array<String> or *.

Monitor state

You can declare onStateChange method on your own class.

Whenever state changes, onStateChange method will be called with two passing arguments:

newState and oldState, which represent the latest state and the last state respectively.

class yourClass extends StateMachine {
  onStateChange(newState, oldState) {
    // your own logic
  }
}

License

MIT

Copyright (c) 2017-present, Yuchen Liu