GithubHelp home page GithubHelp logo

johnpittman / statemanager-js Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 256 KB

Manages states with optional transition callbacks per state. Performance oriented JavaScript.

License: MIT License

JavaScript 100.00%

statemanager-js's Introduction

statemanager

Manages states with optional before and after transitions per state. Always built with JavaScript performance in mind.

Notes

Universal module defined to be used with requirejs, node, commonjs, or global scoped if no module loader is used.

  • All files in the dist folder are minified for production use.
  • All files in the src directory are the source code for development use.
  • Packages point at the dist minified code with source maps.

Development

Requirements

  • nodejs
  • npm install
  • npm install -g gulp bower

Test

gulp test

Gulp Commands

Each process is dependent upon the previous. If one fails the build process exits.

  • gulp
  • gulp test (Unit specifications)
  • gulp build (Test, folder clean-ups, minification, source maps, renaming)
  • gulp deploy (Test, build, versioning)

Usage

Installation

npm: npm install statemanager
bower: bower install statemanager

How to use...

var stateExample = {
    enter: function() {},
    leave: function() {},
    transitions: {
        beforeEnter: function() {},             // Optional
        beforeEnterFromStill: function() {},    // Optional
        leaveToWalking: function() {},          // Optional
    }
};

var movementStates = {
    'Still': {
        enter: function() {
            console.log('Standing Still.');
        },
        leave: function() {
            console.log('Leaving \'Still\'.');
        },
        transitions: {
            beforeEnter: function() {
                console.log('Transitioning to \'Still\'.');
            }
        }
    },
    'Walking': {
        enter: function() {
            console.log('Walking.');
        },
        leave: function() {
            console.log('Leaving \'Walking\'.');
        },
        transitions: {
            beforeEnter: function() {
                console.log('Transitioning to \'Walking\'.');
            }
        }
    },
    'Running': {
        enter: function() {
            console.log('Running.');
        },
        leave: function() {
            console.log('Leaving \'Running\'.');
        },
        transitions: {
            beforeEnterFromWalking: function() {
                console.log('Enter \'Running\' from \'Walking\'.');
            },
            leaveToStill: function() {
                console.log('Leave \'Running\' to \'Still\'.');
            }
        }
    }
}

var listener1 = function(data) {
    console.log('State change listener!');
    console.log(JSON.stringify(data));
};

var movementStateManager = new StateManager(this);

console.log('Adding states.');
movementStateManager.initialize(movementStates, 'Still');

console.log('Initial state: ' + movementStateManager.getCurrentStateId());

console.log('Changing state...');
movementStateManager.changeState('Walking');

console.log('Changing state...');
movementStateManager.changeState('Running');

console.log('Changing state...');
movementStateManager.changeState('Still');

console.log('Changing state...');
movementStateManager.changeState('Walking');

Release Notes

v1.3.0

Breaking Changes...

These are breaking only if you were using them.

  • Removed the initialize and unload processes. An object should be keeping track of it's own initial state. The object would run it's initialize method in the beforeEnter or enter processes and it's unload method in the leave process.

  • Removed before leave processes because these make no sense to have since enter is the main(update) process and before enter is the previous which means leave is the next so we don't need a before next since there's no before previous.

  • Removed the enterFrom process because one process to distinguish what state we're transitioning from is enough.

  • Added: /**

    • Accessor.
    • @return {object} - The actual state object to re-run the enter process if needed. */ StateManager.prototype.getCurrentState;

v1.2.5

Additional Changes...

  • Change the process order of the beforeEnter/beforeEnterFrom/enter calls as well as the beforeLeave/beforeLeaveTo/leave calls. It now is in the logical order of when they would need to be called unless I see something differently down the road but this was pretty thought out.

v1.1.1

Bug Fixes...

  • Removed initialStateId paramenter from addStates(). This is now done on the initializeStates() method or can me manually set by setInitialState().

v1.1.0

Breaking Changes...

  • addStates no longer runs the initial state. The initialize method should be used which will call addStates and then start();
  • Names of the transistion events for each state have changed format. Before ex. onBeforeEnter, Now ex. beforEnter.

Additional Changes...

  • Added setInitialState();
  • Added ability to have optional initialize process for each state that will only get run once unless it's unloaded.
  • Added ability to have optional unload process for each state that will only get run if the initialize process has been run.

Bug Fixes...

  • Make sure the transition object for a state exists.
  • leaveState now passes the data parameter into all leave processes.

statemanager-js's People

Contributors

johnpittman avatar

Stargazers

khaiknievel avatar

Watchers

 avatar

statemanager-js's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.