Skip to content

Commit

Permalink
Merge pull request #463 from StoneCypher/FluentGlobalNamedHooks
Browse files Browse the repository at this point in the history
Fluent global named hooks, fixes StoneCypher/fsl#687
  • Loading branch information
StoneCypher committed May 12, 2022
2 parents fa860a6 + 3c0d18c commit 5f75d87
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="chart1652377526069" width="16" height="9"></canvas>
<canvas id="chart1652377906624" width="16" height="9"></canvas>
</div>
<script>
const format = (num) => {
Expand All @@ -51,18 +51,18 @@
chunked.map((chunk) => chunk.join('')).join(' ') + fractionStr
)
}
const ctx1652377526069 = document
.getElementById('chart1652377526069')
const ctx1652377906624 = document
.getElementById('chart1652377906624')
.getContext('2d')
const chart1652377526069 = new Chart(ctx1652377526069, {
const chart1652377906624 = new Chart(ctx1652377906624, {
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: [16802,1636,13307,818],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(11.687999999999994, 85%, 55%)","hsl(95.04, 85%, 55%)","hsl(5.844000000000006, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(11.687999999999994, 85%, 55%)","hsl(95.04, 85%, 55%)","hsl(5.844000000000006, 85%, 55%)"],
data: [18712,1544,13380,818],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(9.9, 85%, 55%)","hsl(85.8, 85%, 55%)","hsl(5.244000000000005, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(9.9, 85%, 55%)","hsl(85.8, 85%, 55%)","hsl(5.244000000000005, 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-12T17:45:26.069Z",
"date": "2022-05-12T17:51:46.624Z",
"version": "1.1.0",
"results": [
{
"name": "Blind cycle a traffic light 500 times by transition",
"ops": 16802,
"margin": 1.13,
"ops": 18712,
"margin": 3.67,
"percentSlower": 0
},
{
"name": "Blind cycle a hooked traffic light 500 times by transition",
"ops": 1636,
"margin": 0.64,
"percentSlower": 90.26
"ops": 1544,
"margin": 4.11,
"percentSlower": 91.75
},
{
"name": "Blind cycle a traffic light 500 times by action",
"ops": 13307,
"margin": 0.9,
"percentSlower": 20.8
"ops": 13380,
"margin": 0.92,
"percentSlower": 28.5
},
{
"name": "Blind cycle a hooked traffic light 500 times by action",
"ops": 818,
"margin": 0.94,
"percentSlower": 95.13
"margin": 0.8,
"percentSlower": 95.63
}
],
"fastest": {
Expand Down
1 change: 1 addition & 0 deletions dist/es6/jssm.d.ts
Expand Up @@ -101,6 +101,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_global_action(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>;
Expand Down
5 changes: 5 additions & 0 deletions dist/es6/jssm.js
Expand Up @@ -755,6 +755,11 @@ class Machine {
this.set_hook({ kind: 'named', from, to, action, handler });
return this;
}
hook_global_action(action, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'global action', 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 });
Expand Down
2 changes: 1 addition & 1 deletion dist/es6/version.js
@@ -1,2 +1,2 @@
const version = "5.59.0";
const version = "5.59.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.59.0";
const version = "5.59.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: 'named', from, to, action, handler });
return this;
}
hook_global_action(action, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'global action', 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 });
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.59.0";
const version = "5.59.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: 'named', from, to, action, handler });
return this;
}
hook_global_action(action, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'global action', 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 });
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> {
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_global_action(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>;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "jssm",
"version": "5.59.0",
"version": "5.59.1",
"engines": {
"node": ">=10.0.0"
},
Expand Down
10 changes: 10 additions & 0 deletions src/ts/jssm.ts
Expand Up @@ -1079,6 +1079,16 @@ class Machine<mDT> {



hook_global_action(action: string, handler: HookHandler): Machine<mDT> {

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

}



hook_any_action(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 @@ -223,6 +223,25 @@ test('All-action hook rejection works', () => {



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

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

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

foo.hook_global_action( 'foo', () => true );

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

});





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

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.59.0";
const version: string = "5.59.1";
export { version };

0 comments on commit 5f75d87

Please sign in to comment.