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

vue-easy-state-machine

v0.1.0

Published

Light state machine packaged as a vue component

Downloads

29

Readme

npm version npm downloads license

vue-easy-state-machine

Light state machine packaged as a vue component

Installation

$ npm install vue-easy-state-machine

Overview

This packages provides a <easy-state-machine> component managing a small state machine for UI management

Usage

Declare as Vue plugin:

import VueEasyStateMachine from "vue-easy-state-machine";
Vue.use(VueEasyStateMachine);

In your component <script> section, declare your state table with states & associated behavior:

export default {
  data() {
    return {
      states: {
        askEmail: {
          entry: true,
          success: "askPassword",
          failure: "askEmail"
        },
        askPassword: {
          success: "login",
          failure: "askEmail"
        },
        login: {
          success: this.initSession,
          failure: "askEmail"
        },

        //...

        stateXXX: {
          success: "stateYYY",
          failure: "stateZZZ"
        }

        //...
      }
    };
  },
  methods: {
    initSession() {
      //...
    }
  }
};

In your component <template> section, wrap the state machine around components related to each state:

  • State table is provide through states prop.
  • Each declared state value is available through #default variable, in current array; Only the active state is set to true, all other are set to false.
  • easy-state-machine component exposes success() and failure() methods to trigger evolution; You can use restart() to ... restart.
<easy-state-machine :states="states" #default="state">
  <div v-if="state.current.askEmail">
    <input type="email" placeholder="Email" />
    <button type="button" @click="state.success">Next</button>
  </div>
  <div v-if="state.current.askPassword">
    <input type="password" placeholder="Password" />
    <button type="button" @click="state.success">Next</button>
    <button type="button" @click="state.failure">Previous</button>
  </div>
  <div v-if="state.current.login">
    <strong>You are logged</strong>
    <button type="button" @click="state.success">Init Session</button>
    <button type="button" @click="state.failure">Restart</button>
  </div>
</easy-state-machine>

And that's it !

State Table Advanced Configuration

Each state can handle following items:

{
   "entry": true,
   "onEnter" : () => {},
   "success": "stateXXX",
   "failure": "stateYYY",
   "onLeave" : () => {},
}

entry

  • Type: Boolean
  • Default: false
  • Details: Indicates state to start with
  • Restrictions: Only one true entry per state table. If multiple entries, only the first one is taken.

success

  • Type: String or Function
  • Default: undefined
  • Details: Indicates state to go to in case of successful operation at current state. Can be a function returning state name.
  • Restrictions: Will trigger state machine error if pointing nowhere....

failure

  • Type: String or Function
  • Default: value of success entry
  • Details: Indicates state to go to in case of failed operation at current state. Can be a function returning state name.
  • Restrictions: Will trigger state machine error if pointing nowhere...

onEnter

  • Type: Function
  • Default: undefined
  • Details: Function to call on state arrival
  • Restrictions: Must be current component functions, not called if empty

onLeave

  • Type: Function
  • Default: undefined
  • Details: Function to call on state leaving
  • Restrictions: Must be current component functions, not called if empty

Advanced usage & Tips

Complex branching

You can manage complex branching with success and failure functions:

{
   "success": () => { return this.myBooleanValue ? 'stateXXX' : 'stateYYY' },
   "failure": this.myBranchingFunction,
}

Where myBranchingFunction is defined in methods block:

methods: {}
    myBranchingFunction() {
        if ( /** my test **/ )
            return 'stateXXX';
        else
            return 'stateYYY';
    }
}

Events

stateChange

  • When: On each state change, after onLeave and before onEnter are called
  • First argument: previous state name
  • Second argument: next state name

Transitions

You can use with <transition> for a beautiful effect:

<easy-state-machine :states="states" #default="state">
  <transition
    name="transition-login"
    enter-active-class="animated fadeIn"
    leave-active-class="animated fadeOut"
    mode="out-in"
  >
    <div v-if="state.current.askEmail" key="askEmail">
      ...
    </div>
    <div v-if="state.current.askPassword" key="askPassword">
      ...
    </div>
    <div v-if="state.current.login" key="login">
      ...
    </div>
  </transition>
</easy-state-machine>

Validating/Testing

Sample application provided in sample direcory for testing purpose:

$ npm run sample

Add then open ./sample/index.html in your browser