@uuxxx/fsm 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Plugin-C8fnOXt5.d.ts +37 -0
- package/dist/history-plugin.d.ts +10 -0
- package/dist/history-plugin.js +1 -0
- package/dist/index.d.ts +2 -45
- package/dist/index.js +4 -1
- package/package.json +9 -3
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
type AnyFn = (...args: any[]) => any;
|
|
2
|
+
type Noop = () => void;
|
|
3
|
+
type Rec<T = unknown> = Record<string, T>;
|
|
4
|
+
type Key = string | number | symbol;
|
|
5
|
+
type Vdx<T> = T | void;
|
|
6
|
+
type KeyOf<T extends Rec> = keyof T;
|
|
7
|
+
type Entries<T extends Rec> = { [K in KeyOf<T>]: [K, T[K]] }[KeyOf<T>];
|
|
8
|
+
type Label = string;
|
|
9
|
+
type Transition<TState extends Label> = {
|
|
10
|
+
from: '*' | TState | TState[];
|
|
11
|
+
to: TState | ((...args: any[]) => TState | Promise<TState>);
|
|
12
|
+
};
|
|
13
|
+
type Lifecycle<TState extends Label, TEntry extends [Key, Transition<Label>]> = {
|
|
14
|
+
args?: Parameters<Extract<TEntry[1]['to'], AnyFn>>;
|
|
15
|
+
transition: TEntry[0];
|
|
16
|
+
from: TState;
|
|
17
|
+
to: TState;
|
|
18
|
+
};
|
|
19
|
+
type LifecycleMethod<TState extends Label, TTransitions extends Rec<Transition<TState>>> = (lifecycle: Lifecycle<TState, Entries<TTransitions>>) => void;
|
|
20
|
+
type CancelableLifecycleMethod<TState extends Label, TTransitions extends Rec<Transition<TState>>> = (lifecycle: Lifecycle<TState, Entries<TTransitions>>) => Vdx<boolean>;
|
|
21
|
+
type LifecycleMethods<TState extends Label, TTransitions extends Rec<Transition<TState>>> = {
|
|
22
|
+
onBeforeTransition?: CancelableLifecycleMethod<TState, TTransitions>;
|
|
23
|
+
onAfterTransition?: LifecycleMethod<TState, TTransitions>;
|
|
24
|
+
};
|
|
25
|
+
type StateMethods<TState extends Label> = {
|
|
26
|
+
state: () => TState;
|
|
27
|
+
allStates: () => TState[];
|
|
28
|
+
};
|
|
29
|
+
type ApiForPlugin<TState extends Label, TTransitions extends Rec<Transition<TState>>> = {
|
|
30
|
+
init: (listener: (state: TState) => void) => void;
|
|
31
|
+
} & StateMethods<TState> & { [K in KeyOf<LifecycleMethods<TState, TTransitions>>]-?: (listener: LifecycleMethods<TState, TTransitions>[K]) => Noop };
|
|
32
|
+
type PluginApi = {
|
|
33
|
+
name: string;
|
|
34
|
+
api: Rec<AnyFn>;
|
|
35
|
+
};
|
|
36
|
+
type Plugin<TState extends Label = Label, TTransitions extends Rec<Transition<TState>> = Rec<Transition<TState>>> = (api: ApiForPlugin<TState, TTransitions>) => PluginApi;
|
|
37
|
+
export { Transition as a, Rec as c, LifecycleMethods as i, Plugin as n, Label as o, StateMethods as r, KeyOf as s, ApiForPlugin as t };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { a as Transition, c as Rec, o as Label, t as ApiForPlugin } from "./Plugin-C8fnOXt5.js";
|
|
2
|
+
declare const historyPlugin: <TState extends Label, TTransitions extends Rec<Transition<TState>>>() => (api: ApiForPlugin<TState, TTransitions>) => {
|
|
3
|
+
name: "history";
|
|
4
|
+
api: {
|
|
5
|
+
get(): Label[];
|
|
6
|
+
back(steps: number): Label;
|
|
7
|
+
forward(steps: number): Label;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
export { historyPlugin as fsmHistoryPlugin };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
let t=()=>t=>{let i=[],n=0;return t.init(t=>i.push(t)),t.onAfterTransition(({to:t})=>{n++,i.splice(n,i.length-n,t)}),{name:"history",api:{get:()=>i,back:t=>i[n=Math.max(0,n-t)],forward:t=>(n=Math.min(i.length-1,n+t),i[n])}}};export{t as fsmHistoryPlugin};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,40 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
type Noop = () => void;
|
|
3
|
-
type Rec<T = unknown> = Record<string, T>;
|
|
4
|
-
type Key = string | number | symbol;
|
|
5
|
-
type Vdx<T> = T | void;
|
|
6
|
-
type KeyOf<T extends Rec> = keyof T;
|
|
1
|
+
import { a as Transition, c as Rec, i as LifecycleMethods, n as Plugin, o as Label, r as StateMethods, s as KeyOf } from "./Plugin-C8fnOXt5.js";
|
|
7
2
|
type EmptyArray = [];
|
|
8
|
-
type Entries<T extends Rec> = { [K in KeyOf<T>]: [K, T[K]] }[KeyOf<T>];
|
|
9
|
-
type Label = string;
|
|
10
|
-
type Transition<TState extends Label> = {
|
|
11
|
-
from: '*' | TState | TState[];
|
|
12
|
-
to: TState | ((...args: any[]) => TState | Promise<TState>);
|
|
13
|
-
};
|
|
14
|
-
type Lifecycle<TState extends Label, TEntry extends [Key, Transition<Label>]> = {
|
|
15
|
-
args?: Parameters<Extract<TEntry[1]['to'], AnyFn>>;
|
|
16
|
-
transition: TEntry[0];
|
|
17
|
-
from: TState;
|
|
18
|
-
to: TState;
|
|
19
|
-
};
|
|
20
|
-
type LifecycleMethod<TState extends Label, TTransitions extends Rec<Transition<TState>>> = (lifecycle: Lifecycle<TState, Entries<TTransitions>>) => void;
|
|
21
|
-
type CancelableLifecycleMethod<TState extends Label, TTransitions extends Rec<Transition<TState>>> = (lifecycle: Lifecycle<TState, Entries<TTransitions>>) => Vdx<boolean>;
|
|
22
|
-
type LifecycleMethods<TState extends Label, TTransitions extends Rec<Transition<TState>>> = {
|
|
23
|
-
onBeforeTransition?: CancelableLifecycleMethod<TState, TTransitions>;
|
|
24
|
-
onAfterTransition?: LifecycleMethod<TState, TTransitions>;
|
|
25
|
-
};
|
|
26
|
-
type StateMethods<TState extends Label> = {
|
|
27
|
-
state: () => TState;
|
|
28
|
-
allStates: () => TState[];
|
|
29
|
-
};
|
|
30
|
-
type ApiForPlugin<TState extends Label, TTransitions extends Rec<Transition<TState>>> = {
|
|
31
|
-
init: (listener: (state: TState) => void) => void;
|
|
32
|
-
} & StateMethods<TState> & { [K in KeyOf<LifecycleMethods<TState, TTransitions>>]-?: (listener: LifecycleMethods<TState, TTransitions>[K]) => Noop };
|
|
33
|
-
type PluginApi = {
|
|
34
|
-
name: string;
|
|
35
|
-
api: Rec<AnyFn>;
|
|
36
|
-
};
|
|
37
|
-
type Plugin<TState extends Label = Label, TTransitions extends Rec<Transition<TState>> = Rec<Transition<TState>>> = (api: ApiForPlugin<TState, TTransitions>) => PluginApi;
|
|
38
3
|
type Config<TState extends Label, TTransitions extends Rec<Transition<TState>>, TPlugins extends Array<Plugin<TState, TTransitions>> = EmptyArray> = {
|
|
39
4
|
init: TState;
|
|
40
5
|
states: TState[];
|
|
@@ -48,12 +13,4 @@ type PluginsMethods<TState extends Label, TTransitions extends Rec<Transition<TS
|
|
|
48
13
|
}>['api'] };
|
|
49
14
|
type Methods<TState extends Label, TTransitions extends Rec<Transition<TState>>, TPlugins extends Array<Plugin<TState, TTransitions>>> = TransitionMethods<TTransitions> & StateMethods<TState> & PluginsMethods<TState, TTransitions, TPlugins>;
|
|
50
15
|
declare const makeFsm: <TState extends Label, TTransitions extends Rec<Transition<TState>>, TPlugins extends Array<Plugin<TState, TTransitions>>>(config: Config<TState, TTransitions, TPlugins>) => Methods<TState, TTransitions, TPlugins>;
|
|
51
|
-
|
|
52
|
-
name: "history";
|
|
53
|
-
api: {
|
|
54
|
-
get(): Label[];
|
|
55
|
-
back(steps: number): Label;
|
|
56
|
-
forward(steps: number): Label;
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
export { type Config as FsmConfig, type Label as FsmLabel, type Plugin as FsmPlugin, type Transition as FsmTransition, historyPlugin as fsmHistoryPlugin, makeFsm };
|
|
16
|
+
export { type Config as FsmConfig, type Label as FsmLabel, type Plugin as FsmPlugin, type Transition as FsmTransition, makeFsm };
|
package/dist/index.js
CHANGED
|
@@ -1 +1,4 @@
|
|
|
1
|
-
let t={nlx:t=>null===t,ulx:t=>void 0===t,nil:e=>t.nlx(e)||t.ulx(e),not:{nlx:t=>null!==t,ulx:t=>void 0!==t,nil:e=>t.not.nlx(e)&&t.not.ulx(e)},array:t=>Array.isArray(t),string:t=>"string"==typeof t,function:t=>"function"==typeof t,promise:t=>t instanceof Promise,boolean:t=>"boolean"==typeof t,false:t=>!1===t,true:t=>!0===t};function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function r(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,n)}return r}function n(t){for(var n=1;n<arguments.length;n++){var i=null!=arguments[n]?arguments[n]:{};n%2?r(Object(i),!0).forEach(function(r){!function(t,r,n){var i;(i=function(t,r){if("object"!=e(t)||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var i=n.call(t,r||"default");if("object"!=e(i))return i;throw TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}(r,"string"),(r="symbol"==e(i)?i:i+"")in t)?Object.defineProperty(t,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[r]=n}(t,r,i[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(i)):r(Object(i)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(i,e))})}return t}let i=e=>{var r,i,o,l;let s,a,u,f,m,
|
|
1
|
+
let t={nlx:t=>null===t,ulx:t=>void 0===t,nil:e=>t.nlx(e)||t.ulx(e),not:{nlx:t=>null!==t,ulx:t=>void 0!==t,nil:e=>t.not.nlx(e)&&t.not.ulx(e)},array:t=>Array.isArray(t),string:t=>"string"==typeof t,function:t=>"function"==typeof t,promise:t=>t instanceof Promise,boolean:t=>"boolean"==typeof t,false:t=>!1===t,true:t=>!0===t};function e(t){return(e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function r(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,n)}return r}function n(t){for(var n=1;n<arguments.length;n++){var i=null!=arguments[n]?arguments[n]:{};n%2?r(Object(i),!0).forEach(function(r){!function(t,r,n){var i;(i=function(t,r){if("object"!=e(t)||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var i=n.call(t,r||"default");if("object"!=e(i))return i;throw TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}(r,"string"),(r="symbol"==e(i)?i:i+"")in t)?Object.defineProperty(t,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[r]=n}(t,r,i[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(i)):r(Object(i)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(i,e))})}return t}let i=e=>{var r,i,o,l;let s,a,u,f,c,m,b,p,y,g=e.init,v=(s=new Map,{listen(t,e){var r;return s.has(t)||s.set(t,[]),null==(r=s.get(t))||r.push(e),()=>{this.unlisten(t,e)}},unlisten(t,e){let r=s.get(t);if(!r)return;let n=r.filter(t=>t!==e);n.length?s.set(t,n):s.delete(t)},emit(e,...r){var n,i;return null!=(n=null==(i=s.get(e))?void 0:i.map(t=>t(...r)).filter(t.not.ulx))?n:[]},unlistenAll(t){s.delete(t)}});Object.entries(null!=(r=e.methods)?r:{}).forEach(([t,e])=>{v.listen(t,e)}),v.listen("onAfterTransition",({to:t})=>{g=t}),v.listen("error",t=>{throw Error(`[FSM]: ${t}`)});let O={state:()=>g,allStates:()=>[...e.states]},d=(i=e.transitions,o=O.state,u={},c=f={register(e,r){let n=r=>{r.to===r.from?v.emit("warn",`
|
|
2
|
+
Transition: "${e}" is canceled because it's circular.
|
|
3
|
+
Current state is ${r.from}. Transition target state is ${r.to}
|
|
4
|
+
`):v.emit("onBeforeTransition",r).filter(t.boolean).every(t.true)&&v.emit("onAfterTransition",r)};return u[e]=(...i)=>{if(t.array(r.from)?!r.from.includes(o()):"*"!==r.from&&r.from!==o())return v.emit("error",`Transition: "${e}" is forbidden`),o();if(a)return v.emit("error",`Transition: "${e}" can't be made. Has pending transtion: "${a}"`),o();if(!t.function(r.to))return n({transition:e,from:o(),to:r.to}),o();let l=r.to(...i);return t.promise(l)?(a=e,l.then(t=>(a=void 0,n({transition:e,from:o(),to:t,args:i}),o()))):(n({transition:e,from:o(),to:l,args:i}),o())},f},make:()=>u},Object.entries(i).forEach(([t,e])=>c.register(t,e)),c.make()),h=(l=e.plugins,m={},b=n({init(t){v.listen("init",t)},onBeforeTransition:t=>v.listen("onBeforeTransition",t),onAfterTransition:t=>v.listen("onAfterTransition",t)},O),y=p={register(t){let{name:e,api:r}=t(b);return e in m&&v.emit("error",`There are at least two plugins with the same name: ${e}`),m[e]=r,p},make:()=>m},(null!=l?l:[]).forEach(y.register),y.make());return v.emit("init",g),n(n(n({},O),h),d)};export{i as makeFsm};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@uuxxx/fsm",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"author": "Artem Tryapichnikov <golysheeet@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Javascript library for creating finite state machine",
|
|
@@ -29,8 +29,14 @@
|
|
|
29
29
|
"LICENSE"
|
|
30
30
|
],
|
|
31
31
|
"exports": {
|
|
32
|
-
"
|
|
33
|
-
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"default": "./dist/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./history-plugin": {
|
|
37
|
+
"types": "./dist/history-plugin.d.ts",
|
|
38
|
+
"default": "./dist/history-plugin.js"
|
|
39
|
+
}
|
|
34
40
|
},
|
|
35
41
|
"devDependencies": {
|
|
36
42
|
"@babel/core": "^7.28.4",
|