Add State Machine definitions and tests

This commit is contained in:
Boris Yankov
2013-01-09 13:29:39 +02:00
parent 0ce8660fec
commit ecec763914
3 changed files with 68 additions and 0 deletions
+1
View File
@@ -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))
+49
View File
@@ -0,0 +1,49 @@
// Type definitions for Finite State Machine 2.2
// Project: https://github.com/jakesgordon/javascript-state-machine
// Definitions by: Boris Yankov <https://github.com/borisyankov/>
// 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;
+18
View File
@@ -0,0 +1,18 @@
/// <reference path="state-machine-2.2.d.ts" />
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'; },
}
});