diff --git a/README.md b/README.md index 235d5cc00..d7beb91ea 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Complete * [ember.js](http://emberjs.com/) (by [Boris Yankov](https://github.com/borisyankov)) * [Express](http://expressjs.com/) (by [Boris Yankov](https://github.com/borisyankov)) * [Fancybox](http://fancybox.net/) (by [Boris Yankov](https://github.com/borisyankov)) +* [Finite State Machine](https://github.com/jakesgordon/javascript-state-machine) (by [Boris Yankov](https://github.com/borisyankov)) * [Foundation](http://foundation.zurb.com/) (by [Boris Yankov](https://github.com/borisyankov)) * [GoogleMaps](https://developers.google.com/maps/) (by [Esben Nepper](https://github.com/eNepper)) * [Handlebars](http://handlebarsjs.com/) (by [Boris Yankov](https://github.com/borisyankov)) diff --git a/state-machine/state-machine-2.2.d.ts b/state-machine/state-machine-2.2.d.ts new file mode 100644 index 000000000..a8682cdd5 --- /dev/null +++ b/state-machine/state-machine-2.2.d.ts @@ -0,0 +1,49 @@ +// Type definitions for Finite State Machine 2.2 +// Project: https://github.com/jakesgordon/javascript-state-machine +// Definitions by: Boris Yankov +// Definitions: https://github.com/borisyankov/DefinitelyTyped + + +interface ErrorCallback { + (name: string, from: string, to: string, args: any[]): void; +} + +interface EventCallback { + event: string; + from: string; + to: string; + msg?: string; +} + +interface StateMachineEvent { + name: string; + from: string; + to: string; +} + +interface StateMachineConfig { + initial?: any; // string or { state: 'foo', event: 'setup', defer: true|false } + events?: StateMachineEvent[]; + callbacks?: any; + target?: any; + error?: ErrorCallback; +} + +interface StateMachineStatic { + create(config: StateMachineConfig, target?: any): StateMachine; +} + +interface StateMachine { + current: string; + is(sstate: string): bool; + can(event: StateMachineEvent): bool; + cannot(event: StateMachineEvent): bool; + error: ErrorCallback; + + onbeforeevent: EventCallback; + onleaveevent: EventCallback; + onenterevent: EventCallback; + onafterevent: EventCallback; +} + +declare var StateMachine: StateMachineStatic; \ No newline at end of file diff --git a/state-machine/state-machine-tests.ts b/state-machine/state-machine-tests.ts new file mode 100644 index 000000000..4ff9bdf93 --- /dev/null +++ b/state-machine/state-machine-tests.ts @@ -0,0 +1,18 @@ +/// + +var fsm = StateMachine.create({ + initial: 'green', + events: [ + { name: 'warn', from: 'green', to: 'yellow' }, + { name: 'panic', from: 'yellow', to: 'red' }, + { name: 'calm', from: 'red', to: 'yellow' }, + { name: 'clear', from: 'yellow', to: 'green' } + ], + callbacks: { + onpanic: function (event, from, to, msg) { alert('panic! ' + msg); }, + onclear: function (event, from, to, msg) { alert('thanks to ' + msg); }, + ongreen: function (event, from, to) { document.body.className = 'green'; }, + onyellow: function (event, from, to) { document.body.className = 'yellow'; }, + onred: function (event, from, to) { document.body.className = 'red'; }, + } +}); \ No newline at end of file