Skip to content

Commit

Permalink
Merge pull request #460 from StoneCypher/ApiAllActionHook
Browse files Browse the repository at this point in the history
API All-Action hook, fixes StoneCypher/fsl#670
  • Loading branch information
StoneCypher committed May 12, 2022
2 parents 3840469 + 3b54f74 commit 4840482
Show file tree
Hide file tree
Showing 20 changed files with 153 additions and 55 deletions.
14 changes: 7 additions & 7 deletions benchmark/results/general.chart.html
Expand Up @@ -28,7 +28,7 @@
</head>
<body>
<div class="container">
<canvas id="chart1652115720275" width="16" height="9"></canvas>
<canvas id="chart1652326952693" width="16" height="9"></canvas>
</div>
<script>
const format = (num) => {
Expand All @@ -51,18 +51,18 @@
chunked.map((chunk) => chunk.join('')).join(' ') + fractionStr
)
}
const ctx1652115720275 = document
.getElementById('chart1652115720275')
const ctx1652326952693 = document
.getElementById('chart1652326952693')
.getContext('2d')
const chart1652115720275 = new Chart(ctx1652115720275, {
const chart1652326952693 = new Chart(ctx1652326952693, {
type: 'bar',
data: {
labels: ["Blind cycle a traffic light 500 times by transition","Blind cycle a hooked traffic light 500 times by transition","Blind cycle a traffic light 500 times by action","Blind cycle a hooked traffic light 500 times by action"],
datasets: [
{
data: [19229,1609,13038,795],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(10.044000000000006, 85%, 55%)","hsl(81.35999999999999, 85%, 55%)","hsl(4.955999999999994, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(10.044000000000006, 85%, 55%)","hsl(81.35999999999999, 85%, 55%)","hsl(4.955999999999994, 85%, 55%)"],
data: [18356,1653,13179,828],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(10.812000000000006, 85%, 55%)","hsl(86.16, 85%, 55%)","hsl(5.412000000000006, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(10.812000000000006, 85%, 55%)","hsl(86.16, 85%, 55%)","hsl(5.412000000000006, 85%, 55%)"],
borderWidth: 2,
},
],
Expand Down
24 changes: 12 additions & 12 deletions benchmark/results/general.json
@@ -1,31 +1,31 @@
{
"name": "General performance suite",
"date": "2022-05-09T17:02:00.275Z",
"date": "2022-05-12T03:42:32.693Z",
"version": "1.1.0",
"results": [
{
"name": "Blind cycle a traffic light 500 times by transition",
"ops": 19229,
"margin": 1.37,
"ops": 18356,
"margin": 6.39,
"percentSlower": 0
},
{
"name": "Blind cycle a hooked traffic light 500 times by transition",
"ops": 1609,
"margin": 3.02,
"percentSlower": 91.63
"ops": 1653,
"margin": 3.21,
"percentSlower": 90.99
},
{
"name": "Blind cycle a traffic light 500 times by action",
"ops": 13038,
"margin": 8.4,
"percentSlower": 32.2
"ops": 13179,
"margin": 5.95,
"percentSlower": 28.2
},
{
"name": "Blind cycle a hooked traffic light 500 times by action",
"ops": 795,
"margin": 2.86,
"percentSlower": 95.87
"ops": 828,
"margin": 2.11,
"percentSlower": 95.49
}
],
"fastest": {
Expand Down
1 change: 1 addition & 0 deletions dist/es6/jssm.d.ts
Expand Up @@ -46,6 +46,7 @@ declare class Machine<mDT> {
_named_hooks: Map<string, Function>;
_entry_hooks: Map<string, Function>;
_exit_hooks: Map<string, Function>;
_any_action_hook: HookHandler | undefined;
_any_transition_hook: HookHandler | undefined;
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>);
_new_state(state_config: JssmGenericState): StateType;
Expand Down
25 changes: 19 additions & 6 deletions dist/es6/jssm.js
Expand Up @@ -355,6 +355,7 @@ class Machine {
this._named_hooks = new Map();
this._entry_hooks = new Map();
this._exit_hooks = new Map();
this._any_action_hook = undefined;
this._any_transition_hook = undefined;
if (state_declaration) {
state_declaration.map((state_decl) => {
Expand Down Expand Up @@ -718,6 +719,10 @@ class Machine {
this._named_hooks.set(named_hook_name(HookDesc.from, HookDesc.to, HookDesc.action), HookDesc.handler);
this._has_hooks = true;
break;
case 'any action':
this._any_action_hook = HookDesc.handler;
this._has_hooks = true;
break;
case 'any transition':
this._any_transition_hook = HookDesc.handler;
this._has_hooks = true;
Expand Down Expand Up @@ -789,21 +794,29 @@ class Machine {
if (valid) {
if (this._has_hooks) {
// 1. any action hook
if (wasAction) {
if (this._any_action_hook !== undefined) {
if (this._any_action_hook() === false) {
return false;
}
}
}
// 2. global specific action hook
// not yet implemented
// 2. any transition hook
// 3. any transition hook
if (this._any_transition_hook !== undefined) {
if (this._any_transition_hook() === false) {
return false;
}
}
// 3. exit hook
// 4. exit hook
const maybe_ex_hook = this._exit_hooks.get(this._state);
if (maybe_ex_hook !== undefined) {
if (maybe_ex_hook({ from: this._state, forced: wasForced }) === false) {
return false;
}
}
// 4. named transition / action hook
// 5. named transition / action hook
if (wasAction) {
const nhn = named_hook_name(this._state, newState, newStateOrAction), n_maybe_hook = this._named_hooks.get(nhn);
if (n_maybe_hook !== undefined) {
Expand All @@ -812,16 +825,16 @@ class Machine {
}
}
}
// 5. regular hook
// 6. regular hook
const hn = hook_name(this._state, newState), maybe_hook = this._hooks.get(hn);
if (maybe_hook !== undefined) {
if (maybe_hook({ from: this._state, to: newState, forced: wasForced, action: wasAction ? newStateOrAction : undefined }) === false) {
return false;
}
}
// 6. edge type hook
// 7. edge type hook
// not yet implemented
// 7. entry hook
// 8. entry hook
const maybe_en_hook = this._entry_hooks.get(newState);
if (maybe_en_hook !== undefined) {
if (maybe_en_hook({ to: newState, forced: wasForced }) === false) {
Expand Down
6 changes: 5 additions & 1 deletion dist/es6/jssm_types.d.ts
Expand Up @@ -166,6 +166,10 @@ declare type AnyTransitionHook = {
kind: 'any transition';
handler: HookHandler;
};
declare type AnyActionHook = {
kind: 'any action';
handler: HookHandler;
};
declare type EntryHook = {
kind: 'entry';
to: string;
Expand All @@ -176,5 +180,5 @@ declare type ExitHook = {
from: string;
handler: HookHandler;
};
declare type HookDescription = BasicHookDescription | HookDescriptionWithAction | AnyTransitionHook | EntryHook | ExitHook;
declare type HookDescription = BasicHookDescription | HookDescriptionWithAction | AnyActionHook | AnyTransitionHook | EntryHook | ExitHook;
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 };
2 changes: 1 addition & 1 deletion dist/es6/version.js
@@ -1,2 +1,2 @@
const version = "5.57.1";
const version = "5.58.0";
export { version };
2 changes: 1 addition & 1 deletion dist/jssm.es5.cjs.js

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions dist/jssm.es5.cjs.nonmin.js
Expand Up @@ -15886,7 +15886,7 @@ function peg$parse(input, options) {
}
}

const version = "5.57.1";
const version = "5.58.0";

// whargarbl lots of these return arrays could/should be sets
/* eslint-disable complexity */
Expand Down Expand Up @@ -16241,6 +16241,7 @@ class Machine {
this._named_hooks = new Map();
this._entry_hooks = new Map();
this._exit_hooks = new Map();
this._any_action_hook = undefined;
this._any_transition_hook = undefined;
if (state_declaration) {
state_declaration.map((state_decl) => {
Expand Down Expand Up @@ -16604,6 +16605,10 @@ class Machine {
this._named_hooks.set(named_hook_name(HookDesc.from, HookDesc.to, HookDesc.action), HookDesc.handler);
this._has_hooks = true;
break;
case 'any action':
this._any_action_hook = HookDesc.handler;
this._has_hooks = true;
break;
case 'any transition':
this._any_transition_hook = HookDesc.handler;
this._has_hooks = true;
Expand Down Expand Up @@ -16675,21 +16680,29 @@ class Machine {
if (valid) {
if (this._has_hooks) {
// 1. any action hook
if (wasAction) {
if (this._any_action_hook !== undefined) {
if (this._any_action_hook() === false) {
return false;
}
}
}
// 2. global specific action hook
// not yet implemented
// 2. any transition hook
// 3. any transition hook
if (this._any_transition_hook !== undefined) {
if (this._any_transition_hook() === false) {
return false;
}
}
// 3. exit hook
// 4. exit hook
const maybe_ex_hook = this._exit_hooks.get(this._state);
if (maybe_ex_hook !== undefined) {
if (maybe_ex_hook({ from: this._state, forced: wasForced }) === false) {
return false;
}
}
// 4. named transition / action hook
// 5. named transition / action hook
if (wasAction) {
const nhn = named_hook_name(this._state, newState, newStateOrAction), n_maybe_hook = this._named_hooks.get(nhn);
if (n_maybe_hook !== undefined) {
Expand All @@ -16698,16 +16711,16 @@ class Machine {
}
}
}
// 5. regular hook
// 6. regular hook
const hn = hook_name(this._state, newState), maybe_hook = this._hooks.get(hn);
if (maybe_hook !== undefined) {
if (maybe_hook({ from: this._state, to: newState, forced: wasForced, action: wasAction ? newStateOrAction : undefined }) === false) {
return false;
}
}
// 6. edge type hook
// 7. edge type hook
// not yet implemented
// 7. entry hook
// 8. entry hook
const maybe_en_hook = this._entry_hooks.get(newState);
if (maybe_en_hook !== undefined) {
if (maybe_en_hook({ to: newState, forced: wasForced }) === false) {
Expand Down
2 changes: 1 addition & 1 deletion dist/jssm.es5.iife.js

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions dist/jssm.es5.iife.nonmin.js
Expand Up @@ -15885,7 +15885,7 @@ var jssm = (function (exports) {
}
}

const version = "5.57.1";
const version = "5.58.0";

// whargarbl lots of these return arrays could/should be sets
/* eslint-disable complexity */
Expand Down Expand Up @@ -16240,6 +16240,7 @@ var jssm = (function (exports) {
this._named_hooks = new Map();
this._entry_hooks = new Map();
this._exit_hooks = new Map();
this._any_action_hook = undefined;
this._any_transition_hook = undefined;
if (state_declaration) {
state_declaration.map((state_decl) => {
Expand Down Expand Up @@ -16603,6 +16604,10 @@ var jssm = (function (exports) {
this._named_hooks.set(named_hook_name(HookDesc.from, HookDesc.to, HookDesc.action), HookDesc.handler);
this._has_hooks = true;
break;
case 'any action':
this._any_action_hook = HookDesc.handler;
this._has_hooks = true;
break;
case 'any transition':
this._any_transition_hook = HookDesc.handler;
this._has_hooks = true;
Expand Down Expand Up @@ -16674,21 +16679,29 @@ var jssm = (function (exports) {
if (valid) {
if (this._has_hooks) {
// 1. any action hook
if (wasAction) {
if (this._any_action_hook !== undefined) {
if (this._any_action_hook() === false) {
return false;
}
}
}
// 2. global specific action hook
// not yet implemented
// 2. any transition hook
// 3. any transition hook
if (this._any_transition_hook !== undefined) {
if (this._any_transition_hook() === false) {
return false;
}
}
// 3. exit hook
// 4. exit hook
const maybe_ex_hook = this._exit_hooks.get(this._state);
if (maybe_ex_hook !== undefined) {
if (maybe_ex_hook({ from: this._state, forced: wasForced }) === false) {
return false;
}
}
// 4. named transition / action hook
// 5. named transition / action hook
if (wasAction) {
const nhn = named_hook_name(this._state, newState, newStateOrAction), n_maybe_hook = this._named_hooks.get(nhn);
if (n_maybe_hook !== undefined) {
Expand All @@ -16697,16 +16710,16 @@ var jssm = (function (exports) {
}
}
}
// 5. regular hook
// 6. regular hook
const hn = hook_name(this._state, newState), maybe_hook = this._hooks.get(hn);
if (maybe_hook !== undefined) {
if (maybe_hook({ from: this._state, to: newState, forced: wasForced, action: wasAction ? newStateOrAction : undefined }) === false) {
return false;
}
}
// 6. edge type hook
// 7. edge type hook
// not yet implemented
// 7. entry hook
// 8. entry hook
const maybe_en_hook = this._entry_hooks.get(newState);
if (maybe_en_hook !== undefined) {
if (maybe_en_hook({ to: newState, forced: wasForced }) === false) {
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/assets/search.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/docs/classes/Machine.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/docs/modules.html

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions jssm.d.ts
Expand Up @@ -46,6 +46,7 @@ declare class Machine<mDT> {
_named_hooks: Map<string, Function>;
_entry_hooks: Map<string, Function>;
_exit_hooks: Map<string, Function>;
_any_action_hook: HookHandler | undefined;
_any_transition_hook: HookHandler | undefined;
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>);
_new_state(state_config: JssmGenericState): StateType;
Expand Down
6 changes: 5 additions & 1 deletion jssm_types.d.ts
Expand Up @@ -166,6 +166,10 @@ declare type AnyTransitionHook = {
kind: 'any transition';
handler: HookHandler;
};
declare type AnyActionHook = {
kind: 'any action';
handler: HookHandler;
};
declare type EntryHook = {
kind: 'entry';
to: string;
Expand All @@ -176,5 +180,5 @@ declare type ExitHook = {
from: string;
handler: HookHandler;
};
declare type HookDescription = BasicHookDescription | HookDescriptionWithAction | AnyTransitionHook | EntryHook | ExitHook;
declare type HookDescription = BasicHookDescription | HookDescriptionWithAction | AnyActionHook | AnyTransitionHook | EntryHook | ExitHook;
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 };
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "jssm",
"version": "5.57.1",
"version": "5.58.0",
"engines": {
"node": ">=10.0.0"
},
Expand Down

0 comments on commit 4840482

Please sign in to comment.