Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

kristi

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

kristi

Simple FSM engine

  • 1.2.4
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

Literate programming NPM version

Kristi

Kristi is an asynchronous finite state machine engine. It allows you to describe a program (or part of it) using an automata-based approach.

In addition, this is my first experiment with literate programming (with help of a literate-programming-lib).

Kristi is inspired by Machina.js library.

Usage

Import

Users of npm can use npm install kristi. In other case, use gulp build-min to get minified UMD-compatible build.

FSM Construction

import { Automaton, EVENTS } from 'kristi';

let fsm = new Automaton({
	'login-screen-is-shown': {
		transitions: {
			'user-authenticated'           : 'todo-screen-is-shown',
			'password-recovery-requested'  : 'password-recovery-screen-is-shown',
		},
		coming() {
			let fsm = this; // Automaton instance is set as `this` in `coming` and `leaving`;

			// AJAX requests for screen template, etc...
			return new Promise((resolve) => {
				$('#btn-recover-passw').click(() => {
					fsm.processEvent('password-recovery-requested');
				});

				resolve();
			});
		},

		leaving() {
			// Some clean-up
		}
	},

	'todo-screen-is-shown' : {
		...
	},

	...
});

fsm.startWith('login-screen-is-shown');

The "best practice" is to move transition functions out of fsm-schema definition:

let fsm = new Automaton({
	'login-screen-is-shown': {
		transitions: {
			'user-authenticated'           : 'todo-screen-is-shown',
			'password-recovery-requested'  : 'password-recovery-screen-is-show',
		},
		coming:  showLoginScreen,
		leaving: hideLoginScreen
	}
	...
});

Events

Kristi provides simple on/off interface, so you can subscribe to some events from fsm instance.

fsm.on(EVENTS.TRANSITION, ({from, to}) => {...});

fsm.on(EVENTS.PROCESSING, ({state, event}) => {...});

TODO: add full Event API description.

API

The full API description could be found here.

License

MIT-LICENSE

Keywords

FAQs

Package last updated on 29 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc