@@ -10,13 +10,13 @@ A fluent JavaScript State Machine (with TypeScript support)
1010
1111Install package
1212
13- ```
13+ ``` Shell
1414npm i @2toad/fluent-state
1515```
1616
1717## Usage
1818
19- ```
19+ ``` JavaScript
2020import { fluentState } from ' @2toad/fluent-state' ;
2121// or
2222var fluentState = require (' @2toad/fluent-state' ).fluentState ;
@@ -45,15 +45,15 @@ The current state
4545#### from(name: string): State
4646Adds a state
4747
48- ```
48+ ``` JavaScript
4949// Add the 'vegetable' state
5050fluentState .from (' vegetable' );
5151```
5252
5353#### to(name: string): Transition
5454Adds a transition to a state
5555
56- ```
56+ ``` JavaScript
5757fluentState
5858 .from (' vegetable' ) // Add the 'vegetable' state
5959 .to (' diced' ); // add the 'diced' state, with a transtion from 'vegetable'
@@ -62,7 +62,7 @@ fluentState
6262#### or(name: string): Transition
6363Adds a transition to a state
6464
65- ```
65+ ``` JavaScript
6666fluentState
6767 .from (' vegetable' ) // Add the 'vegetable' state
6868 .to (' diced' ) // add the 'diced' state, with a transtion from 'vegetable'
@@ -73,7 +73,7 @@ fluentState
7373#### setState(name: string): void
7474Explicitly set the state without triggering a transition
7575
76- ```
76+ ``` JavaScript
7777fluentState .setState (' diced' );
7878```
7979
@@ -82,21 +82,21 @@ fluentState.setState('diced');
8282#### has(name: string): boolean {
8383Returns true if the state exists
8484
85- ```
85+ ``` JavaScript
8686fluentState .has (' vegetable' );
8787```
8888
8989### remove(name: string): void
9090Removes a state (and all of its transitions)
9191
92- ```
92+ ``` JavaScript
9393fluentState .remove (' vegetable' );
9494```
9595
9696#### clear(): void
9797Removes all states
9898
99- ```
99+ ``` JavaScript
100100fluentState .clear ();
101101```
102102
@@ -105,7 +105,7 @@ fluentState.clear();
105105- If multiple states are specified, a state is chosen at random.
106106- Returns ` true ` upon success.
107107
108- ```
108+ ``` JavaScript
109109// Transition to the 'diced' state
110110fluentState .transition (' diced' );
111111
@@ -119,7 +119,7 @@ fluentState.transition('diced', 'discarded');
119119 - With the option to exclude specified states from the random selection.
120120- Returns ` true ` upon success.
121121
122- ```
122+ ``` JavaScript
123123fluentState .next ();
124124
125125// A random state, excluding 'pickled' and 'discarded'
@@ -132,14 +132,14 @@ You can add callbacks to any state
132132#### when(name: string): Event
133133Specifies the state you want to add a callback to
134134
135- ```
135+ ``` JavaScript
136136fluentState .when (' diced' );
137137```
138138
139139#### do(handler: (previousState: State, fluentState: FluentState) => any): Handler
140140Adds a callback
141141
142- ```
142+ ``` JavaScript
143143fluentState
144144 .when (' diced' )
145145 .do ((previousState , fluentState ) => {
@@ -150,7 +150,7 @@ fluentState
150150#### and(handler: (previousState: State, fluentState: FluentState) => any): Handler
151151Adds another callback
152152
153- ```
153+ ``` JavaScript
154154fluentState
155155 .when (' diced' )
156156 .do (() => console .log (' Transitioned to "diced"' ))
@@ -161,7 +161,7 @@ fluentState
161161
162162> And of course it's all chainable
163163
164- ```
164+ ``` JavaScript
165165fluentState
166166 .when (' diced' ).do (() => console .log (' Transitioned to "diced"' ))
167167 .when (' pickled' ).do (() => console .log (' Transitioned to "pickled"' ));
@@ -170,7 +170,7 @@ fluentState
170170### Lifecycle
171171You can hook into the state machine lifecycle via the ` observe ` method.
172172
173- ```
173+ ``` JavaScript
174174fluentState .observe (Lifecycle .BeforeTransition , (currentState , newState ) => {
175175 // You can prevent the transition by returning false from this event
176176 return false ;
0 commit comments