Skip to content

Commit 2c57e07

Browse files
committed
Add better docs
1 parent a1a86ce commit 2c57e07

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

readme.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,66 @@
1616
npm add yieldmachine
1717
```
1818

19+
## `start(machineDefinition: GeneratorFunction)`
20+
21+
Starts a machine, transitioning to its initially returned state.
22+
23+
### `.current: string | Record<string, unknown>`
24+
25+
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.
26+
27+
### `.changeCount: number`
28+
29+
The number of times this machine has transitioned. Useful for consumers updating only when changes have been made.
30+
31+
### `.results: Promise<unknown>`
32+
33+
The result of any `entry()` or `exit()` messages.
34+
35+
### `.next(eventName: string | symbol)`
36+
37+
Sends an event to the machine, transitioning if the event was recognised. Unrecognised events are ignored.
38+
39+
40+
## Messages
41+
42+
### `on(eventName: string | symbol, target: GeneratorFunction)`
43+
44+
### `enter(action: () => undefined | unknown | Promise<unknown>)`
45+
46+
### `exit(action: () => undefined | unknown | Promise<unknown>)`
47+
48+
### `cond(predicate: () => boolean, target: GeneratorFunction)`
49+
50+
### `always(target: GeneratorFunction)`
51+
52+
### `listenTo(sender: EventTarget, eventName: string)`
53+
54+
Listens to an `EventTarget` — for example, an HTMLElement like a button.
55+
56+
Uses [`.addEventListener()`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) to listen to the event. The listener is removed when transitioning to a different state, so no extra clean up is necessary.
57+
58+
```ts
59+
function ButtonClickListener(button: HTMLButtonElement) {
60+
function* initial() {
61+
yield on("click", clicked);
62+
yield listenTo(button, "click");
63+
}
64+
function* clicked() {}
65+
66+
return initial;
67+
}
68+
69+
const button = document.createElement('button');
70+
const machine = start(ButtonClickListener.bind(null, button));
71+
72+
machine.current; // "initial"
73+
button.click();
74+
machine.current; // "clicked"
75+
button.click();
76+
machine.current; // "clicked"
77+
```
78+
1979
## Examples
2080

2181
### HTTP Loader
@@ -118,6 +178,30 @@ loader.results.then((results) => {
118178
});
119179
```
120180

181+
### `AbortController` wrapper
182+
183+
```ts
184+
function AbortListener(controller: AbortController) {
185+
function* initial() {
186+
if (controller.signal.aborted) {
187+
yield always(aborted);
188+
} else {
189+
yield on("abort", aborted);
190+
yield listenTo(controller.signal, "abort");
191+
}
192+
}
193+
function* aborted() {}
194+
195+
return initial;
196+
}
197+
198+
const aborter = new AbortController();
199+
const machine = start(AbortListener.bind(null, aborter));
200+
201+
machine.current; // "initial"
202+
aborter.abort();
203+
machine.current; // "aborted"
204+
```
121205

122206
----
123207

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function exit(f: ActionBody): ExitAction {
7979
return { type: "exit", f };
8080
}
8181

82-
export function listenTo<Event extends string>(sender: EventTarget, eventName: Event): ListenTo {
82+
export function listenTo(sender: EventTarget, eventName: string): ListenTo {
8383
return { type: "listenTo", sender, eventName };
8484
}
8585

0 commit comments

Comments
 (0)