GithubHelp home page GithubHelp logo

node-fsmjs's Introduction

fsmjs

Build Status

State machines are back, the async way!

$ npm install fsmjs

By example

var fsmjs = require('../lib/fsmjs');

var tim = fsmjs({

	idle: {

		// when 'go' or 'start' or 'g' are pushed, we move to 'running' and start
		// an interval that emits a 'tick' event every 100ms.
		'(go|start|g)': function(cb) {
			process.stdout.write('starting engines...\n');
			tim.state = 'running'; 
			tim._timer = tim.interval('tick', 100);
			cb();
		},

		// strings are target states (and emitted events)
		exit: 'end',
		e: 'error',

		// any other event in this state shows this error
		'.*': function(cb, e) {
			console.log('error: i cant understand what you mean by "' + e + '"');
			cb();
		},
	},

	running: {

		// animate clock every tick
		tick: function(cb) {
			var clock = [ '|', '/', '-', '\\' ];
			process.stdout.write('(' + clock[tim._i] + ")");
			for (var i = 0; i < 50; ++i) process.stdout.write(' ');
			process.stdout.write('\r');
			tim._i = (tim._i + 1) % clock.length;
			cb();
		},

		// cannot exit from this state
		exit: 'error',

		// when 'no' or 'stop' or 'x' are pushed, move to 'stopping' and start
		// a 2sec timeout that emits 'elapsed' when elapsed (surpise!)
		'(no|stop|x)': function(cb) { 
			process.stdout.write('stopping...\n');
			tim.state = 'stopping';
			tim.timeout('stopped', 5000);

			cb();
		},

		$exit: function(cb) {
			// take some time before changing state.
			console.log('our running times are over.. give me 2 more seconds.');
			setTimeout(cb, 2000);
		},

	},

	stopping: {

		// called when the stopping timer elapses. clears the 
		// interval and changes state to 'idle'
		stopped: function(cb) {
			process.stdout.write('\nall done.\n');
			clearInterval(tim._timer);
			tim.state = 'idle';
			cb();
		},

		// a tick during stop operation, show dots
		tick: function(cb) {
			process.stdout.write(".");
			cb();
		},

		exit: 'error',

	},

	error: function(cb, state) {
		console.log('An error occured in state', state);
		tim.state = state;
		cb();
	},

	_timer: null, // timer object to allow clearing the interval
	_i: 0, // animated clock
});

tim.on('end', function() {
	process.exit();
});

tim.on('error', function() {
	console.log('on-error');
});

tim.on('idle.start', function() {
	console.log("try 'go' the next time...");
});

var i = require('../lib/debugger')(tim, { verbose: false });

More examples under examples/.

Lisence

MIT

node-fsmjs's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.