Skip to content

Commit ac79eb1

Browse files
committed
Update store README.md
1 parent 80db725 commit ac79eb1

File tree

1 file changed

+78
-14
lines changed

1 file changed

+78
-14
lines changed

packages/xstate-store/README.md

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,36 @@ export const donutStore = createStore({
2929
favoriteFlavor: 'chocolate'
3030
},
3131
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,
3942
donuts: 0
40-
}
43+
})
4144
}
4245
});
4346

4447
donutStore.subscribe((snapshot) => {
4548
console.log(snapshot.context);
4649
});
4750

48-
donutStore.send({ type: 'addDonut' });
51+
// Equivalent to
52+
// donutStore.send({ type: 'addDonut' });
53+
donutStore.trigger.addDonut();
4954
// => { donuts: 1, favoriteFlavor: 'chocolate' }
5055

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' }
5662
```
5763

5864
<details>
@@ -215,3 +221,61 @@ const donutStore = createStore({
215221
}
216222
});
217223
```
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

Comments
 (0)