From 1a4fd4ff86a0122601bd01ca6e62f309b122ea92 Mon Sep 17 00:00:00 2001 From: taoqf Date: Fri, 2 Jun 2017 21:19:50 +0800 Subject: [PATCH] rename state-machine to javascript-state-machine (#16883) * rename state-machine to javascript-state-machine * add state-machine to notNeededPackages.json * update version of javascript-state-machine * remove state-machine from notNeededPackes.json --- types/javascript-state-machine/index.d.ts | 105 ++++++++++++++++++ .../javascript-state-machine-tests.ts | 32 ++++++ .../tsconfig.json | 2 +- types/javascript-state-machine/tslint.json | 3 + types/state-machine/index.d.ts | 97 ---------------- types/state-machine/state-machine-tests.ts | 32 ------ 6 files changed, 141 insertions(+), 130 deletions(-) create mode 100644 types/javascript-state-machine/index.d.ts create mode 100644 types/javascript-state-machine/javascript-state-machine-tests.ts rename types/{state-machine => javascript-state-machine}/tsconfig.json (90%) create mode 100644 types/javascript-state-machine/tslint.json delete mode 100644 types/state-machine/index.d.ts delete mode 100644 types/state-machine/state-machine-tests.ts diff --git a/types/javascript-state-machine/index.d.ts b/types/javascript-state-machine/index.d.ts new file mode 100644 index 0000000000..32088d7bd4 --- /dev/null +++ b/types/javascript-state-machine/index.d.ts @@ -0,0 +1,105 @@ +// Type definitions for Finite State Machine 2.4 +// Project: https://github.com/jakesgordon/javascript-state-machine +// Definitions by: Boris Yankov , +// Maarten Docter , +// William Sears , +// samael , +// taoqf +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export type StateMachineErrorCallback = ( + eventName?: string, + from?: string, + to?: string, + args?: any[], + errorCode?: number, + errorMessage?: string, + ex?: Error +) => void; // NB. errorCode? See: StateMachine.Error + +export interface StateMachineEventDef { + name: string; + from: any; // string or string[] + to: string; +} + +export type StateMachineEvent = (...args: any[]) => void; + +export interface StateMachineConfig { + initial?: any; // string or { state: 'foo', event: 'setup', defer: true|false } + events?: StateMachineEventDef[]; + callbacks?: { + [s: string]: (event?: string, from?: string, to?: string, ...args: any[]) => any; + }; + target?: StateMachine; + error?: StateMachineErrorCallback; +} + +export type StateMachineIsFinished = (state: string) => boolean; + +export const VERSION: string; // = "2.4.0" +export const WILDCARD: string; // = '*' +export const ASYNC: string; // = 'async' + +export const Result: { + SUCCEEDED: number; // = 1, the event transitioned successfully from one state to another + NOTRANSITION: number; // = 2, the event was successfull but no state transition was necessary + CANCELLED: number; // = 3, the event was cancelled by the caller in a beforeEvent callback + PENDING: number; // = 4, the event is asynchronous and the caller is in control of when the transition occurs +}; + +export const Error: { + INVALID_TRANSITION: number; // = 100, caller tried to fire an event that was innapropriate in the current state + PENDING_TRANSITION: number; // = 200, caller tried to fire an event while an async transition was still pending + INVALID_CALLBACK: number; // = 300, caller provided callback function threw an exception +}; + +export function create(config: StateMachineConfig, target?: StateMachine): StateMachine; + +export interface StateMachineTransition { + (): void; + cancel(): void; +} + +export type StateMachineIs = (state: string) => boolean; + +export type StateMachineCan = (evt: string) => boolean; + +export type StateMachineTransitions = () => string[]; + +export interface StateMachine { + current: string; + is: StateMachineIs; + can: StateMachineCan; + cannot: StateMachineCan; + error: StateMachineErrorCallback; + isFinished: StateMachineIsFinished; + /* transition - only available when performing async state transitions; otherwise null. Can be a: + [1] fsm.transition(); // called from async callback + [2] fsm.transition.cancel(); + */ + transition: StateMachineTransition; + transitions: StateMachineTransitions; +} + +export namespace StateMachine { + const VERSION: string; // = "2.4.0" + const WILDCARD: string; // = '*' + const ASYNC: string; // = 'async' + + const Result: { + SUCCEEDED: number; // = 1, the event transitioned successfully from one state to another + NOTRANSITION: number; // = 2, the event was successfull but no state transition was necessary + CANCELLED: number; // = 3, the event was cancelled by the caller in a beforeEvent callback + PENDING: number; // = 4, the event is asynchronous and the caller is in control of when the transition occurs + }; + + const Error: { + INVALID_TRANSITION: number; // = 100, caller tried to fire an event that was innapropriate in the current state + PENDING_TRANSITION: number; // = 200, caller tried to fire an event while an async transition was still pending + INVALID_CALLBACK: number; // = 300, caller provided callback function threw an exception + }; + function create(config: StateMachineConfig, target?: StateMachine): StateMachine; +} + +export as namespace StateMachine; diff --git a/types/javascript-state-machine/javascript-state-machine-tests.ts b/types/javascript-state-machine/javascript-state-machine-tests.ts new file mode 100644 index 0000000000..099926acf4 --- /dev/null +++ b/types/javascript-state-machine/javascript-state-machine-tests.ts @@ -0,0 +1,32 @@ +import { StateMachine, StateMachineEvent, create } from 'javascript-state-machine'; + +interface StateMachineTest extends StateMachine { + warn?: StateMachineEvent; + panic?: StateMachineEvent; + calm?: StateMachineEvent; + clear?: StateMachineEvent; +} + +const fsm: StateMachineTest = 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(event?, from?, to?, msg?) { alert('panic! ' + msg); }, + onclear(event?, from?, to?, msg?) { alert('thanks to ' + msg); }, + ongreen(event?, from?, to?) { document.body.className = 'green'; }, + onyellow(event?, from?, to?) { document.body.className = 'yellow'; }, + onred(event?, from?, to?) { document.body.className = 'red'; }, + } +}); + +// fsm.warn(); // transition from green to yellow +// fsm.panic("ERROR ALERT"); // transition from yellow to red +// fsm.calm(); // transition from red to yellow +// fsm.clear("All clear"); // transition from yellow to green + +const transitions = fsm.transitions(); diff --git a/types/state-machine/tsconfig.json b/types/javascript-state-machine/tsconfig.json similarity index 90% rename from types/state-machine/tsconfig.json rename to types/javascript-state-machine/tsconfig.json index 3d1eaef3f7..45c4f171fb 100644 --- a/types/state-machine/tsconfig.json +++ b/types/javascript-state-machine/tsconfig.json @@ -18,6 +18,6 @@ }, "files": [ "index.d.ts", - "state-machine-tests.ts" + "javascript-state-machine-tests.ts" ] } \ No newline at end of file diff --git a/types/javascript-state-machine/tslint.json b/types/javascript-state-machine/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/javascript-state-machine/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} diff --git a/types/state-machine/index.d.ts b/types/state-machine/index.d.ts deleted file mode 100644 index 5ece36c4c1..0000000000 --- a/types/state-machine/index.d.ts +++ /dev/null @@ -1,97 +0,0 @@ -// Type definitions for Finite State Machine 2.3.5 -// Project: https://github.com/jakesgordon/javascript-state-machine -// Definitions by: Boris Yankov , Maarten Docter , William Sears , samael -// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped - -interface StateMachineErrorCallback { - (eventName?: string, from?: string, to?: string, args?: any[], errorCode?: number, errorMessage?: string, ex?: Error): void; // NB. errorCode? See: StateMachine.Error -} - -interface StateMachineEventDef { - name: string; - from: any; // string or string[] - to: string; -} - -interface StateMachineEvent { - (...args: any[]): void; -} - -interface StateMachineConfig { - initial?: any; // string or { state: 'foo', event: 'setup', defer: true|false } - events?: StateMachineEventDef[]; - callbacks?: { - [s: string]: (event?: string, from?: string, to?: string, ...args: any[]) => any; - }; - target?: StateMachine; - error?: StateMachineErrorCallback; -} - -interface StateMachineIsFinished { - (state: string): boolean; -} - -interface StateMachineStatic { - - VERSION: string; // = "2.3.5" - WILDCARD: string; // = '*' - ASYNC: string; // = 'async' - - Result: { - SUCCEEDED: number; // = 1, the event transitioned successfully from one state to another - NOTRANSITION: number; // = 2, the event was successfull but no state transition was necessary - CANCELLED: number; // = 3, the event was cancelled by the caller in a beforeEvent callback - PENDING: number; // = 4, the event is asynchronous and the caller is in control of when the transition occurs - }; - - Error: { - INVALID_TRANSITION: number; // = 100, caller tried to fire an event that was innapropriate in the current state - PENDING_TRANSITION: number; // = 200, caller tried to fire an event while an async transition was still pending - INVALID_CALLBACK: number; // = 300, caller provided callback function threw an exception - }; - - create(config: StateMachineConfig, target?: StateMachine): StateMachine; -} - -interface StateMachineTransition { - (): void; - cancel(): void; -} - -interface StateMachineIs { - (state: string): boolean; -} - -interface StateMachineCan { - (evt: string): boolean; -} - -interface StateMachineTransitions { - (): Array; -} - -interface StateMachine { - current: string; - is: StateMachineIs; - can: StateMachineCan; - cannot: StateMachineCan; - error: StateMachineErrorCallback; - isFinished: StateMachineIsFinished; - /* transition - only available when performing async state transitions; otherwise null. Can be a: - [1] fsm.transition(); // called from async callback - [2] fsm.transition.cancel(); - */ - transition: StateMachineTransition; - transitions: StateMachineTransitions; -} - -declare var StateMachine: StateMachineStatic; - -declare module "state-machine" { - export = StateMachine; -} - -declare module "javascript-state-machine" { - - export let StateMachine: StateMachineStatic; -} diff --git a/types/state-machine/state-machine-tests.ts b/types/state-machine/state-machine-tests.ts deleted file mode 100644 index 0a433de593..0000000000 --- a/types/state-machine/state-machine-tests.ts +++ /dev/null @@ -1,32 +0,0 @@ - - -interface StateMachineTest extends StateMachine { - warn?: StateMachineEvent; - panic?: StateMachineEvent; - calm?: StateMachineEvent; - clear?: StateMachineEvent; -} - -var fsm: StateMachineTest = 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'; }, - } -}); - -//fsm.warn(); // transition from green to yellow -//fsm.panic("ERROR ALERT"); // transition from yellow to red -//fsm.calm(); // transition from red to yellow -//fsm.clear("All clear"); // transition from yellow to green - -let transitions: Array = fsm.transitions();