jssm 5.47.0 → 5.50.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.
@@ -38,8 +38,11 @@ declare class Machine<mDT> {
38
38
  _theme: FslTheme;
39
39
  _flow: FslDirection;
40
40
  _has_hooks: boolean;
41
+ _has_basic_hooks: boolean;
42
+ _has_named_hooks: boolean;
41
43
  _hooks: Map<string, Function>;
42
44
  _named_hooks: Map<string, Function>;
45
+ _any_transition_hook: HookHandler | undefined;
43
46
  constructor({ start_states, complete, transitions, machine_author, machine_comment, machine_contributor, machine_definition, machine_language, machine_license, machine_name, machine_version, state_declaration, fsl_version, dot_preamble, arrange_declaration, arrange_start_declaration, arrange_end_declaration, theme, flow, graph_layout }: JssmGenericConfig<mDT>);
44
47
  _new_state(state_config: JssmGenericState): StateType;
45
48
  state(): StateType;
@@ -92,7 +95,9 @@ declare class Machine<mDT> {
92
95
  set_hook(HookDesc: HookDescription): void;
93
96
  hook(from: string, to: string, handler: HookHandler): Machine<mDT>;
94
97
  hook_action(from: string, to: string, action: string, handler: HookHandler): Machine<mDT>;
98
+ hook_any_transition(handler: HookHandler): Machine<mDT>;
95
99
  action(name: StateType, newData?: mDT): boolean;
100
+ transition_impl(newState: StateType, newData: mDT | undefined, wasForced: boolean, wasAction: boolean): boolean;
96
101
  transition(newState: StateType, newData?: mDT): boolean;
97
102
  force_transition(newState: StateType, newData?: mDT): boolean;
98
103
  current_action_for(action: StateType): number;
package/dist/es6/jssm.js CHANGED
@@ -152,13 +152,13 @@ function makeTransition(this_se, from, to, isRight, _wasList, _wasIndex) {
152
152
  // if ((wasIndex !== undefined) && (wasList === undefined)) { throw new TypeError("Must be in a list if transition has an index"); }
153
153
  /*
154
154
  if (typeof edge.to === 'object') {
155
-
155
+
156
156
  if (edge.to.key === 'cycle') {
157
157
  if (wasList === undefined) { throw "Must have a waslist if a to is type cycle"; }
158
158
  const nextIndex = wrapBy(wasIndex, edge.to.value, wasList.length);
159
159
  edge.to = wasList[nextIndex];
160
160
  }
161
-
161
+
162
162
  }
163
163
  */
164
164
  const action = isRight ? 'r_action' : 'l_action', probability = isRight ? 'r_probability' : 'l_probability';
@@ -346,8 +346,11 @@ class Machine {
346
346
  this._flow = flow;
347
347
  this._graph_layout = graph_layout;
348
348
  this._has_hooks = false;
349
+ this._has_basic_hooks = false;
350
+ this._has_named_hooks = false;
349
351
  this._hooks = new Map();
350
352
  this._named_hooks = new Map();
353
+ this._any_transition_hook = undefined;
351
354
  if (state_declaration) {
352
355
  state_declaration.map((state_decl) => {
353
356
  if (this._state_declarations.has(state_decl.state)) { // no repeats
@@ -456,7 +459,7 @@ class Machine {
456
459
  }
457
460
  /* whargarbl todo major
458
461
  when we reimplement this, reintroduce this change to the is_final call
459
-
462
+
460
463
  is_changing(): boolean {
461
464
  return true; // todo whargarbl
462
465
  }
@@ -655,7 +658,8 @@ class Machine {
655
658
  return Array.from(ra_base.values())
656
659
  .map((edgeId) => this._edges[edgeId])
657
660
  .filter((o) => o.from === whichState)
658
- .map((filtered) => ({ action: filtered.action,
661
+ .map((filtered) => ({
662
+ action: filtered.action,
659
663
  probability: filtered.probability
660
664
  }));
661
665
  }
@@ -709,6 +713,10 @@ class Machine {
709
713
  this._named_hooks.set(named_hook_name(HookDesc.from, HookDesc.to, HookDesc.action), HookDesc.handler);
710
714
  this._has_hooks = true;
711
715
  break;
716
+ case 'any transition':
717
+ this._any_transition_hook = HookDesc.handler;
718
+ this._has_hooks = true;
719
+ break;
712
720
  // case 'entry':
713
721
  // console.log('TODO: Should add entry hook here');
714
722
  // throw 'TODO: Should add entry hook here';
@@ -730,6 +738,11 @@ class Machine {
730
738
  this.set_hook({ kind: 'named', from, to, action, handler });
731
739
  return this;
732
740
  }
741
+ hook_any_transition(handler) {
742
+ // TODO: should this throw if setting the hook fails, or ignore it and continue?
743
+ this.set_hook({ kind: 'any transition', handler });
744
+ return this;
745
+ }
733
746
  // remove_hook(HookDesc: HookDescription) {
734
747
  // throw 'TODO: Should remove hook here';
735
748
  // }
@@ -741,6 +754,11 @@ class Machine {
741
754
  const edge = this.current_action_edge_for(name);
742
755
  if (this._has_hooks) {
743
756
  let hook_permits = undefined;
757
+ if (this._any_transition_hook !== undefined) {
758
+ if (this._any_transition_hook() === false) {
759
+ return false;
760
+ }
761
+ }
744
762
  const nhn = named_hook_name(this._state, edge.to, name), maybe_hook = this._named_hooks.get(nhn);
745
763
  if (maybe_hook === undefined) {
746
764
  hook_permits = true;
@@ -765,51 +783,34 @@ class Machine {
765
783
  return false;
766
784
  }
767
785
  }
768
- transition(newState, newData) {
769
- // todo whargarbl implement hooks
770
- // todo whargarbl implement data stuff
771
- // todo major incomplete whargarbl comeback
772
- if (this.valid_transition(newState, newData)) {
773
- if (this._has_hooks) {
774
- let hook_permits = undefined;
775
- const hn = hook_name(this._state, newState), maybe_hook = this._hooks.get(hn);
776
- if (maybe_hook === undefined) {
777
- hook_permits = true;
778
- }
779
- else {
780
- hook_permits = maybe_hook({ from: this._state, to: newState });
781
- }
782
- if (hook_permits !== false) {
783
- this._state = newState;
784
- return true;
785
- }
786
- else {
787
- return false;
788
- }
789
- }
790
- else {
791
- this._state = newState;
792
- return true;
786
+ transition_impl(newState, newData, wasForced, wasAction) {
787
+ let valid = false;
788
+ if (wasForced) {
789
+ if (this.valid_force_transition(newState, newData)) {
790
+ valid = true;
793
791
  }
794
792
  }
795
793
  else {
796
- return false;
794
+ if (this.valid_transition(newState, newData)) {
795
+ valid = true;
796
+ }
797
797
  }
798
- }
799
- // can leave machine in inconsistent state. generally do not use
800
- force_transition(newState, newData) {
801
- // todo whargarbl implement hooks
802
798
  // todo whargarbl implement data stuff
803
799
  // todo major incomplete whargarbl comeback
804
- if (this.valid_force_transition(newState, newData)) {
800
+ if (valid) {
805
801
  if (this._has_hooks) {
806
802
  let hook_permits = undefined;
803
+ if (this._any_transition_hook !== undefined) {
804
+ if (this._any_transition_hook() === false) {
805
+ return false;
806
+ }
807
+ }
807
808
  const hn = hook_name(this._state, newState), maybe_hook = this._hooks.get(hn);
808
809
  if (maybe_hook === undefined) {
809
810
  hook_permits = true;
810
811
  }
811
812
  else {
812
- hook_permits = maybe_hook({ from: this._state, to: newState, forced: true });
813
+ hook_permits = maybe_hook({ from: this._state, to: newState, forced: wasForced });
813
814
  }
814
815
  if (hook_permits !== false) {
815
816
  this._state = newState;
@@ -828,6 +829,13 @@ class Machine {
828
829
  return false;
829
830
  }
830
831
  }
832
+ transition(newState, newData) {
833
+ return this.transition_impl(newState, newData, false, false);
834
+ }
835
+ // can leave machine in inconsistent state. generally do not use
836
+ force_transition(newState, newData) {
837
+ return this.transition_impl(newState, newData, true, false);
838
+ }
831
839
  current_action_for(action) {
832
840
  const action_base = this._actions.get(action);
833
841
  return action_base
@@ -162,6 +162,10 @@ declare type HookDescriptionWithAction = {
162
162
  action: string;
163
163
  handler: HookHandler;
164
164
  };
165
+ declare type AnyTransitionHook = {
166
+ kind: 'any transition';
167
+ handler: HookHandler;
168
+ };
165
169
  declare type EntryHook = {
166
170
  kind: 'entry';
167
171
  to: string;
@@ -172,5 +176,5 @@ declare type ExitHook = {
172
176
  from: string;
173
177
  handler: HookHandler;
174
178
  };
175
- declare type HookDescription = BasicHookDescription | HookDescriptionWithAction | EntryHook | ExitHook;
179
+ declare type HookDescription = BasicHookDescription | HookDescriptionWithAction | AnyTransitionHook | EntryHook | ExitHook;
176
180
  export { JssmColor, JssmTransition, JssmTransitions, JssmTransitionList, JssmTransitionRule, JssmArrow, JssmArrowKind, JssmArrowDirection, JssmGenericConfig, JssmGenericState, JssmGenericMachine, JssmParseTree, JssmCompileSe, JssmCompileSeStart, JssmCompileRule, JssmPermitted, JssmPermittedOpt, JssmResult, JssmStateDeclaration, JssmStateDeclarationRule, JssmLayout, JssmParseFunctionType, JssmMachineInternalState, FslDirection, FslTheme, HookDescription, HookHandler };
@@ -1,2 +1,2 @@
1
- const version = "5.47.0";
1
+ const version = "5.50.0";
2
2
  export { version };