Skip to content

Commit

Permalink
Post global action hook, fixes StoneCypher/fsl#905; post any action h…
Browse files Browse the repository at this point in the history
…ook, fixes StoneCypher/fsl#906
  • Loading branch information
StoneCypher committed Jul 1, 2022
1 parent e5bfa2a commit 99847f8
Show file tree
Hide file tree
Showing 64 changed files with 320 additions and 16,543 deletions.
10 changes: 10 additions & 0 deletions dist/es6/jssm.d.ts
Expand Up @@ -569,6 +569,16 @@ declare class Machine<mDT> {
hook_any_transition(handler: HookHandler<mDT>): Machine<mDT>;
hook_entry(to: string, handler: HookHandler<mDT>): Machine<mDT>;
hook_exit(from: string, handler: HookHandler<mDT>): Machine<mDT>;
post_hook(from: string, to: string, handler: HookHandler<mDT>): Machine<mDT>;
post_hook_action(from: string, to: string, action: string, handler: HookHandler<mDT>): Machine<mDT>;
post_hook_global_action(action: string, handler: HookHandler<mDT>): Machine<mDT>;
post_hook_any_action(handler: HookHandler<mDT>): Machine<mDT>;
post_hook_standard_transition(handler: HookHandler<mDT>): Machine<mDT>;
post_hook_main_transition(handler: HookHandler<mDT>): Machine<mDT>;
post_hook_forced_transition(handler: HookHandler<mDT>): Machine<mDT>;
post_hook_any_transition(handler: HookHandler<mDT>): Machine<mDT>;
post_hook_entry(to: string, handler: HookHandler<mDT>): Machine<mDT>;
post_hook_exit(from: string, handler: HookHandler<mDT>): Machine<mDT>;
edges_between(from: string, to: string): JssmTransition<mDT>[];
transition_impl(newStateOrAction: StateType, newData: mDT | undefined, wasForced: boolean, wasAction: boolean): boolean;
/*********
Expand Down
79 changes: 61 additions & 18 deletions dist/es6/jssm.js
Expand Up @@ -1272,61 +1272,51 @@ class Machine {
this._has_exit_hooks = true;
break;
case 'post hook':
this._hooks.set(hook_name(HookDesc.from, HookDesc.to), HookDesc.handler);
this._has_hooks = true;
this._post_hooks.set(hook_name(HookDesc.from, HookDesc.to), HookDesc.handler);
this._has_post_hooks = true;
this._has_basic_hooks = true;
break;
case 'post named':
this._post_named_hooks.set(named_hook_name(HookDesc.from, HookDesc.to, HookDesc.action), HookDesc.handler);
this._has_hooks = true;
this._has_post_hooks = true;
this._has_named_hooks = true;
break;
case 'post global action':
this._post_global_action_hooks.set(HookDesc.action, HookDesc.handler);
this._has_hooks = true;
this._has_post_hooks = true;
this._has_post_global_action_hooks = true;
break;
case 'post any action':
this._post_any_action_hook = HookDesc.handler;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post standard transition':
this._post_standard_transition_hook = HookDesc.handler;
this._has_post_transition_hooks = true;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post main transition':
this._post_main_transition_hook = HookDesc.handler;
this._has_post_transition_hooks = true;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post forced transition':
this._post_forced_transition_hook = HookDesc.handler;
this._has_post_transition_hooks = true;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post any transition':
this._post_any_transition_hook = HookDesc.handler;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post entry':
this._post_entry_hooks.set(HookDesc.to, HookDesc.handler);
this._has_post_entry_hooks = true;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post exit':
this._post_exit_hooks.set(HookDesc.from, HookDesc.handler);
this._has_post_exit_hooks = true;
this._has_hooks = true;
this._has_post_hooks = true;
break;
default:
Expand Down Expand Up @@ -1373,6 +1363,46 @@ class Machine {
this.set_hook({ kind: 'exit', from, handler });
return this;
}
post_hook(from, to, handler) {
this.set_hook({ kind: 'post hook', from, to, handler });
return this;
}
post_hook_action(from, to, action, handler) {
this.set_hook({ kind: 'post named', from, to, action, handler });
return this;
}
post_hook_global_action(action, handler) {
this.set_hook({ kind: 'post global action', action, handler });
return this;
}
post_hook_any_action(handler) {
this.set_hook({ kind: 'post any action', handler });
return this;
}
post_hook_standard_transition(handler) {
this.set_hook({ kind: 'post standard transition', handler });
return this;
}
post_hook_main_transition(handler) {
this.set_hook({ kind: 'post main transition', handler });
return this;
}
post_hook_forced_transition(handler) {
this.set_hook({ kind: 'post forced transition', handler });
return this;
}
post_hook_any_transition(handler) {
this.set_hook({ kind: 'post any transition', handler });
return this;
}
post_hook_entry(to, handler) {
this.set_hook({ kind: 'post entry', to, handler });
return this;
}
post_hook_exit(from, handler) {
this.set_hook({ kind: 'post exit', from, handler });
return this;
}
// remove_hook(HookDesc: HookDescription) {
// throw new JssmError(this, 'TODO: Should remove hook here');
// }
Expand Down Expand Up @@ -1408,15 +1438,15 @@ class Machine {
newState = newStateOrAction;
}
}
const hook_args = {
data: this._data,
action: fromAction,
from: this._state,
to: newState,
forced: wasForced
};
if (valid) {
if (this._has_hooks) {
const hook_args = {
data: this._data,
action: fromAction,
from: this._state,
to: newState,
forced: wasForced
};
function update_fields(res) {
if (res.hasOwnProperty('data')) {
hook_args.data = res.data;
Expand Down Expand Up @@ -1531,6 +1561,19 @@ class Machine {
return false;
}
// posthooks begin here
if (this._has_post_hooks) {
if (wasAction) {
// 1. any action posthook
if (this._post_any_action_hook !== undefined) {
this._post_any_action_hook(hook_args);
}
// 2. global specific action hook
const pgah = this._post_global_action_hooks.get(newStateOrAction);
if (pgah !== undefined) {
pgah(hook_args);
}
}
}
return true;
}
/*********
Expand Down
2 changes: 1 addition & 1 deletion dist/jssm.es5.cjs.js

Large diffs are not rendered by default.

79 changes: 61 additions & 18 deletions dist/jssm.es5.cjs.nonmin.js
Expand Up @@ -18081,61 +18081,51 @@ class Machine {
this._has_exit_hooks = true;
break;
case 'post hook':
this._hooks.set(hook_name(HookDesc.from, HookDesc.to), HookDesc.handler);
this._has_hooks = true;
this._post_hooks.set(hook_name(HookDesc.from, HookDesc.to), HookDesc.handler);
this._has_post_hooks = true;
this._has_basic_hooks = true;
break;
case 'post named':
this._post_named_hooks.set(named_hook_name(HookDesc.from, HookDesc.to, HookDesc.action), HookDesc.handler);
this._has_hooks = true;
this._has_post_hooks = true;
this._has_named_hooks = true;
break;
case 'post global action':
this._post_global_action_hooks.set(HookDesc.action, HookDesc.handler);
this._has_hooks = true;
this._has_post_hooks = true;
this._has_post_global_action_hooks = true;
break;
case 'post any action':
this._post_any_action_hook = HookDesc.handler;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post standard transition':
this._post_standard_transition_hook = HookDesc.handler;
this._has_post_transition_hooks = true;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post main transition':
this._post_main_transition_hook = HookDesc.handler;
this._has_post_transition_hooks = true;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post forced transition':
this._post_forced_transition_hook = HookDesc.handler;
this._has_post_transition_hooks = true;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post any transition':
this._post_any_transition_hook = HookDesc.handler;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post entry':
this._post_entry_hooks.set(HookDesc.to, HookDesc.handler);
this._has_post_entry_hooks = true;
this._has_hooks = true;
this._has_post_hooks = true;
break;
case 'post exit':
this._post_exit_hooks.set(HookDesc.from, HookDesc.handler);
this._has_post_exit_hooks = true;
this._has_hooks = true;
this._has_post_hooks = true;
break;
default:
Expand Down Expand Up @@ -18182,6 +18172,46 @@ class Machine {
this.set_hook({ kind: 'exit', from, handler });
return this;
}
post_hook(from, to, handler) {
this.set_hook({ kind: 'post hook', from, to, handler });
return this;
}
post_hook_action(from, to, action, handler) {
this.set_hook({ kind: 'post named', from, to, action, handler });
return this;
}
post_hook_global_action(action, handler) {
this.set_hook({ kind: 'post global action', action, handler });
return this;
}
post_hook_any_action(handler) {
this.set_hook({ kind: 'post any action', handler });
return this;
}
post_hook_standard_transition(handler) {
this.set_hook({ kind: 'post standard transition', handler });
return this;
}
post_hook_main_transition(handler) {
this.set_hook({ kind: 'post main transition', handler });
return this;
}
post_hook_forced_transition(handler) {
this.set_hook({ kind: 'post forced transition', handler });
return this;
}
post_hook_any_transition(handler) {
this.set_hook({ kind: 'post any transition', handler });
return this;
}
post_hook_entry(to, handler) {
this.set_hook({ kind: 'post entry', to, handler });
return this;
}
post_hook_exit(from, handler) {
this.set_hook({ kind: 'post exit', from, handler });
return this;
}
// remove_hook(HookDesc: HookDescription) {
// throw new JssmError(this, 'TODO: Should remove hook here');
// }
Expand Down Expand Up @@ -18217,15 +18247,15 @@ class Machine {
newState = newStateOrAction;
}
}
const hook_args = {
data: this._data,
action: fromAction,
from: this._state,
to: newState,
forced: wasForced
};
if (valid) {
if (this._has_hooks) {
const hook_args = {
data: this._data,
action: fromAction,
from: this._state,
to: newState,
forced: wasForced
};
function update_fields(res) {
if (res.hasOwnProperty('data')) {
hook_args.data = res.data;
Expand Down Expand Up @@ -18340,6 +18370,19 @@ class Machine {
return false;
}
// posthooks begin here
if (this._has_post_hooks) {
if (wasAction) {
// 1. any action posthook
if (this._post_any_action_hook !== undefined) {
this._post_any_action_hook(hook_args);
}
// 2. global specific action hook
const pgah = this._post_global_action_hooks.get(newStateOrAction);
if (pgah !== undefined) {
pgah(hook_args);
}
}
}
return true;
}
/*********
Expand Down
2 changes: 1 addition & 1 deletion dist/jssm.es5.iife.js

Large diffs are not rendered by default.

0 comments on commit 99847f8

Please sign in to comment.