jssm 5.48.0 → 5.51.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>;
95
- action(name: StateType, newData?: mDT): boolean;
98
+ hook_any_transition(handler: HookHandler): Machine<mDT>;
99
+ transition_impl(newStateOrAction: StateType, newData: mDT | undefined, wasForced: boolean, wasAction: boolean): boolean;
100
+ action(actionName: StateType, newData?: mDT): 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,54 +738,63 @@ 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
  // }
736
- action(name, newData) {
737
- // todo whargarbl implement hooks
738
- // todo whargarbl implement data stuff
739
- // todo major incomplete whargarbl comeback
740
- if (this.valid_action(name, newData)) {
741
- const edge = this.current_action_edge_for(name);
742
- if (this._has_hooks) {
743
- let hook_permits = undefined;
744
- const nhn = named_hook_name(this._state, edge.to, name), maybe_hook = this._named_hooks.get(nhn);
745
- if (maybe_hook === undefined) {
746
- hook_permits = true;
747
- }
748
- else {
749
- hook_permits = maybe_hook({ from: this._state, to: edge.to, action: name });
750
- }
751
- if (hook_permits !== false) {
752
- this._state = edge.to;
753
- return true;
754
- }
755
- else {
756
- return false;
757
- }
749
+ transition_impl(newStateOrAction, newData, wasForced, wasAction) {
750
+ let valid = false, newState;
751
+ if (wasForced) {
752
+ if (this.valid_force_transition(newStateOrAction, newData)) {
753
+ valid = true;
754
+ newState = newStateOrAction;
758
755
  }
759
- else {
760
- this._state = edge.to;
761
- return true;
756
+ }
757
+ else if (wasAction) {
758
+ if (this.valid_action(newStateOrAction, newData)) {
759
+ const edge = this.current_action_edge_for(newStateOrAction);
760
+ valid = true;
761
+ newState = edge.to;
762
762
  }
763
763
  }
764
764
  else {
765
- return false;
765
+ if (this.valid_transition(newStateOrAction, newData)) {
766
+ valid = true;
767
+ newState = newStateOrAction;
768
+ }
766
769
  }
767
- }
768
- transition(newState, newData) {
769
- // todo whargarbl implement hooks
770
770
  // todo whargarbl implement data stuff
771
771
  // todo major incomplete whargarbl comeback
772
- if (this.valid_transition(newState, newData)) {
772
+ if (valid) {
773
773
  if (this._has_hooks) {
774
774
  let hook_permits = undefined;
775
+ if (this._any_transition_hook !== undefined) {
776
+ if (this._any_transition_hook() === false) {
777
+ return false;
778
+ }
779
+ }
780
+ if (wasAction) {
781
+ const nhn = named_hook_name(this._state, newState, newStateOrAction), n_maybe_hook = this._named_hooks.get(nhn);
782
+ if (n_maybe_hook === undefined) {
783
+ hook_permits = true;
784
+ }
785
+ else {
786
+ hook_permits = n_maybe_hook({ from: this._state, to: newState, action: newStateOrAction });
787
+ }
788
+ if (!(hook_permits)) {
789
+ return false;
790
+ }
791
+ }
775
792
  const hn = hook_name(this._state, newState), maybe_hook = this._hooks.get(hn);
776
793
  if (maybe_hook === undefined) {
777
794
  hook_permits = true;
778
795
  }
779
796
  else {
780
- hook_permits = maybe_hook({ from: this._state, to: newState });
797
+ hook_permits = maybe_hook({ from: this._state, to: newState, forced: wasForced, action: wasAction ? newStateOrAction : undefined });
781
798
  }
782
799
  if (hook_permits !== false) {
783
800
  this._state = newState;
@@ -796,37 +813,15 @@ class Machine {
796
813
  return false;
797
814
  }
798
815
  }
816
+ action(actionName, newData) {
817
+ return this.transition_impl(actionName, newData, false, true);
818
+ }
819
+ transition(newState, newData) {
820
+ return this.transition_impl(newState, newData, false, false);
821
+ }
799
822
  // can leave machine in inconsistent state. generally do not use
800
823
  force_transition(newState, newData) {
801
- // todo whargarbl implement hooks
802
- // todo whargarbl implement data stuff
803
- // todo major incomplete whargarbl comeback
804
- if (this.valid_force_transition(newState, newData)) {
805
- if (this._has_hooks) {
806
- let hook_permits = undefined;
807
- const hn = hook_name(this._state, newState), maybe_hook = this._hooks.get(hn);
808
- if (maybe_hook === undefined) {
809
- hook_permits = true;
810
- }
811
- else {
812
- hook_permits = maybe_hook({ from: this._state, to: newState, forced: true });
813
- }
814
- if (hook_permits !== false) {
815
- this._state = newState;
816
- return true;
817
- }
818
- else {
819
- return false;
820
- }
821
- }
822
- else {
823
- this._state = newState;
824
- return true;
825
- }
826
- }
827
- else {
828
- return false;
829
- }
824
+ return this.transition_impl(newState, newData, true, false);
830
825
  }
831
826
  current_action_for(action) {
832
827
  const action_base = this._actions.get(action);
@@ -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.48.0";
1
+ const version = "5.51.0";
2
2
  export { version };