Skip to content

Commit 5714774

Browse files
committed
Add more yield machine docs
1 parent 85fc2d2 commit 5714774

File tree

12 files changed

+728
-259
lines changed

12 files changed

+728
-259
lines changed

.editorconfig

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
root = true
2+
3+
[*]
4+
indent_size = 2
5+
indent_style = space
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.{json,yml,md}]
12+
indent_style = space
13+
14+
[Makefile]
15+
indent_size = 2
16+
indent_style = tab

src/components/LeftSidebar/LeftSidebar.astro

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const langCode = getLanguageFromURL(currentPage);
77
88
const sidebarSections = [];
99
10-
for (const section of [SIDEBAR.parser, SIDEBAR.machines]) {
10+
for (const section of [SIDEBAR.machines, SIDEBAR.parser]) {
1111
for (const item of section) {
1212
if (item.header) {
1313
sidebarSections.push({ ...item, children: [] });

src/config.ts

+15-7
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,30 @@ export const KNOWN_LANGUAGES = {
3333
// }
3434

3535
export const SIDEBAR = {
36-
parser: [
37-
{ text: 'Parser', header: true },
38-
{ text: 'Introduction', link: 'parser' },
39-
{ text: 'Routes', link: 'parser/routes' },
40-
],
4136
machines: [
42-
{ text: 'Machines', header: true },
37+
{ text: 'YieldMachine', header: true },
4338
{ text: 'Introduction', link: 'machines' },
39+
{ text: 'Primitives', link: 'machines/primitives' },
40+
{ text: 'Working with promises', link: 'machines/promises' },
41+
{ text: 'Entry and exit callbacks', link: 'machines/entry-exit' },
42+
{ text: 'Reading properties from the received event', link: 'machines/event-props' },
43+
{ text: 'Listening to external events', link: 'machines/external-events' },
44+
{ text: 'Nested machines', link: 'machines/nested-machines' },
45+
{ text: 'Examples', link: 'machines/examples' },
4446
{ text: 'Annotated source', link: 'machines/source' },
4547
],
48+
parser: [
49+
{ text: 'YieldParser', header: true },
50+
{ text: 'Introduction', link: 'parser' },
51+
{ text: 'Routes', link: 'parser/routes' },
52+
{ text: 'HTTP Helpers', link: 'parser/routes' },
53+
],
4654
en: [
4755
{ text: 'Section Header', header: true },
4856
{ text: 'Introduction', link: 'en/introduction' },
4957
{ text: 'Page 2', link: 'en/page-2' },
5058
{ text: 'Page 3', link: 'en/page-3' },
51-
59+
5260
{ text: 'Another Section', header: true },
5361
{ text: 'Page 4', link: 'en/page-4' },
5462
]

src/pages/machines/entry-exit.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: Entry & exit callbacks in YieldMachine
3+
description: Know when a state is entered or exited
4+
layout: ../../layouts/MainLayout.astro
5+
---

src/pages/machines/event-props.md

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
title: Listening to external events with YieldMachine
3+
description: Listening to external events
4+
layout: ../../layouts/MainLayout.astro
5+
---
6+
7+
```js
8+
function KeyShortcutListener(el: HTMLElement) {
9+
function isEscapeKey(readContext: ReadContextCallback) {
10+
const event = readContext("event");
11+
return event instanceof KeyboardEvent && event.key === "Escape";
12+
}
13+
function isEnterKey(readContext: ReadContextCallback) {
14+
const event = readContext("event");
15+
return event instanceof KeyboardEvent && event.key === "Enter";
16+
}
17+
18+
const openChoiceKeydown = new Map([
19+
[isEscapeKey, Closed],
20+
[null, Open as any],
21+
]);
22+
const closedChoiceKeydown = new Map([
23+
[isEnterKey, Open],
24+
[null, Closed as any],
25+
]);
26+
27+
function* Open() {
28+
yield on("keydown", openChoiceKeydown);
29+
yield listenTo(el, ["keydown"]);
30+
}
31+
function* Closed() {
32+
yield on("keydown", closedChoiceKeydown);
33+
yield listenTo(el, ["keydown"]);
34+
}
35+
36+
return Closed;
37+
}
38+
39+
const aborter = new AbortController();
40+
const input = document.createElement("input");
41+
const machine = start(KeyShortcutListener.bind(null, input), {
42+
signal: aborter.signal,
43+
});
44+
machine.value.state; // "Closed"
45+
46+
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter" }));
47+
expect(machine.value).toMatchObject({
48+
state: "Open",
49+
change: 1,
50+
});
51+
52+
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter" }));
53+
expect(machine.value).toMatchObject({
54+
state: "Open",
55+
change: 1,
56+
});
57+
58+
input.dispatchEvent(new KeyboardEvent("keydown", { key: "a" }));
59+
expect(machine.value).toMatchObject({
60+
state: "Open",
61+
change: 1,
62+
});
63+
64+
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape" }));
65+
expect(machine.value).toMatchObject({
66+
state: "Closed",
67+
change: 2,
68+
});
69+
70+
input.dispatchEvent(new KeyboardEvent("keydown", { key: "a" }));
71+
expect(machine.value).toMatchObject({
72+
state: "Closed",
73+
change: 2,
74+
});
75+
```

0 commit comments

Comments
 (0)