-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@xstate/store] Event emitter (#5064)
* Add emitting * Zod? * Types or schema * Export setup + changeset * Update packages/xstate-store/src/setup.ts Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com> * Fix types * Overload hell * WIP * Delete setup, update changeset * Add sub/unsub tests * Update packages/xstate-store/src/store.ts Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com> * Update packages/xstate-store/test/fromStore.test.ts Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com> * Update packages/xstate-store/src/fromStore.ts Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com> * Update packages/xstate-store/src/fromStore.ts Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com> * Update packages/xstate-store/src/fromStore.ts Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com> * Address PR comments * Fix types with fromStore * Remove schemas (separate PR) * fix small things * remove redundant `NoInfer` * tweak types * fix knip * support `TTypes['events']` * Update .changeset/silver-maps-grab.md Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com> * Revert "support `TTypes['events']`" This reverts commit 3ffa8dc. * Simplify overload * Update fromStore * Changeset * Fix emitted event ordering issue * Emitted after observers * move type tests to a separate file * add missing import, oops * use a spy * fixed test title --------- Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
- Loading branch information
1 parent
f89de0f
commit 84aca37
Showing
10 changed files
with
709 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
'@xstate/store': minor | ||
--- | ||
|
||
There is a new single-argument config API for `createStore(config)`: | ||
|
||
```ts | ||
const store = createStore({ | ||
// Types (optional) | ||
types: { | ||
emitted: {} as { type: 'incremented' } | ||
}, | ||
|
||
// Context | ||
context: { count: 0 }, | ||
|
||
// Transitions | ||
on: { | ||
inc: (context, event: { by: number }, enq) => { | ||
enq.emit({ type: 'incremented' }); | ||
|
||
return { count: context.count + event.by }; | ||
}, | ||
dec: (context, event: { by: number }) => ({ | ||
count: context.count - event.by | ||
}) | ||
} | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
'@xstate/store': minor | ||
--- | ||
|
||
You can now emit events from a store: | ||
|
||
```ts | ||
import { createStore } from '@xstate/store'; | ||
|
||
const store = createStore({ | ||
context: { | ||
count: 0 | ||
}, | ||
on: { | ||
increment: (context, event, { emit }) => { | ||
emit({ type: 'incremented' }); | ||
return { count: context.count + 1 }; | ||
} | ||
} | ||
}); | ||
|
||
store.on('incremented', () => { | ||
console.log('incremented!'); | ||
}); | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.