|
16 | 16 | npm add yieldmachine
|
17 | 17 | ```
|
18 | 18 |
|
| 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 | + |
19 | 79 | ## Examples
|
20 | 80 |
|
21 | 81 | ### HTTP Loader
|
@@ -118,6 +178,30 @@ loader.results.then((results) => {
|
118 | 178 | });
|
119 | 179 | ```
|
120 | 180 |
|
| 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 | +``` |
121 | 205 |
|
122 | 206 | ----
|
123 | 207 |
|
|
0 commit comments