@uuxxx/fsm 1.3.0 → 1.4.0
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/README.md +22 -16
- package/dist/index.d.ts +7 -5
- package/dist/index.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ type State = 'idle' | 'loading' | 'success' | 'error';
|
|
|
32
32
|
|
|
33
33
|
const fsm = makeFsm({
|
|
34
34
|
init: 'idle',
|
|
35
|
-
states: ['idle', 'loading', 'success', 'error']
|
|
35
|
+
states: ['idle', 'loading', 'success', 'error'],
|
|
36
36
|
transitions: {
|
|
37
37
|
start: {
|
|
38
38
|
from: 'idle',
|
|
@@ -285,11 +285,13 @@ Plugin names must be unique — registering two plugins with the same name trigg
|
|
|
285
285
|
|
|
286
286
|
### History Plugin
|
|
287
287
|
|
|
288
|
-
|
|
288
|
+
Read-only state history tracking with pointer-based navigation.
|
|
289
|
+
|
|
290
|
+
`back()` and `forward()` move an internal pointer and return the state at that position — they do **not** change the FSM state. Use transition methods to actually navigate (e.g. `fsm.goto(fsm.history.back(1))`).
|
|
289
291
|
|
|
290
292
|
```typescript
|
|
291
293
|
import { makeFsm } from '@uuxxx/fsm';
|
|
292
|
-
import {
|
|
294
|
+
import { historyPlugin } from '@uuxxx/fsm-plugins/history';
|
|
293
295
|
|
|
294
296
|
const fsm = makeFsm({
|
|
295
297
|
init: 'a',
|
|
@@ -297,27 +299,31 @@ const fsm = makeFsm({
|
|
|
297
299
|
transitions: {
|
|
298
300
|
goto: { from: '*', to: (s: 'a' | 'b' | 'c') => s },
|
|
299
301
|
},
|
|
300
|
-
plugins: [
|
|
302
|
+
plugins: [historyPlugin()],
|
|
301
303
|
});
|
|
302
304
|
|
|
303
305
|
fsm.goto('b');
|
|
304
306
|
fsm.goto('c');
|
|
305
|
-
fsm.history.get(); // ['a', 'b', 'c']
|
|
306
|
-
|
|
307
|
-
fsm.history.back(1); // returns 'b'
|
|
308
|
-
fsm.history.
|
|
309
|
-
fsm.history.
|
|
307
|
+
fsm.history.get(); // ['a', 'b', 'c'] (returns a copy)
|
|
308
|
+
|
|
309
|
+
fsm.history.back(1); // returns 'b' (pointer moved, FSM state unchanged)
|
|
310
|
+
fsm.history.current(); // 'b'
|
|
311
|
+
fsm.history.canBack(); // true
|
|
312
|
+
fsm.history.canForward(); // true
|
|
313
|
+
fsm.history.forward(1); // returns 'c'
|
|
314
|
+
fsm.goto(fsm.history.current()); // actually transition to 'c'
|
|
310
315
|
```
|
|
311
316
|
|
|
312
317
|
#### History API
|
|
313
318
|
|
|
314
|
-
| Method | Returns | Description
|
|
315
|
-
| ---------------------------- | ---------- |
|
|
316
|
-
| `fsm.history.get()` | `TState[]` |
|
|
317
|
-
| `fsm.history.
|
|
318
|
-
| `fsm.history.
|
|
319
|
-
|
|
320
|
-
|
|
319
|
+
| Method | Returns | Description |
|
|
320
|
+
| ---------------------------- | ---------- | --------------------------------------------------------------------------------------------------------------- |
|
|
321
|
+
| `fsm.history.get()` | `TState[]` | Returns a copy of the full history array |
|
|
322
|
+
| `fsm.history.current()` | `TState` | Returns the state at the current pointer position |
|
|
323
|
+
| `fsm.history.back(steps)` | `TState` | Move pointer back by `steps`, returns the state at that position. Clamps to start. Ignores non-positive values |
|
|
324
|
+
| `fsm.history.forward(steps)` | `TState` | Move pointer forward by `steps`, returns the state at that position. Clamps to end. Ignores non-positive values |
|
|
325
|
+
| `fsm.history.canBack()` | `boolean` | Whether the pointer can move back (pointer > 0) |
|
|
326
|
+
| `fsm.history.canForward()` | `boolean` | Whether the pointer can move forward (pointer < end) |
|
|
321
327
|
|
|
322
328
|
When a transition occurs, any forward history after the current pointer is discarded (like browser navigation).
|
|
323
329
|
|
package/dist/index.d.ts
CHANGED
|
@@ -29,6 +29,8 @@ type CancelableLifecycleMethod<TState extends Label, TTransitions extends Rec<Tr
|
|
|
29
29
|
type LifecycleMethods<TState extends Label, TTransitions extends Rec<Transition<TState>>> = {
|
|
30
30
|
onBeforeTransition?: CancelableLifecycleMethod<TState, TTransitions>;
|
|
31
31
|
onAfterTransition?: LifecycleMethod<TState, TTransitions>;
|
|
32
|
+
onError?: (msg: string, lifecycle?: Lifecycle<TState, Entries<TTransitions>>) => void;
|
|
33
|
+
onWarn?: (msg: string, lifecycle: Lifecycle<TState, Entries<TTransitions>>) => void;
|
|
32
34
|
};
|
|
33
35
|
type StateMethods<TState extends Label> = {
|
|
34
36
|
state: () => TState;
|
|
@@ -37,8 +39,9 @@ type StateMethods<TState extends Label> = {
|
|
|
37
39
|
/** API object passed to each plugin during registration. Provides state access, lifecycle hooks, and error listeners. */
|
|
38
40
|
type ApiForPlugin<TState extends Label, TTransitions extends Rec<Transition<TState>>> = {
|
|
39
41
|
init: (listener: (state: TState) => void) => void;
|
|
40
|
-
onError: (listener: (msg: string) => void) => Noop;
|
|
41
|
-
|
|
42
|
+
onError: (listener: (msg: string, lifecycle?: Lifecycle<TState, Entries<TTransitions>>) => void) => Noop;
|
|
43
|
+
onWarn: (listener: (msg: string, lifecycle: Lifecycle<TState, Entries<TTransitions>>) => void) => Noop;
|
|
44
|
+
} & StateMethods<TState> & { [K in Exclude<KeyOf<LifecycleMethods<TState, TTransitions>>, 'onError' | 'onWarn'>]-?: (listener: LifecycleMethods<TState, TTransitions>[K]) => Noop };
|
|
42
45
|
type PluginApi = {
|
|
43
46
|
name: string;
|
|
44
47
|
api: Rec<AnyFn>;
|
|
@@ -53,10 +56,9 @@ type Plugin<TState extends Label = Label, TTransitions extends Rec<Transition<TS
|
|
|
53
56
|
type Config<TState extends Label, TTransitions extends Rec<Transition<TState>>, TPlugins extends Array<Plugin<TState, TTransitions>> = EmptyArray> = {
|
|
54
57
|
/** Initial state of the FSM. */init: TState; /** All valid states. The FSM will reject transitions to states not in this list. */
|
|
55
58
|
states: TState[]; /** Transition definitions. Each key becomes a method on the FSM instance. */
|
|
56
|
-
transitions: TTransitions; /** Optional lifecycle hooks (`onBeforeTransition`, `onAfterTransition`). */
|
|
59
|
+
transitions: TTransitions; /** Optional lifecycle hooks (`onBeforeTransition`, `onAfterTransition`, `onError`). */
|
|
57
60
|
methods?: LifecycleMethods<TState, TTransitions>; /** Optional plugins to extend the FSM with additional APIs. */
|
|
58
|
-
plugins?: TPlugins;
|
|
59
|
-
onError?: (msg: string) => void;
|
|
61
|
+
plugins?: TPlugins;
|
|
60
62
|
};
|
|
61
63
|
type TransitionMethods<TTransitions extends Rec<Transition<Label>>> = { [K in KeyOf<TTransitions>]: TTransitions[K]['to'] extends Label ? () => TTransitions[K]['to'] : TTransitions[K]['to'] };
|
|
62
64
|
type PluginsMethods<TState extends Label, TTransitions extends Rec<Transition<TState>>, TPlugins extends Array<Plugin<TState, TTransitions>>> = { [K in ReturnType<TPlugins[number]>['name']]: Extract<ReturnType<TPlugins[number]>, {
|
package/dist/index.js
CHANGED
|
@@ -1,4 +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,
|
|
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=["onError","onWarn"],o=e=>{var r,o,l;let s,a,u,f,c,m,b=e.init,p=e.states.includes(e.init)?[...e.states]:[...e.states,e.init],y=(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)}}),g=null!=(r=e.methods)?r:{},{onError:v,onWarn:O}=g;Object.entries(function(t,e){if(null==t)return{};var r,n,i=function(t,e){if(null==t)return{};var r={};for(var n in t)if(({}).hasOwnProperty.call(t,n)){if(e.includes(n))continue;r[n]=t[n]}return r}(t,e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(t);for(n=0;n<o.length;n++)r=o[n],e.includes(r)||({}).propertyIsEnumerable.call(t,r)&&(i[r]=t[r])}return i}(g,i)).forEach(([t,e])=>{y.listen(t,e)}),y.listen("onAfterTransition",({to:t})=>{b=t}),y.listen("error",null!=v?v:t=>{throw Error(`[FSM]: ${t}`)}),O&&y.listen("warn",O);let d={state:()=>b,allStates:()=>p},h=(o=e.transitions,a=((e,{state:r,allStates:n})=>{let i,o={},l={register(s,a){let u=r=>{n().includes(r.to)?r.to===r.from?e.emit("warn",`
|
|
2
2
|
Transition: "${s}" is canceled because it's circular.
|
|
3
3
|
Current state is ${r.from}. Transition target state is ${r.to}
|
|
4
|
-
|
|
4
|
+
`,r):e.emit("onBeforeTransition",r).filter(t.boolean).every(t.true)&&e.emit("onAfterTransition",r):e.emit("error",`Transition: "${s}" can't be executed. It has invalid "to": "${r.to}"`,r)};return o[s]=(...n)=>{if(t.array(a.from)?!a.from.includes(r()):"*"!==a.from&&a.from!==r())return e.emit("error",`Transition: "${s}" is forbidden`),r();if(i)return e.emit("error",`Transition: "${s}" can't be made. Has pending transtion: "${i}"`),r();if(!t.function(a.to))return u({transition:s,from:r(),to:a.to}),r();let o=a.to(...n);return t.promise(o)?(i=s,o.then(t=>(i=void 0,u({transition:s,from:r(),to:t,args:n}),r()))):(u({transition:s,from:r(),to:o,args:n}),r())},l},make:()=>o};return l})(y,d),Object.entries(o).forEach(([t,e])=>a.register(t,e)),a.make()),j=(l=e.plugins,u={},f=n({init(t){y.listen("init",t)},onError:t=>y.listen("error",t),onWarn:t=>y.listen("warn",t),onBeforeTransition:t=>y.listen("onBeforeTransition",t),onAfterTransition:t=>y.listen("onAfterTransition",t)},d),m=c={register(t){let{name:e,api:r}=t(f);return e in u&&y.emit("error",`There are at least two plugins with the same name: "${e}"`),u[e]=r,c},make:()=>u},(null!=l?l:[]).forEach(m.register),m.make());return y.emit("init",b),n(n(n({},d),j),h)};export{o as makeFsm};
|