jssm 5.61.1 → 5.61.4
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 +33 -12
- package/dist/es6/jssm.d.ts +2 -0
- package/dist/es6/jssm.js +38 -18
- package/dist/es6/version.js +1 -1
- package/dist/jssm.es5.cjs.js +1 -1
- package/dist/jssm.es5.iife.js +1 -1
- package/jssm.d.ts +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -42,18 +42,39 @@ log( TrafficLight.state() ); // 'Green'
|
|
|
42
42
|
What if the notation supported action names easily?
|
|
43
43
|
|
|
44
44
|
```javascript
|
|
45
|
-
const
|
|
45
|
+
const TLWA = sm`Red 'next' -> Green 'next' -> Yellow 'next' -> Red;`; // TLWA = Traffic Light With Actions
|
|
46
46
|
|
|
47
|
-
log(
|
|
47
|
+
log( TLWA.state() ); // 'Red'
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
log(
|
|
49
|
+
TLWA.action('next'); // true
|
|
50
|
+
log( TLWA.state() ); // 'Green'
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
log(
|
|
52
|
+
TLWA.action('next'); // true
|
|
53
|
+
log( TLWA.state() ); // 'Yellow'
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
log(
|
|
55
|
+
TLWA.action('next'); // true
|
|
56
|
+
log( TLWA.state() ); // 'Red'
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
<br/>
|
|
60
|
+
|
|
61
|
+
What if integration with the outside was straightforward?
|
|
62
|
+
|
|
63
|
+
```javascript
|
|
64
|
+
const MTL = sm`Red 'next' -> Green 'next' -> Yellow 'next' -> Red;` // MTL = More Traffic Lights
|
|
65
|
+
.hook('Red', 'Green', () => console.log('GO GO GO')) // node will jump the gun when you hit return, though
|
|
66
|
+
.hook_entry('Red', () => console.log('STOP')); // so put it on one line in node
|
|
67
|
+
|
|
68
|
+
log( MTL.state() ); // 'Red'
|
|
69
|
+
|
|
70
|
+
TLWA.action('next'); // true, console logs 'GO GO GO'
|
|
71
|
+
log( TLWA.state() ); // 'Green'
|
|
72
|
+
|
|
73
|
+
TLWA.action('next'); // true
|
|
74
|
+
log( TLWA.state() ); // 'Yellow'
|
|
75
|
+
|
|
76
|
+
TLWA.action('next'); // true, console logs 'STOP'
|
|
77
|
+
log( TLWA.state() ); // 'Red'
|
|
57
78
|
```
|
|
58
79
|
|
|
59
80
|
<br/>
|
|
@@ -61,11 +82,11 @@ log( TrafficLightWithActions.state() ); // 'Red'
|
|
|
61
82
|
What if the machine followed JS standards, and distinguished refusals as `false` from mistakes as `throw`n?
|
|
62
83
|
|
|
63
84
|
```javascript
|
|
64
|
-
const
|
|
85
|
+
const ATL = sm`Red -> Green -> Yellow -> Red;`; // ATL = Another Traffic Light
|
|
65
86
|
|
|
66
|
-
log(
|
|
67
|
-
|
|
68
|
-
|
|
87
|
+
log( ATL.state() ); // 'Red' - uses 1st state unless told otherwise
|
|
88
|
+
ATL.transition('Yellow'); // false (Yellow isn't allowed from Red)
|
|
89
|
+
ATL.transition('Blue'); // throws (Blue isn't a state at all)
|
|
69
90
|
```
|
|
70
91
|
|
|
71
92
|
<br/>
|
package/dist/es6/jssm.d.ts
CHANGED
|
@@ -42,6 +42,8 @@ declare class Machine<mDT> {
|
|
|
42
42
|
_has_named_hooks: boolean;
|
|
43
43
|
_has_entry_hooks: boolean;
|
|
44
44
|
_has_exit_hooks: boolean;
|
|
45
|
+
_has_global_action_hooks: boolean;
|
|
46
|
+
_has_transition_hooks: boolean;
|
|
45
47
|
_hooks: Map<string, Function>;
|
|
46
48
|
_named_hooks: Map<string, Function>;
|
|
47
49
|
_entry_hooks: Map<string, Function>;
|
package/dist/es6/jssm.js
CHANGED
|
@@ -350,6 +350,8 @@ class Machine {
|
|
|
350
350
|
this._has_named_hooks = false;
|
|
351
351
|
this._has_entry_hooks = false;
|
|
352
352
|
this._has_exit_hooks = false;
|
|
353
|
+
this._has_global_action_hooks = false;
|
|
354
|
+
this._has_transition_hooks = true;
|
|
353
355
|
// no need for a boolean has any transition hook, as it's one or nothing, so just test that for undefinedness
|
|
354
356
|
this._hooks = new Map();
|
|
355
357
|
this._named_hooks = new Map();
|
|
@@ -719,14 +721,17 @@ class Machine {
|
|
|
719
721
|
case 'hook':
|
|
720
722
|
this._hooks.set(hook_name(HookDesc.from, HookDesc.to), HookDesc.handler);
|
|
721
723
|
this._has_hooks = true;
|
|
724
|
+
this._has_basic_hooks = true;
|
|
722
725
|
break;
|
|
723
726
|
case 'named':
|
|
724
727
|
this._named_hooks.set(named_hook_name(HookDesc.from, HookDesc.to, HookDesc.action), HookDesc.handler);
|
|
725
728
|
this._has_hooks = true;
|
|
729
|
+
this._has_named_hooks = true;
|
|
726
730
|
break;
|
|
727
731
|
case 'global action':
|
|
728
732
|
this._global_action_hooks.set(HookDesc.action, HookDesc.handler);
|
|
729
733
|
this._has_hooks = true;
|
|
734
|
+
this._has_global_action_hooks = true;
|
|
730
735
|
break;
|
|
731
736
|
case 'any action':
|
|
732
737
|
this._any_action_hook = HookDesc.handler;
|
|
@@ -734,14 +739,17 @@ class Machine {
|
|
|
734
739
|
break;
|
|
735
740
|
case 'standard transition':
|
|
736
741
|
this._standard_transition_hook = HookDesc.handler;
|
|
742
|
+
this._has_transition_hooks = true;
|
|
737
743
|
this._has_hooks = true;
|
|
738
744
|
break;
|
|
739
745
|
case 'main transition':
|
|
740
746
|
this._main_transition_hook = HookDesc.handler;
|
|
747
|
+
this._has_transition_hooks = true;
|
|
741
748
|
this._has_hooks = true;
|
|
742
749
|
break;
|
|
743
750
|
case 'forced transition':
|
|
744
751
|
this._forced_transition_hook = HookDesc.handler;
|
|
752
|
+
this._has_transition_hooks = true;
|
|
745
753
|
this._has_hooks = true;
|
|
746
754
|
break;
|
|
747
755
|
case 'any transition':
|
|
@@ -751,10 +759,12 @@ class Machine {
|
|
|
751
759
|
case 'entry':
|
|
752
760
|
this._entry_hooks.set(HookDesc.to, HookDesc.handler);
|
|
753
761
|
this._has_hooks = true;
|
|
762
|
+
this._has_entry_hooks = true;
|
|
754
763
|
break;
|
|
755
764
|
case 'exit':
|
|
756
765
|
this._exit_hooks.set(HookDesc.from, HookDesc.handler);
|
|
757
766
|
this._has_hooks = true;
|
|
767
|
+
this._has_exit_hooks = true;
|
|
758
768
|
break;
|
|
759
769
|
default:
|
|
760
770
|
console.log(`Unknown hook type ${HookDesc.kind}, should be impossible`);
|
|
@@ -838,8 +848,10 @@ class Machine {
|
|
|
838
848
|
}
|
|
839
849
|
else {
|
|
840
850
|
if (this.valid_transition(newStateOrAction, newData)) {
|
|
851
|
+
if (this._has_transition_hooks) {
|
|
852
|
+
trans_type = this.edges_between(this._state, newStateOrAction)[0].kind; // TODO this won't do the right thing if various edges have different types
|
|
853
|
+
}
|
|
841
854
|
valid = true;
|
|
842
|
-
trans_type = this.edges_between(this._state, newStateOrAction)[0].kind; // TODO this won't do the right thing if various edges have different types
|
|
843
855
|
newState = newStateOrAction;
|
|
844
856
|
}
|
|
845
857
|
}
|
|
@@ -869,26 +881,32 @@ class Machine {
|
|
|
869
881
|
}
|
|
870
882
|
}
|
|
871
883
|
// 4. exit hook
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
if (maybe_ex_hook
|
|
875
|
-
|
|
884
|
+
if (this._has_exit_hooks) {
|
|
885
|
+
const maybe_ex_hook = this._exit_hooks.get(this._state);
|
|
886
|
+
if (maybe_ex_hook !== undefined) {
|
|
887
|
+
if (maybe_ex_hook({ from: this._state, forced: wasForced }) === false) {
|
|
888
|
+
return false;
|
|
889
|
+
}
|
|
876
890
|
}
|
|
877
891
|
}
|
|
878
892
|
// 5. named transition / action hook
|
|
879
|
-
if (
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
if (n_maybe_hook
|
|
883
|
-
|
|
893
|
+
if (this._has_named_hooks) {
|
|
894
|
+
if (wasAction) {
|
|
895
|
+
const nhn = named_hook_name(this._state, newState, newStateOrAction), n_maybe_hook = this._named_hooks.get(nhn);
|
|
896
|
+
if (n_maybe_hook !== undefined) {
|
|
897
|
+
if (n_maybe_hook({ from: this._state, to: newState, action: newStateOrAction }) === false) {
|
|
898
|
+
return false;
|
|
899
|
+
}
|
|
884
900
|
}
|
|
885
901
|
}
|
|
886
902
|
}
|
|
887
903
|
// 6. regular hook
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
if (maybe_hook
|
|
891
|
-
|
|
904
|
+
if (this._has_basic_hooks) {
|
|
905
|
+
const hn = hook_name(this._state, newState), maybe_hook = this._hooks.get(hn);
|
|
906
|
+
if (maybe_hook !== undefined) {
|
|
907
|
+
if (maybe_hook({ from: this._state, to: newState, forced: wasForced, action: wasAction ? newStateOrAction : undefined }) === false) {
|
|
908
|
+
return false;
|
|
909
|
+
}
|
|
892
910
|
}
|
|
893
911
|
}
|
|
894
912
|
// 7. edge type hook
|
|
@@ -923,10 +941,12 @@ class Machine {
|
|
|
923
941
|
}
|
|
924
942
|
}
|
|
925
943
|
// 8. entry hook
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
if (maybe_en_hook
|
|
929
|
-
|
|
944
|
+
if (this._has_entry_hooks) {
|
|
945
|
+
const maybe_en_hook = this._entry_hooks.get(newState);
|
|
946
|
+
if (maybe_en_hook !== undefined) {
|
|
947
|
+
if (maybe_en_hook({ to: newState, forced: wasForced }) === false) {
|
|
948
|
+
return false;
|
|
949
|
+
}
|
|
930
950
|
}
|
|
931
951
|
}
|
|
932
952
|
this._state = newState;
|
package/dist/es6/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const version = "5.61.
|
|
1
|
+
const version = "5.61.4";
|
|
2
2
|
export { version };
|