Skip to content

Commit

Permalink
Merge pull request #461 from StoneCypher/FluentAllActionHook
Browse files Browse the repository at this point in the history
Fluent all action hook
  • Loading branch information
StoneCypher committed May 12, 2022
2 parents 4840482 + 1b8acfb commit 1c18d04
Show file tree
Hide file tree
Showing 17 changed files with 74 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="chart1652326952693" width="16" height="9"></canvas>
<canvas id="chart1652327395025" width="16" height="9"></canvas>
</div>
<script>
const format = (num) => {
Expand All @@ -51,18 +51,18 @@
chunked.map((chunk) => chunk.join('')).join(' ') + fractionStr
)
}
const ctx1652326952693 = document
.getElementById('chart1652326952693')
const ctx1652327395025 = document
.getElementById('chart1652327395025')
.getContext('2d')
const chart1652326952693 = new Chart(ctx1652326952693, {
const chart1652327395025 = new Chart(ctx1652327395025, {
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: [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%)"],
data: [19509,1675,13411,828],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(10.308000000000003, 85%, 55%)","hsl(82.48799999999999, 85%, 55%)","hsl(5.087999999999994, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(10.308000000000003, 85%, 55%)","hsl(82.48799999999999, 85%, 55%)","hsl(5.087999999999994, 85%, 55%)"],
borderWidth: 2,
},
],
Expand Down
22 changes: 11 additions & 11 deletions benchmark/results/general.json
@@ -1,31 +1,31 @@
{
"name": "General performance suite",
"date": "2022-05-12T03:42:32.693Z",
"date": "2022-05-12T03:49:55.025Z",
"version": "1.1.0",
"results": [
{
"name": "Blind cycle a traffic light 500 times by transition",
"ops": 18356,
"margin": 6.39,
"ops": 19509,
"margin": 1.35,
"percentSlower": 0
},
{
"name": "Blind cycle a hooked traffic light 500 times by transition",
"ops": 1653,
"margin": 3.21,
"percentSlower": 90.99
"ops": 1675,
"margin": 0.58,
"percentSlower": 91.41
},
{
"name": "Blind cycle a traffic light 500 times by action",
"ops": 13179,
"margin": 5.95,
"percentSlower": 28.2
"ops": 13411,
"margin": 0.93,
"percentSlower": 31.26
},
{
"name": "Blind cycle a hooked traffic light 500 times by action",
"ops": 828,
"margin": 2.11,
"percentSlower": 95.49
"margin": 0.85,
"percentSlower": 95.76
}
],
"fastest": {
Expand Down
1 change: 1 addition & 0 deletions dist/es6/jssm.d.ts
Expand Up @@ -100,6 +100,7 @@ declare class Machine<mDT> {
set_hook(HookDesc: HookDescription): void;
hook(from: string, to: string, handler: HookHandler): Machine<mDT>;
hook_action(from: string, to: string, action: string, handler: HookHandler): Machine<mDT>;
hook_any_action(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>;
Expand Down
5 changes: 5 additions & 0 deletions dist/es6/jssm.js
Expand Up @@ -750,6 +750,11 @@ class Machine {
this.set_hook({ kind: 'named', from, to, action, handler });
return this;
}
hook_any_action(handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'any action', handler });
return this;
}
hook_any_transition(handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'any transition', handler });
Expand Down
2 changes: 1 addition & 1 deletion dist/es6/version.js
@@ -1,2 +1,2 @@
const version = "5.58.0";
const version = "5.58.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.58.0";
const version = "5.58.1";

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

// whargarbl lots of these return arrays could/should be sets
/* eslint-disable complexity */
Expand Down Expand Up @@ -16635,6 +16635,11 @@ var jssm = (function (exports) {
this.set_hook({ kind: 'named', from, to, action, handler });
return this;
}
hook_any_action(handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'any action', handler });
return this;
}
hook_any_transition(handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'any transition', handler });
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 @@ -100,6 +100,7 @@ declare class Machine<mDT> {
set_hook(HookDesc: HookDescription): void;
hook(from: string, to: string, handler: HookHandler): Machine<mDT>;
hook_action(from: string, to: string, action: string, handler: HookHandler): Machine<mDT>;
hook_any_action(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>;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "jssm",
"version": "5.58.0",
"version": "5.58.1",
"engines": {
"node": ">=10.0.0"
},
Expand Down
10 changes: 10 additions & 0 deletions src/ts/jssm.ts
Expand Up @@ -1072,6 +1072,16 @@ class Machine<mDT> {



hook_any_action(handler: HookHandler): Machine<mDT> {

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

}



hook_any_transition(handler: HookHandler): Machine<mDT> {

// TODO: should this throw if setting the hook fails, or ignore it and continue?
Expand Down
19 changes: 19 additions & 0 deletions src/ts/tests/hooks.spec.ts
Expand Up @@ -194,6 +194,25 @@ test('All-action hook rejection works', () => {



test('Fluent all-action hook rejection works', () => {

const foo = sm`a 'foo' => b;`
.hook_any_action( () => false );

expect(foo.action('foo')).toBe(false);
expect(foo.state()).toBe('a');

foo.hook_any_action( () => true );

expect(foo.action('foo')).toBe(true);
expect(foo.state()).toBe('b');

});





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

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

const version: string = "5.58.0";
const version: string = "5.58.1";
export { version };

0 comments on commit 1c18d04

Please sign in to comment.