mirror of
https://github.com/wassname/DefinitelyTyped.git
synced 2026-09-11 11:50:54 +08:00
Add State Machine definitions and tests
This commit is contained in:
Vendored
+49
@@ -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;
|
||||
@@ -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'; },
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user