You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: readme.md
+59-2Lines changed: 59 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,11 +16,42 @@
16
16
npm add yieldmachine
17
17
```
18
18
19
-
## `start(machineDefinition: GeneratorFunction)`
19
+
## Overview
20
20
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`.
22
22
23
23
```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
+
returnOff;
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
+
24
55
function Switch() {
25
56
function* Off() {
26
57
yieldon("FLICK", On);
@@ -33,6 +64,32 @@ function Switch() {
33
64
}
34
65
```
35
66
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
+
36
93
### `.current: string | Record<string, unknown>`
37
94
38
95
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