Skip to content

Commit

Permalink
Convenience hook action call in fluent api
Browse files Browse the repository at this point in the history
  • Loading branch information
StoneCypher committed May 5, 2022
1 parent fa1bf46 commit 4f44dff
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 49 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="chart1651779155663" width="16" height="9"></canvas>
<canvas id="chart1651781613072" width="16" height="9"></canvas>
</div>
<script>
const format = (num) => {
Expand All @@ -51,18 +51,18 @@
chunked.map((chunk) => chunk.join('')).join(' ') + fractionStr
)
}
const ctx1651779155663 = document
.getElementById('chart1651779155663')
const ctx1651781613072 = document
.getElementById('chart1651781613072')
.getContext('2d')
const chart1651779155663 = new Chart(ctx1651779155663, {
const chart1651781613072 = new Chart(ctx1651781613072, {
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: [19086,1615,13476,1429],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(10.151999999999992, 85%, 55%)","hsl(84.732, 85%, 55%)","hsl(8.987999999999994, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(10.151999999999992, 85%, 55%)","hsl(84.732, 85%, 55%)","hsl(8.987999999999994, 85%, 55%)"],
data: [19593,1623,13488,1380],
backgroundColor: ["hsl(120, 85%, 55%)","hsl(9.936000000000002, 85%, 55%)","hsl(82.608, 85%, 55%)","hsl(8.448000000000008, 85%, 55%)"],
borderColor: ["hsl(120, 85%, 55%)","hsl(9.936000000000002, 85%, 55%)","hsl(82.608, 85%, 55%)","hsl(8.448000000000008, 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-05T19:32:35.663Z",
"date": "2022-05-05T20:13:33.072Z",
"version": "1.1.0",
"results": [
{
"name": "Blind cycle a traffic light 500 times by transition",
"ops": 19086,
"margin": 1.86,
"ops": 19593,
"margin": 2.72,
"percentSlower": 0
},
{
"name": "Blind cycle a hooked traffic light 500 times by transition",
"ops": 1615,
"margin": 2.44,
"percentSlower": 91.54
"ops": 1623,
"margin": 1.62,
"percentSlower": 91.72
},
{
"name": "Blind cycle a traffic light 500 times by action",
"ops": 13476,
"margin": 3.02,
"percentSlower": 29.39
"ops": 13488,
"margin": 1.34,
"percentSlower": 31.16
},
{
"name": "Blind cycle a hooked traffic light 500 times by action",
"ops": 1429,
"margin": 1.4,
"percentSlower": 92.51
"ops": 1380,
"margin": 1.86,
"percentSlower": 92.96
}
],
"fastest": {
Expand Down
1 change: 1 addition & 0 deletions dist/es6/jssm.d.ts
Expand Up @@ -91,6 +91,7 @@ declare class Machine<mDT> {
has_completes(): boolean;
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>;
action(name: StateType, newData?: mDT): boolean;
transition(newState: StateType, newData?: mDT): boolean;
force_transition(newState: StateType, newData?: mDT): boolean;
Expand Down
5 changes: 5 additions & 0 deletions dist/es6/jssm.js
Expand Up @@ -725,6 +725,11 @@ class Machine {
this.set_hook({ kind: 'hook', from, to, handler });
return this;
}
hook_action(from, to, action, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'named', from, to, action, 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.46.0";
const version = "5.47.0";
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 @@ -15887,7 +15887,7 @@ function peg$parse(input, options) {
}
}

const version = "5.46.0";
const version = "5.47.0";

// whargarbl lots of these return arrays could/should be sets
/* eslint-disable complexity */
Expand Down Expand Up @@ -16612,6 +16612,11 @@ class Machine {
this.set_hook({ kind: 'hook', from, to, handler });
return this;
}
hook_action(from, to, action, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'named', from, to, action, 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 @@ -15886,7 +15886,7 @@ var jssm = (function (exports) {
}
}

const version = "5.46.0";
const version = "5.47.0";

// whargarbl lots of these return arrays could/should be sets
/* eslint-disable complexity */
Expand Down Expand Up @@ -16611,6 +16611,11 @@ var jssm = (function (exports) {
this.set_hook({ kind: 'hook', from, to, handler });
return this;
}
hook_action(from, to, action, handler) {
// TODO: should this throw if setting the hook fails, or ignore it and continue?
this.set_hook({ kind: 'named', from, to, action, 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 @@ -91,6 +91,7 @@ declare class Machine<mDT> {
has_completes(): boolean;
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>;
action(name: StateType, newData?: mDT): boolean;
transition(newState: StateType, newData?: mDT): boolean;
force_transition(newState: StateType, newData?: mDT): boolean;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "jssm",
"version": "5.46.0",
"version": "5.47.0",
"engines": {
"node": ">=10.0.0"
},
Expand Down
10 changes: 10 additions & 0 deletions src/ts/jssm.ts
Expand Up @@ -1027,6 +1027,16 @@ class Machine<mDT> {



hook_action(from: string, to: string, action: string, handler: HookHandler): Machine<mDT> {

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

}



// remove_hook(HookDesc: HookDescription) {
// throw 'TODO: Should remove hook here';
// }
Expand Down
51 changes: 31 additions & 20 deletions src/ts/tests/hooks.spec.ts
Expand Up @@ -303,26 +303,37 @@ describe('Basic hooks on fluent API', () => {



// test('Named hooks call their handler', () => {

// const handler = jest.fn(x => true),
// uncalled = jest.fn(x => true);

// expect( () => {
// const _foo = sm`a 'next' -> b 'next' -> c;`;
// _foo.hook_action('a', 'b', handler, 'next');
// _foo.hook_action('a', 'b', uncalled, 'borg');
// _foo.hook_action('b', 'a', uncalled, 'next');
// _foo.action('next');
// _foo.action('next');
// })
// .not.toThrow();

// // should hook from first, but not from second
// expect(handler.mock.calls.length).toBe(1);
// expect(uncalled.mock.calls.length).toBe(0);

// } );
test('Named hooks call their handler', () => {

const handler = jest.fn(x => true),
handler2 = jest.fn(x => true),
handler3 = jest.fn(x => true),
uncalled = jest.fn(x => true);

expect( () => {

const _foo = sm`a 'next' -> b 'next' -> c 'next' -> d 'next' -> e;`
.hook_action('c', 'd', 'next', handler3)
.hook_action('d', 'c', 'next', uncalled)
.hook_action('d', 'e', 'borg', uncalled);

_foo.hook_action('a', 'b', 'next', handler);
_foo.hook_action('a', 'b', 'borg', uncalled);
_foo.hook_action('b', 'a', 'next', uncalled);

_foo.action('next');
_foo.action('next');
_foo.action('next');
_foo.action('next');

})
.not.toThrow();

// should hook from first, but not from second
expect(handler.mock.calls.length).toBe(1);
expect(uncalled.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.46.0";
const version: string = "5.47.0";
export { version };

0 comments on commit 4f44dff

Please sign in to comment.