Skip to content

Commit

Permalink
Adding hooks to the readme, no big
Browse files Browse the repository at this point in the history
  • Loading branch information
StoneCypher committed May 13, 2022
1 parent 0f1535b commit ee39784
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 94 deletions.
45 changes: 33 additions & 12 deletions README.md
Expand Up @@ -42,30 +42,51 @@ log( TrafficLight.state() ); // 'Green'
What if the notation supported action names easily?

```javascript
const TrafficLightWithActions = sm`Red 'next' -> Green 'next' -> Yellow 'next' -> Red;`;
const TLWA = sm`Red 'next' -> Green 'next' -> Yellow 'next' -> Red;`; // TLWA = Traffic Light With Actions

log( TrafficLightWithActions.state() ); // 'Red'
log( TLWA.state() ); // 'Red'

TrafficLightWithActions.action('next'); // true
log( TrafficLightWithActions.state() ); // 'Green'
TLWA.action('next'); // true
log( TLWA.state() ); // 'Green'

TrafficLightWithActions.action('next'); // true
log( TrafficLightWithActions.state() ); // 'Yellow'
TLWA.action('next'); // true
log( TLWA.state() ); // 'Yellow'

TrafficLightWithActions.action('next'); // true
log( TrafficLightWithActions.state() ); // 'Red'
TLWA.action('next'); // true
log( TLWA.state() ); // 'Red'
```

<br/>

What if integration with the outside was straightforward?

```javascript
const MTL = sm`Red 'next' -> Green 'next' -> Yellow 'next' -> Red;` // MTL = More Traffic Lights
.hook('Red', 'Green', () => console.log('GO GO GO')) // node will jump the gun when you hit return, though
.hook_entry('Red', () => console.log('STOP')); // so put it on one line in node

log( MTL.state() ); // 'Red'

TLWA.action('next'); // true, console logs 'GO GO GO'
log( TLWA.state() ); // 'Green'

TLWA.action('next'); // true
log( TLWA.state() ); // 'Yellow'

TLWA.action('next'); // true, console logs 'STOP'
log( TLWA.state() ); // 'Red'
```

<br/>

What if the machine followed JS standards, and distinguished refusals as `false` from mistakes as `throw`n?

```javascript
const AnotherTrafficLight = sm`Red -> Green -> Yellow -> Red;`;
const ATL = sm`Red -> Green -> Yellow -> Red;`; // ATL = Another Traffic Light

log( AnotherTrafficLight.state() ); // 'Red' - uses 1st state unless told otherwise
AnotherTrafficLight.transition('Yellow'); // false (Yellow isn't allowed from Red)
AnotherTrafficLight.transition('Blue'); // throws (Blue isn't a state at all)
log( ATL.state() ); // 'Red' - uses 1st state unless told otherwise
ATL.transition('Yellow'); // false (Yellow isn't allowed from Red)
ATL.transition('Blue'); // throws (Blue isn't a state at all)
```

