jssm 5.55.0 → 5.57.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.
@@ -40,8 +40,12 @@ declare class Machine<mDT> {
40
40
  _has_hooks: boolean;
41
41
  _has_basic_hooks: boolean;
42
42
  _has_named_hooks: boolean;
43
+ _has_entry_hooks: boolean;
44
+ _has_exit_hooks: boolean;
43
45
  _hooks: Map<string, Function>;
44
46
  _named_hooks: Map<string, Function>;
47
+ _entry_hooks: Map<string, Function>;
48
+ _exit_hooks: Map<string, Function>;
45
49
  _any_transition_hook: HookHandler | undefined;
46
50
  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>);
47
51
  _new_state(state_config: JssmGenericState): StateType;
@@ -96,6 +100,7 @@ declare class Machine<mDT> {
96
100
  hook(from: string, to: string, handler: HookHandler): Machine<mDT>;
97
101
  hook_action(from: string, to: string, action: string, handler: HookHandler): Machine<mDT>;
98
102
  hook_any_transition(handler: HookHandler): Machine<mDT>;
103
+ hook_entry(to: string, handler: HookHandler): Machine<mDT>;
99
104
  transition_impl(newStateOrAction: StateType, newData: mDT | undefined, wasForced: boolean, wasAction: boolean): boolean;
100
105
  action(actionName: StateType, newData?: mDT): boolean;
101
106
  transition(newState: StateType, newData?: mDT): boolean;
package/dist/es6/jssm.js CHANGED
@@ -348,8 +348,13 @@ class Machine {
348
348
  this._has_hooks = false;
349
349
  this._has_basic_hooks = false;
350
350
  this._has_named_hooks = false;
351
+ this._has_entry_hooks = false;
352
+ this._has_exit_hooks = false;
353
+ // no need for a boolean has any transition hook, as it's one or nothing, so just test that for undefinedness
351
354
  this._hooks = new Map();
352
355
  this._named_hooks = new Map();
356
+ this._entry_hooks = new Map();
357
+ this._exit_hooks = new Map();
353
358
  this._any_transition_hook = undefined;
354
359
  if (state_declaration) {
355
360
  state_declaration.map((state_decl) => {
@@ -717,12 +722,14 @@ class Machine {
717
722
  this._any_transition_hook = HookDesc.handler;
718
723
  this._has_hooks = true;
719
724
  break;
720
- // case 'entry':
721
- // console.log('TODO: Should add entry hook here');
722
- // throw 'TODO: Should add entry hook here';
723
- // case 'exit':
724
- // console.log('TODO: Should add exit hook here');
725
- // throw 'TODO: Should add exit hook here';
725
+ case 'entry':
726
+ this._entry_hooks.set(HookDesc.to, HookDesc.handler);
727
+ this._has_hooks = true;
728
+ break;
729
+ case 'exit':
730
+ this._exit_hooks.set(HookDesc.from, HookDesc.handler);
731
+ this._has_hooks = true;
732
+ break;
726
733
  default:
727
734
  console.log(`Unknown hook type ${HookDesc.kind}, should be impossible`);
728
735
  throw new RangeError(`Unknown hook type ${HookDesc.kind}, should be impossible`);
@@ -743,6 +750,11 @@ class Machine {
743
750
  this.set_hook({ kind: 'any transition', handler });
744
751
  return this;
745
752
  }
753
+ hook_entry(to, handler) {
754
+ // TODO: should this throw if setting the hook fails, or ignore it and continue?
755
+ this.set_hook({ kind: 'entry', to, handler });
756
+ return this;
757
+ }
746
758
  // remove_hook(HookDesc: HookDescription) {
747
759
  // throw 'TODO: Should remove hook here';
748
760
  // }
@@ -771,43 +783,55 @@ class Machine {
771
783
  // todo major incomplete whargarbl comeback
772
784
  if (valid) {
773
785
  if (this._has_hooks) {
774
- let hook_permits = undefined;
786
+ // 1. any action hook
787
+ // not yet implemented
788
+ // 2. any transition hook
775
789
  if (this._any_transition_hook !== undefined) {
776
790
  if (this._any_transition_hook() === false) {
777
791
  return false;
778
792
  }
779
793
  }
794
+ // 3. exit hook
795
+ const maybe_ex_hook = this._exit_hooks.get(this._state);
796
+ if (maybe_ex_hook !== undefined) {
797
+ if (maybe_ex_hook({ from: this._state, forced: wasForced }) === false) {
798
+ return false;
799
+ }
800
+ }
801
+ // 4. named transition / action hook
780
802
  if (wasAction) {
781
803
  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;
804
+ if (n_maybe_hook !== undefined) {
805
+ if (n_maybe_hook({ from: this._state, to: newState, action: newStateOrAction }) === false) {
806
+ return false;
807
+ }
790
808
  }
791
809
  }
810
+ // 5. regular hook
792
811
  const hn = hook_name(this._state, newState), maybe_hook = this._hooks.get(hn);
793
- if (maybe_hook === undefined) {
794
- hook_permits = true;
795
- }
796
- else {
797
- hook_permits = maybe_hook({ from: this._state, to: newState, forced: wasForced, action: wasAction ? newStateOrAction : undefined });
798
- }
799
- if (hook_permits !== false) {
800
- this._state = newState;
801
- return true;
812
+ if (maybe_hook !== undefined) {
813
+ if (maybe_hook({ from: this._state, to: newState, forced: wasForced, action: wasAction ? newStateOrAction : undefined }) === false) {
814
+ return false;
815
+ }
802
816
  }
803
- else {
804
- return false;
817
+ // 6. edge type hook
818
+ // not yet implemented
819
+ // 7. entry hook
820
+ const maybe_en_hook = this._entry_hooks.get(newState);
821
+ if (maybe_en_hook !== undefined) {
822
+ if (maybe_en_hook({ to: newState, forced: wasForced }) === false) {
823
+ return false;
824
+ }
805
825
  }
826
+ this._state = newState;
827
+ return true;
828
+ // or without hooks
806
829
  }
807
830
  else {
808
831
  this._state = newState;
809
832
  return true;
810
833
  }
834
+ // not valid
811
835
  }
812
836
  else {
813
837
  return false;
@@ -1,2 +1,2 @@
1
- const version = "5.55.0";
1
+ const version = "5.57.0";
2
2
  export { version };