Skip to content

Commit

Permalink
Merge pull request #459 from StoneCypher/FluentExitHooks
Browse files Browse the repository at this point in the history
Exit fluent hooks, fixes StoneCypher/fsl#682
  • Loading branch information
StoneCypher committed May 9, 2022
2 parents 99fccf0 + 63d5b33 commit 3840469
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 39 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="chart1652115341663" width="16" height="9"></canvas>
<canvas id="chart1652115720275" width="16" height="9"></canvas>
</div>
<script>
const format = (num) => {
Expand All @@ -51,18 +51,18 @@
chunked.map((chunk) => chunk.join('')).join(' ') + fractionStr
)
}
const ctx1652115341663 = document
.getElementById('chart1652115341663')
const ctx1652115720275 = document
.getElementById('chart1652115720275')
.getContext('2d')
const chart1652115341663 = new Chart(ctx1652115341663, {
const chart1652115720275 = new Chart(ctx1652115720275, {
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: [18730,1543,14281,805],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(9.887999999999993, 85%, 55%)","hsl(91.5, 85%, 55%)","hsl(5.159999999999997, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(9.887999999999993, 85%, 55%)","hsl(91.5, 85%, 55%)","hsl(5.159999999999997, 85%, 55%)"],
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%)"],
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:55:41.663Z",
"date": "2022-05-09T17:02:00.275Z",
"version": "1.1.0",
"results": [
{
"name": "Blind cycle a traffic light 500 times by transition",
"ops": 18730,
"margin": 1.2,
"ops": 19229,
"margin": 1.37,
"percentSlower": 0
},
{
"name": "Blind cycle a hooked traffic light 500 times by transition",
"ops": 1543,
"margin": 5.49,
"percentSlower": 91.76
"ops": 1609,
"margin": 3.02,
"percentSlower": 91.63
},
{
"name": "Blind cycle a traffic light 500 times by action",
"ops": 14281,
"margin": 1.96,
"percentSlower": 23.75
"ops": 13038,
"margin": 8.4,
"percentSlower": 32.2
},
{
"name": "Blind cycle a hooked traffic light 500 times by action",
"ops": 805,
"margin": 1.05,
"percentSlower": 95.7
"ops": 795,
"margin": 2.86,
"percentSlower": 95.87
}
],
"fastest": {
Expand Down
1 change: 1 addition & 0 deletions dist/es6/jssm.d.ts
Expand Up @@ -101,6 +101,7 @@ declare class 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>;
hook_exit(from: 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 @@ -755,6 +755,11 @@ class Machine {
this.set_hook({ kind: 'entry', to, handler });
return this;
}
hook_exit(from, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'exit', from, 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.57.0";
const version = "5.57.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.57.0";
const version = "5.57.1";

// whargarbl lots of these return arrays could/should be sets
/* eslint-disable complexity */
Expand Down Expand Up @@ -16641,6 +16641,11 @@ class Machine {
this.set_hook({ kind: 'entry', to, handler });
return this;
}
hook_exit(from, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'exit', from, 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.57.0";
const version = "5.57.1";

// whargarbl lots of these return arrays could/should be sets
/* eslint-disable complexity */
Expand Down Expand Up @@ -16640,6 +16640,11 @@ var jssm = (function (exports) {
this.set_hook({ kind: 'entry', to, handler });
return this;
}
hook_exit(from, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'exit', from, 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 @@ -101,6 +101,7 @@ declare class 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>;
hook_exit(from: 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
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "jssm",
"version": "5.57.0",
"version": "5.57.1",
"engines": {
"node": ">=10.0.0"
},
Expand Down
10 changes: 10 additions & 0 deletions src/ts/jssm.ts
Expand Up @@ -1085,6 +1085,16 @@ class Machine<mDT> {



hook_exit(from: string, handler: HookHandler): Machine<mDT> {

// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'exit', from, handler });
return this;

}



// remove_hook(HookDesc: HookDescription) {
// throw 'TODO: Should remove hook here';
// }
Expand Down
20 changes: 10 additions & 10 deletions src/ts/tests/hooks.spec.ts
Expand Up @@ -251,21 +251,21 @@ test('Exit hook rejection works', () => {



// test('Fluent exit hook works', () => {
test('Fluent exit hook works', () => {

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

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

// foo.transition('b');
foo.transition('b');

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

// });
});



Expand Down
2 changes: 1 addition & 1 deletion src/ts/version.ts
@@ -1,3 +1,3 @@

const version: string = "5.57.0";
const version: string = "5.57.1";
export { version };

0 comments on commit 3840469

Please sign in to comment.