<br/>
Expand Down
14 changes: 7 additions & 7 deletions benchmark/results/general.chart.html
Expand Up @@ -28,7 +28,7 @@
</head>
<body>
<div class="container">
<canvas id="chart1652399505339" width="16" height="9"></canvas>
<canvas id="chart1652463799674" width="16" height="9"></canvas>
</div>
<script>
const format = (num) => {
Expand All @@ -51,18 +51,18 @@
chunked.map((chunk) => chunk.join('')).join(' ') + fractionStr
)
}
const ctx1652399505339 = document
.getElementById('chart1652399505339')
const ctx1652463799674 = document
.getElementById('chart1652463799674')
.getContext('2d')
const chart1652399505339 = new Chart(ctx1652399505339, {
const chart1652463799674 = new Chart(ctx1652463799674, {
type: 'bar',
data: {
labels: ["Blind cycle a traffic light 500 times by transition","Blind cycle a traffic light 500 times by action","Blind cycle a basic-hooked traffic light 500 times by transition","Blind cycle a named-hooked traffic light 500 times by transition","Blind cycle an any-transition traffic light 500 times by transition","Blind cycle an exit hooked traffic light 500 times by transition","Blind cycle an enter hooked traffic light 500 times by transition","Blind cycle a standard-transition hooked light by transition","Blind cycle a main-transition hooked light by transition","Blind cycle a force-transition hooked light by transition","Blind cycle a traffic light 500 times by action","Blind cycle a basic-hooked traffic light 500 times by action","Blind cycle a named-hooked traffic light 500 times by action","Blind cycle an any-action traffic light 500 times by action","Blind cycle a global-action traffic light 500 times by action","Blind cycle an exit hooked traffic light 500 times by action","Blind cycle an enter hooked traffic light 500 times by action","Blind cycle a standard transition tl 500 times by action","Blind cycle a main transition tl 500 times by action","Blind cycle a forced transition tl 500 times by action"],
datasets: [
{
data: [6018,12290,1207,1173,1267,1243,1246,1256,1259,24351,12645,796,750,819,815,822,824,60076,52154,57609],
backgroundColor: ["hsl(12.023999999999994, 85%, 55%)","hsl(24.551999999999992, 85%, 55%)","hsl(2.412000000000006, 85%, 55%)","hsl(2.3400000000000034, 85%, 55%)","hsl(2.531999999999999, 85%, 55%)","hsl(2.4839999999999915, 85%, 55%)","hsl(2.4839999999999915, 85%, 55%)","hsl(2.508000000000004, 85%, 55%)","hsl(2.519999999999993, 85%, 55%)","hsl(48.635999999999996, 85%, 55%)","hsl(25.259999999999994, 85%, 55%)","hsl(1.5839999999999919, 85%, 55%)","hsl(1.5, 85%, 55%)","hsl(1.6319999999999992, 85%, 55%)","hsl(1.6319999999999992, 85%, 55%)","hsl(1.6440000000000055, 85%, 55%)","hsl(1.6440000000000055, 85%, 55%)","hsl(120, 85%, 55%)","hsl(104.172, 85%, 55%)","hsl(115.068, 85%, 55%)"],
borderColor: ["hsl(12.023999999999994, 85%, 55%)","hsl(24.551999999999992, 85%, 55%)","hsl(2.412000000000006, 85%, 55%)","hsl(2.3400000000000034, 85%, 55%)","hsl(2.531999999999999, 85%, 55%)","hsl(2.4839999999999915, 85%, 55%)","hsl(2.4839999999999915, 85%, 55%)","hsl(2.508000000000004, 85%, 55%)","hsl(2.519999999999993, 85%, 55%)","hsl(48.635999999999996, 85%, 55%)","hsl(25.259999999999994, 85%, 55%)","hsl(1.5839999999999919, 85%, 55%)","hsl(1.5, 85%, 55%)","hsl(1.6319999999999992, 85%, 55%)","hsl(1.6319999999999992, 85%, 55%)","hsl(1.6440000000000055, 85%, 55%)","hsl(1.6440000000000055, 85%, 55%)","hsl(120, 85%, 55%)","hsl(104.172, 85%, 55%)","hsl(115.068, 85%, 55%)"],
data: [6679.1,13893.5,1373.2,1403.7,1383.6,1372.9,1396.9,1313.3,1368.2,22647.7,14244.6,820.6,827.9,833,839.8,832.5,793.2,70442.4,71922.3,71420.9],
backgroundColor: ["hsl(11.148000000000009, 85%, 55%)","hsl(23.18399999999999, 85%, 55%)","hsl(2.291999999999996, 85%, 55%)","hsl(2.3400000000000034, 85%, 55%)","hsl(2.304000000000002, 85%, 55%)","hsl(2.291999999999996, 85%, 55%)","hsl(2.327999999999997, 85%, 55%)","hsl(2.195999999999998, 85%, 55%)","hsl(2.280000000000007, 85%, 55%)","hsl(37.788, 85%, 55%)","hsl(23.772000000000002, 85%, 55%)","hsl(1.3680000000000008, 85%, 55%)","hsl(1.3800000000000068, 85%, 55%)","hsl(1.391999999999996, 85%, 55%)","hsl(1.4040000000000021, 85%, 55%)","hsl(1.391999999999996, 85%, 55%)","hsl(1.3199999999999932, 85%, 55%)","hsl(117.52799999999999, 85%, 55%)","hsl(120, 85%, 55%)","hsl(119.16, 85%, 55%)"],
borderColor: ["hsl(11.148000000000009, 85%, 55%)","hsl(23.18399999999999, 85%, 55%)","hsl(2.291999999999996, 85%, 55%)","hsl(2.3400000000000034, 85%, 55%)","hsl(2.304000000000002, 85%, 55%)","hsl(2.291999999999996, 85%, 55%)","hsl(2.327999999999997, 85%, 55%)","hsl(2.195999999999998, 85%, 55%)","hsl(2.280000000000007, 85%, 55%)","hsl(37.788, 85%, 55%)","hsl(23.772000000000002, 85%, 55%)","hsl(1.3680000000000008, 85%, 55%)","hsl(1.3800000000000068, 85%, 55%)","hsl(1.391999999999996, 85%, 55%)","hsl(1.4040000000000021, 85%, 55%)","hsl(1.391999999999996, 85%, 55%)","hsl(1.3199999999999932, 85%, 55%)","hsl(117.52799999999999, 85%, 55%)","hsl(120, 85%, 55%)","hsl(119.16, 85%, 55%)"],
borderWidth: 2,
},
],
Expand Down
128 changes: 64 additions & 64 deletions benchmark/results/general.json
@@ -1,135 +1,135 @@
{
"name": "General performance suite",
"date": "2022-05-12T23:51:45.339Z",
"date": "2022-05-13T17:43:19.674Z",
"version": "1.2.0",
"results": [
{
"name": "Blind cycle a traffic light 500 times by transition",
"ops": 6018,
"margin": 1.96,
"percentSlower": 89.98
"ops": 6679.1,
"margin": 1.14,
"percentSlower": 90.71
},
{
"name": "Blind cycle a traffic light 500 times by action",
"ops": 12290,
"margin": 2.53,
"percentSlower": 79.54
"ops": 13893.5,
"margin": 0.87,
"percentSlower": 80.68
},
{
"name": "Blind cycle a basic-hooked traffic light 500 times by transition",
"ops": 1207,
"margin": 4.7,
"percentSlower": 97.99
"ops": 1373.2,
"margin": 0.95,
"percentSlower": 98.09
},
{
"name": "Blind cycle a named-hooked traffic light 500 times by transition",
"ops": 1173,
"margin": 5.8,
"ops": 1403.7,
"margin": 1.11,
"percentSlower": 98.05
},
{
"name": "Blind cycle an any-transition traffic light 500 times by transition",
"ops": 1267,
"margin": 1.77,
"percentSlower": 97.89
"ops": 1383.6,
"margin": 1.25,
"percentSlower": 98.08
},
{
"name": "Blind cycle an exit hooked traffic light 500 times by transition",
"ops": 1243,
"margin": 2.17,
"percentSlower": 97.93
"ops": 1372.9,
"margin": 2,
"percentSlower": 98.09
},
{
"name": "Blind cycle an enter hooked traffic light 500 times by transition",
"ops": 1246,
"margin": 1.63,
"percentSlower": 97.93
"ops": 1396.9,
"margin": 1.02,
"percentSlower": 98.06
},
{
"name": "Blind cycle a standard-transition hooked light by transition",
"ops": 1256,
"margin": 1.56,
"percentSlower": 97.91
"ops": 1313.3,
"margin": 3.79,
"percentSlower": 98.17
},
{
"name": "Blind cycle a main-transition hooked light by transition",
"ops": 1259,
"margin": 1.59,
"percentSlower": 97.9
"ops": 1368.2,
"margin": 0.87,
"percentSlower": 98.1
},
{
"name": "Blind cycle a force-transition hooked light by transition",
"ops": 24351,
"margin": 1.65,
"percentSlower": 59.47
"ops": 22647.7,
"margin": 1.02,
"percentSlower": 68.51
},
{
"name": "Blind cycle a traffic light 500 times by action",
"ops": 12645,
"margin": 1.85,
"percentSlower": 78.95
"ops": 14244.6,
"margin": 0.85,
"percentSlower": 80.19
},
{
"name": "Blind cycle a basic-hooked traffic light 500 times by action",
"ops": 796,
"margin": 1.36,
"percentSlower": 98.68
"ops": 820.6,
"margin": 0.92,
"percentSlower": 98.86
},
{
"name": "Blind cycle a named-hooked traffic light 500 times by action",
"ops": 750,
"margin": 6.79,
"percentSlower": 98.75
"ops": 827.9,
"margin": 0.84,
"percentSlower": 98.85
},
{
"name": "Blind cycle an any-action traffic light 500 times by action",
"ops": 819,
"margin": 1.27,
"percentSlower": 98.64
"ops": 833,
"margin": 0.62,
"percentSlower": 98.84
},
{
"name": "Blind cycle a global-action traffic light 500 times by action",
"ops": 815,
"margin": 1.92,
"percentSlower": 98.64
"ops": 839.8,
"margin": 0.9,
"percentSlower": 98.83
},
{
"name": "Blind cycle an exit hooked traffic light 500 times by action",
"ops": 822,
"margin": 1,
"percentSlower": 98.63
"ops": 832.5,
"margin": 0.53,
"percentSlower": 98.84
},
{
"name": "Blind cycle an enter hooked traffic light 500 times by action",
"ops": 824,
"margin": 0.68,
"percentSlower": 98.63
"ops": 793.2,
"margin": 1.72,
"percentSlower": 98.9
},
{
"name": "Blind cycle a standard transition tl 500 times by action",
"ops": 60076,
"margin": 3.91,
"percentSlower": 0
"ops": 70442.4,
"margin": 2.46,
"percentSlower": 2.06
},
{
"name": "Blind cycle a main transition tl 500 times by action",
"ops": 52154,
"margin": 7.86,
"percentSlower": 13.19
"ops": 71922.3,
"margin": 1.01,
"percentSlower": 0
},
{
"name": "Blind cycle a forced transition tl 500 times by action",
"ops": 57609,
"margin": 2.65,
"percentSlower": 4.11
"ops": 71420.9,
"margin": 1.06,
"percentSlower": 0.7
}
],
"fastest": {
"name": "Blind cycle a standard transition tl 500 times by action",
"index": 17
"name": "Blind cycle a main transition tl 500 times by action",
"index": 18
},
"slowest": {
"name": "Blind cycle a named-hooked traffic light 500 times by action",
"index": 12
"name": "Blind cycle an enter hooked traffic light 500 times by action",
"index": 16
}
}
2 changes: 1 addition & 1 deletion dist/es6/version.js
@@ -1,2 +1,2 @@
const version = "5.61.1";
const version = "5.60.5";
export { version };
2 changes: 1 addition & 1 deletion dist/jssm.es5.cjs.js

Large diffs are not rendered by default.

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

const version = "5.61.1";
const version = "5.60.5";

// whargarbl lots of these return arrays could/should be sets
/* eslint-disable complexity */
Expand Down
2 changes: 1 addition & 1 deletion dist/jssm.es5.iife.js

Large diffs are not rendered by default.

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

const version = "5.61.1";
const version = "5.60.5";

// whargarbl lots of these return arrays could/should be sets
/* eslint-disable complexity */
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/classes/Machine.html

Large diffs are not rendered by default.

0 comments on commit ee39784

Please sign in to comment.