Skip to content

Commit

Permalink
Fluent entry hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
StoneCypher committed May 9, 2022
1 parent 8e20d05 commit af441da
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 28 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="chart1652112065483" width="16" height="9"></canvas>
<canvas id="chart1652112411203" width="16" height="9"></canvas>
</div>
<script>
const format = (num) => {
Expand All @@ -51,18 +51,18 @@
chunked.map((chunk) => chunk.join('')).join(' ') + fractionStr
)
}
const ctx1652112065483 = document
.getElementById('chart1652112065483')
const ctx1652112411203 = document
.getElementById('chart1652112411203')
.getContext('2d')
const chart1652112065483 = new Chart(ctx1652112065483, {
const chart1652112411203 = new Chart(ctx1652112411203, {
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: [18713,1726,14874,819],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(11.063999999999998, 85%, 55%)","hsl(95.376, 85%, 55%)","hsl(5.255999999999995, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(11.063999999999998, 85%, 55%)","hsl(95.376, 85%, 55%)","hsl(5.255999999999995, 85%, 55%)"],
data: [19390,1654,14799,848],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(10.236000000000002, 85%, 55%)","hsl(91.58399999999999, 85%, 55%)","hsl(5.244000000000005, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(10.236000000000002, 85%, 55%)","hsl(91.58399999999999, 85%, 55%)","hsl(5.244000000000005, 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-09T16:01:05.483Z",
"date": "2022-05-09T16:06:51.203Z",
"version": "1.1.0",
"results": [
{
"name": "Blind cycle a traffic light 500 times by transition",
"ops": 18713,
"margin": 1.21,
"ops": 19390,
"margin": 6.39,
"percentSlower": 0
},
{
"name": "Blind cycle a hooked traffic light 500 times by transition",
"ops": 1726,
"margin": 0.59,
"percentSlower": 90.78
"ops": 1654,
"margin": 1.66,
"percentSlower": 91.47
},
{
"name": "Blind cycle a traffic light 500 times by action",
"ops": 14874,
"margin": 0.46,
"percentSlower": 20.52
"ops": 14799,
"margin": 0.87,
"percentSlower": 23.68
},
{
"name": "Blind cycle a hooked traffic light 500 times by action",
"ops": 819,
"margin": 3.29,
"percentSlower": 95.62
"ops": 848,
"margin": 0.86,
"percentSlower": 95.63
}
],
"fastest": {
Expand Down
1 change: 1 addition & 0 deletions dist/es6/jssm.d.ts
Expand Up @@ -98,6 +98,7 @@ declare class Machine<mDT> {
hook(from: string, to: string, handler: HookHandler): Machine<mDT>;
hook_action(from: string, to: string, action: string, handler: HookHandler): Machine<mDT>;
hook_any_transition(handler: HookHandler): Machine<mDT>;
hook_entry(to: string, handler: HookHandler): Machine<mDT>;
transition_impl(newStateOrAction: StateType, newData: mDT | undefined, wasForced: boolean, wasAction: boolean): boolean;
action(actionName: StateType, newData?: mDT): boolean;
transition(newState: StateType, newData?: mDT): boolean;
Expand Down
5 changes: 5 additions & 0 deletions dist/es6/jssm.js
Expand Up @@ -747,6 +747,11 @@ class Machine {
this.set_hook({ kind: 'any transition', handler });
return this;
}
hook_entry(to, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'entry', to, handler });
return this;
}
// remove_hook(HookDesc: HookDescription) {
// throw 'TODO: Should remove hook here';
// }
Expand Down
2 changes: 1 addition & 1 deletion dist/es6/version.js
@@ -1,2 +1,2 @@
const version = "5.56.0";
const version = "5.56.1";
export { version };
2 changes: 1 addition & 1 deletion dist/jssm.es5.cjs.js

Large diffs are not rendered by default.

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

const version = "5.56.0";
const version = "5.56.1";

// whargarbl lots of these return arrays could/should be sets
/* eslint-disable complexity */
Expand Down Expand Up @@ -16633,6 +16633,11 @@ class Machine {
this.set_hook({ kind: 'any transition', handler });
return this;
}
hook_entry(to, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'entry', to, handler });
return this;
}
// remove_hook(HookDesc: HookDescription) {
// throw 'TODO: Should remove hook here';
// }
Expand Down
2 changes: 1 addition & 1 deletion dist/jssm.es5.iife.js

Large diffs are not rendered by default.

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

const version = "5.56.0";
const version = "5.56.1";

// whargarbl lots of these return arrays could/should be sets
/* eslint-disable complexity */
Expand Down Expand Up @@ -16632,6 +16632,11 @@ var jssm = (function (exports) {
this.set_hook({ kind: 'any transition', handler });
return this;
}
hook_entry(to, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'entry', to, handler });
return this;
}
// remove_hook(HookDesc: HookDescription) {
// throw 'TODO: Should remove hook here';
// }
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 @@ -98,6 +98,7 @@ declare class Machine<mDT> {
hook(from: string, to: string, handler: HookHandler): Machine<mDT>;
hook_action(from: string, to: string, action: string, handler: HookHandler): Machine<mDT>;
hook_any_transition(handler: HookHandler): Machine<mDT>;
hook_entry(to: string, handler: HookHandler): Machine<mDT>;
transition_impl(newStateOrAction: StateType, newData: mDT | undefined, wasForced: boolean, wasAction: boolean): boolean;
action(actionName: StateType, newData?: mDT): boolean;
transition(newState: StateType, newData?: mDT): boolean;
Expand Down
20 changes: 20 additions & 0 deletions src/ts/tests/hooks.spec.ts
Expand Up @@ -207,6 +207,26 @@ test('Entry hook rejection works on actions', () => {



test('Fluent entry hook works', () => {

const cnt = jest.fn(x => true),
nope = jest.fn(x => true);

const foo = sm`a => b => c;`
.hook_entry('b', cnt)
.hook_entry('c', nope);

foo.transition('b');

expect(cnt.mock.calls.length).toBe(1);
expect(nope.mock.calls.length).toBe(0);

});





test('All-transition hook rejection works on transitions', () => {

const foo = sm`a => b;`;
Expand Down
2 changes: 1 addition & 1 deletion src/ts/version.ts
@@ -1,3 +1,3 @@

const version: string = "5.56.0";
const version: string = "5.56.1";
export { version };

0 comments on commit af441da

Please sign in to comment.