portable-fsm

1.6.0 • Public • Published

portable-fsm

Simple, node/browser compatible generic FSM system

Examples

var
	FSM = require('portable-fsm'),
	fsm = new FSM();

fsm.debug = true;

fsm.addState({
	Start: {
		init: 'InProgress'
	},
	InProgress: {
		abort: 'Cancelled',
		complete: 'Done'
	}
});

fsm
	.on('InProgress', function() {
		console.log('Progress start');
		if(Math.random() > 0.5) {
			console.log(' -> Aborting...');
			fsm.consume('abort');
		} else {
			console.log(' -> Completing...');
			fsm.consume('complete');
		}
	}, function() {
			console.log('Progress end');
	})
	.on('Cancelled', function() {
		console.log('Operation cancelled');
	})
	.on('Done', function() {
		console.log('Operation completed');
	});

fsm
	.when('complete', function() {
		console.log('ALL DONE!');
	})
	.when('abort', function() {
		console.log('ABORT! ABORT! ABORT!');
	});

fsm.consume('init');

/portable-fsm/

    Package Sidebar

    Install

    npm i portable-fsm

    Weekly Downloads

    2

    Version

    1.6.0

    License

    MIT

    Last publish

    Collaborators

    • killroy