Skip to content

Commit 2c92656

Browse files
committed
Add more inline examples to docs
1 parent 6cc51cc commit 2c92656

File tree

2 files changed

+102
-8
lines changed

2 files changed

+102
-8
lines changed

readme.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ npm add yieldmachine
2020

2121
Starts a machine, transitioning to its initially returned state.
2222

23+
```ts
24+
function Switch() {
25+
function* Off() {
26+
yield on("FLICK", On);
27+
}
28+
function* On() {
29+
yield on("FLICK", Off);
30+
}
31+
32+
return Off;
33+
}
34+
```
35+
2336
### `.current: string | Record<string, unknown>`
2437

2538
The current state of the machine. If machines were nested then an object is returned with the parent machine as the key, and its current state as the value.
@@ -47,14 +60,95 @@ Cleans up the machine.
4760

4861
Transitions to the target state when the given event occurs.
4962

63+
```ts
64+
import { on, start } from "yieldmachine";
65+
66+
function Switch() {
67+
function* Off() {
68+
yield on("FLICK", On);
69+
yield on("TOGGLE", On);
70+
}
71+
function* On() {
72+
yield on("FLICK", Off);
73+
yield on("TOGGLE", Off);
74+
}
75+
76+
return Off;
77+
}
78+
79+
const machine = start(Switch);
80+
machine.current; // "Off"
81+
machine.next("FLICK");
82+
machine.current; // "On"
83+
machine.next("TOGGLE");
84+
machine.current; // "Off"
85+
```
86+
5087
### `enter(action: () => undefined | unknown | Promise<unknown>)`
5188

5289
Runs the provided function when this state is entered. If the function returns a promise, its value is made available in the `.results` property of the machine, keyed by the name of this passed function.
5390

91+
```ts
92+
import { start, on, enter } from "yieldmachine";
93+
94+
let onCount = 0;
95+
function recordOn() {
96+
onCount++;
97+
}
98+
99+
function Switch() {
100+
function* Off() {
101+
yield on("FLICK", On);
102+
}
103+
function* On() {
104+
yield enter(recordOn);
105+
yield on("FLICK", Off);
106+
}
107+
108+
return Off;
109+
}
110+
111+
const machine = start(Switch);
112+
machine.next("FLICK");
113+
console.log(recordOn, machine.current); // 1, "ON"
114+
machine.next("FLICK");
115+
console.log(recordOn, machine.current); // 1, "OFF"
116+
machine.next("FLICK");
117+
console.log(recordOn, machine.current); // 2, "ON"
118+
```
119+
54120
### `exit(action: () => undefined | unknown | Promise<unknown>)`
55121

56122
Runs the provided function when this state is exited.
57123

124+
```ts
125+
import { start, on, exit } from "yieldmachine";
126+
127+
let lastSessionEnded = null;
128+
function recordSessionEnd() {
129+
lastSessionEnded = new Date();
130+
}
131+
132+
function Session() {
133+
function* SignedOut() {
134+
yield on("AUTHENTICATE", SignedIn);
135+
}
136+
function* SignedIn() {
137+
yield exit(recordSessionEnd);
138+
yield on("LOG_OFF", SignedOut);
139+
}
140+
141+
return SignedOut;
142+
}
143+
144+
const machine = start(Switch);
145+
console.log(lastSessionEnded, machine.current); // null, "SignedOut"
146+
machine.next("AUTHENTICATE");
147+
console.log(lastSessionEnded, machine.current); // null, "SignedIn"
148+
machine.next("LOG_OFF");
149+
console.log(lastSessionEnded, machine.current); // (current time), "SignedOut"
150+
```
151+
58152
### `cond(predicate: () => boolean, target: GeneratorFunction)`
59153

60154
Immediately transitions to the target state if the provided predicate function returns `true`.

src/index.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -414,27 +414,27 @@ describe("Hierarchical Traffic Lights Machine", () => {
414414

415415
describe("Switch", () => {
416416
function Switch() {
417-
function* OFF() {
418-
yield on("FLICK", ON);
417+
function* Off() {
418+
yield on("FLICK", On);
419419
}
420-
function* ON() {
421-
yield on("FLICK", OFF);
420+
function* On() {
421+
yield on("FLICK", Off);
422422
}
423423

424-
return OFF;
424+
return Off;
425425
}
426426

427427
test("sending events", () => {
428428
const machine = start(Switch);
429429
expect(machine).toBeDefined();
430-
expect(machine.current).toEqual("OFF");
430+
expect(machine.current).toEqual("Off");
431431

432432
machine.next("FLICK");
433-
expect(machine.current).toEqual("ON");
433+
expect(machine.current).toEqual("On");
434434
expect(machine.changeCount).toEqual(1);
435435

436436
machine.next("FLICK");
437-
expect(machine.current).toEqual("OFF");
437+
expect(machine.current).toEqual("Off");
438438
expect(machine.changeCount).toEqual(2);
439439
});
440440
});

0 commit comments

Comments
 (0)