@@ -29,30 +29,36 @@ export const donutStore = createStore({
29
29
favoriteFlavor: ' chocolate'
30
30
},
31
31
on: {
32
- addDonut: {
33
- donuts : (context ) => context .donuts + 1
34
- },
35
- changeFlavor: {
36
- favoriteFlavor : (context , event : { flavor: string }) => event .flavor
37
- },
38
- eatAllDonuts: {
32
+ addDonut : (context ) => ({
33
+ ... context ,
34
+ donuts: context .donuts + 1
35
+ }),
36
+ changeFlavor : (context , event : { flavor: string }) => ({
37
+ ... context ,
38
+ favoriteFlavor: event .flavor
39
+ }),
40
+ eatAllDonuts : (context ) => ({
41
+ ... context ,
39
42
donuts: 0
40
- }
43
+ })
41
44
}
42
45
});
43
46
44
47
donutStore .subscribe ((snapshot ) => {
45
48
console .log (snapshot .context );
46
49
});
47
50
48
- donutStore .send ({ type: ' addDonut' });
51
+ // Equivalent to
52
+ // donutStore.send({ type: 'addDonut' });
53
+ donutStore .trigger .addDonut ();
49
54
// => { donuts: 1, favoriteFlavor: 'chocolate' }
50
55
51
- donutStore .send ({
52
- type: ' changeFlavor' ,
53
- flavor: ' strawberry' // Strongly-typed!
54
- });
55
- // => { donuts: 1, favoriteFlavor: 'chocolate' }
56
+ // donutStore.send({
57
+ // type: 'changeFlavor',
58
+ // flavor: 'strawberry' // Strongly-typed!
59
+ // });
60
+ donutStore .trigger .changeFlavor ({ flavor: ' strawberry' });
61
+ // => { donuts: 1, favoriteFlavor: 'strawberry' }
56
62
```
57
63
58
64
<details >
@@ -215,3 +221,61 @@ const donutStore = createStore({
215
221
}
216
222
});
217
223
```
224
+
225
+ ## Effects and Side Effects
226
+
227
+ You can enqueue effects in state transitions using the ` enqueue ` argument:
228
+
229
+ ``` ts
230
+ import { createStore } from ' @xstate/store' ;
231
+
232
+ const store = createStore ({
233
+ context: { count: 0 },
234
+ on: {
235
+ incrementDelayed : (context , event , enqueue ) => {
236
+ enqueue .effect (async () => {
237
+ await new Promise ((resolve ) => setTimeout (resolve , 1000 ));
238
+ store .send ({ type: ' increment' });
239
+ });
240
+
241
+ return context ;
242
+ },
243
+ increment : (context ) => ({
244
+ ... context ,
245
+ count: context .count + 1
246
+ })
247
+ }
248
+ });
249
+ ```
250
+
251
+ ## Emitting Events
252
+
253
+ You can emit events from transitions by defining them in the ` emits ` property and using ` enqueue.emit ` :
254
+
255
+ ``` ts
256
+ import { createStore } from ' @xstate/store' ;
257
+
258
+ const store = createStore ({
259
+ context: { count: 0 },
260
+ emits: {
261
+ increased : (payload : { by: number }) => {
262
+ // Optional side effects can go here
263
+ }
264
+ },
265
+ on: {
266
+ inc : (context , event : { by: number }, enqueue ) => {
267
+ enqueue .emit .increased ({ by: event .by });
268
+
269
+ return {
270
+ ... context ,
271
+ count: context .count + event .by
272
+ };
273
+ }
274
+ }
275
+ });
276
+
277
+ // Listen for emitted events
278
+ store .on (' increased' , (event ) => {
279
+ console .log (` Count increased by ${event .by } ` );
280
+ });
281
+ ```
0 commit comments