Skip to content

Commit 1d057e3

Browse files
committed
Improve docs
1 parent 2c92656 commit 1d057e3

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

readme.md

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,42 @@
1616
npm add yieldmachine
1717
```
1818

19-
## `start(machineDefinition: GeneratorFunction)`
19+
## Overview
2020

21-
Starts a machine, transitioning to its initially returned state.
21+
You define your machine using a function. For example, you could define a state machine representing a light switch. We’ll name our function `Switch`.
2222

2323
```ts
24+
function Switch() {
25+
26+
}
27+
```
28+
29+
Inside you declare each state you want as a [generator function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*).
30+
31+
Our `Switch` will have two states: `Off` and `On`. We return `Off` as that’s what we want as our initial state to be — our light is off by default.
32+
33+
```ts
34+
import { on, start } from "yieldmachine";
35+
36+
function Switch() {
37+
function* Off() {
38+
}
39+
function* On() {
40+
}
41+
42+
return Off;
43+
}
44+
```
45+
46+
Our `Switch` can be flicked on and off. The string `"FLICK"` is our event that will represent the flicking on and off of our switch.
47+
48+
When our `Switch` is `Off` and it is sent a `FLICK` event, it transitions to `On`.
49+
50+
And, when our `Switch` is `On` and it is sent a `FLICK` event, it transitions back to `Off`.
51+
52+
```ts
53+
import { on, start } from "yieldmachine";
54+
2455
function Switch() {
2556
function* Off() {
2657
yield on("FLICK", On);
@@ -33,6 +64,32 @@ function Switch() {
3364
}
3465
```
3566

67+
Now our machine is ready to be run. We pass our `Switch` to the `start` function we import from `yieldmachine`, and it will run an instance of our machine. And as we send it `"FLICK"` message, you’ll see the `current` state of our machine instance change.
68+
69+
```ts
70+
const machine = start(Switch);
71+
machine.current; // "Off"
72+
machine.next("FLICK");
73+
machine.current; // "On"
74+
machine.next("TOGGLE");
75+
machine.current; // "Off"
76+
```
77+
78+
## Benefits of Generator Functions
79+
80+
- Generator Functions are a built-in feature of JavaScript and TypeScript.
81+
- They have built-in syntax highlighting, autocompletion, and general rich language support in editors like Visual Studio Code.
82+
- Our states are represented by actual JavaScript functions. This means if we pass a state that’s either spelled incorrectly or isn’t in scope, our editor will tell us.
83+
- Generator Functions can be reused, composed, and partially applied like any function. This increases the modularity and reuse of our machine parts.
84+
- Our states use the name of the function.
85+
- Coming soon: our machine definitions can be serialized and deserialized. This means they could be generated on a back-end and sent to the front-end. They could be stored away in a database. They could even be generated dynamically on the fly.
86+
87+
## Documentation
88+
89+
### `start(machineDefinition: Function | GeneratorFunction)`
90+
91+
Starts a machine, transitioning to its initially returned state.
92+
3693
### `.current: string | Record<string, unknown>`
3794

3895
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.

0 commit comments

Comments
 (0)