Coder Social home page Coder Social logo

bluebang / state.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from steelbreeze/state.js

0.0 1.0 0.0 2.87 MB

DEPRECATED, please see @steelbreeze/state):

Home Page: https://steelbreeze.net/state

License: MIT License

JavaScript 73.29% TypeScript 26.71%

state.js's Introduction

state.js

Executable state machine for Node and Web development.

NPM Version NPM Downloads Travis CI Code Climate Test Coverage Dev Dependencies

If you like state.js, please star it.

Update: v6 is now complete and can be found at @steelbreeze/state. This repo is now deprecated.

Getting started

The state.js API is split into:

  1. Classes that represent a state machine model (State, PseudoState, Transition, etc.)
  2. An interface and implementation of active state configuration (current state); this allows multiple concurrent instances of a single state machine model
  3. A set of functions that provide the state machine runtime

The API is bound to a global object of your choosing.

state.js is developed in TypeScript and transpiled to JavaScript; you can use it in either language.

Node

1. Install state.js in your project:

$ npm install state.js

2. Import state.js and build your state machine:

var state = require("state.js");

// send log messages, warnings and errors to the console
state.setConsole(console);

// create the state machine model elements
var model = new state.StateMachine("model");
var initial = new state.PseudoState("initial", model, state.PseudoStateKind.Initial);
var stateA = new state.State("stateA", model);
var stateB = new state.State("stateB", model);

// create the state machine model transitions
initial.to(stateA);
stateA.to(stateB).when(function (message) { return message === "move"; });

// create a state machine instance
var instance = new state.StateMachineInstance("instance");

// initialise the model and instance
state.initialise(model, instance);

// send the machine instance a message for evaluation, this will trigger the transition from stateA to stateB
state.evaluate(model, instance, "move");

Web

1. Add state.js to your project:

$ bower install --save state

Alternatively, download direct from lib/web/state.js or lib/web/state.min.js.

2. Include state.js as a script in your page:

<script type="text/javascript" src="/bower_components/state/lib/web/state.min.js" target="state"></script>

Note: the target attribute within the script element defines the name of the global object that the state.js API will be bound to. If not specified, state.js will be bound to window.fsm.

3. Create your state machine in another script:

<script>
	// send log messages, warnings and errors to the console
	state.setConsole(console);

	// create the state machine model elements
	var model = new state.StateMachine("model");
	var initial = new state.PseudoState("initial", model, state.PseudoStateKind.Initial);
	var stateA = new state.State("stateA", model);
	var stateB = new state.State("stateB", model);

	// create the state machine model transitions
	initial.to(stateA);
	stateA.to(stateB).when(function (message) { return message === "move"; });

	// create a state machine instance
	var instance = new state.StateMachineInstance("test");

	// initialise the model and instance
	state.initialise(model, instance);

	// send the machine instance a message for evaluation, this will trigger the transition from stateA to stateB
	state.evaluate(model, instance, "move");
</script>

Versioning

The versions are in the form {major}.{minor}.{build}

  • Major changes introduce significant new behavior and will update the public API.
  • Minor changes introduce features, bug fixes, etc, but note that they also may break the public API.
  • Build changes can introduce features, though usually are fixes and performance enhancements; these will never break the public API.

Documentation

Documentation for the public API can be found here.

Licence

state.js is dual-licenecd under the MIT and GPL v3 licences.

state.js's People

Contributors

ubald avatar

Watchers

James Cloos avatar

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.