-
-
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.
* Only complete assigners are allowed * `createStore` only takes a single { context, on } argument now * createStoreWithProducer * Enhance store functionality by introducing effects in event handling - Added `StoreEffect` type to support both emitted events and side effects. - Updated `createStoreTransition` to return effects instead of emitted events. - Modified `receive` function to handle effects, executing functions or emitting events accordingly. - Added a test case to verify that effects can be enqueued and executed after state updates. This change improves the flexibility of the store's event handling mechanism. * use overload trick 🫠 * Fix fromStore * Update changesets * Add support for type parameters * Revert "Add support for type parameters" This reverts commit cfe28e6. * Refactor store type parameters to improve type safety and flexibility - Updated `createStore` and `createStoreWithProducer` to use more explicit type parameters - Replaced `types: { emitted }` with separate type parameters for context, event payloads, and emitted events - Removed `Cast` import and simplified type definitions - Updated test cases to use new type parameter approach - Added `EventMap` type to support event type mapping * Add trigger + test + changeset * Add emits * Fixed TS issue * use correct types * Remove changeset * use util * remove redundant test * tweak things * remove commented out code * Quick typestates test * Update tests * Update fromStore * Fix tests * Update packages/xstate-store/test/fromStore.test.ts Co-authored-by: Mateusz Burzyński <[email protected]> * Improve jsdoc comments and test --------- Co-authored-by: Mateusz Burzyński <[email protected]>
- Loading branch information
1 parent
53b2c8e
commit 38aa9f5
Showing
18 changed files
with
763 additions
and
589 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,27 @@ | ||
--- | ||
'@xstate/store': major | ||
--- | ||
|
||
The `createStore` function now only accepts a single configuration object argument. This is a breaking change that simplifies the API and aligns with the configuration pattern used throughout XState. | ||
|
||
```ts | ||
// Before | ||
// createStore( | ||
// { | ||
// count: 0 | ||
// }, | ||
// { | ||
// increment: (context) => ({ count: context.count + 1 }) | ||
// } | ||
// ); | ||
|
||
// After | ||
createStore({ | ||
context: { | ||
count: 0 | ||
}, | ||
on: { | ||
increment: (context) => ({ count: context.count + 1 }) | ||
} | ||
}); | ||
``` |
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,24 @@ | ||
--- | ||
'@xstate/store': major | ||
--- | ||
|
||
You can now enqueue effects in state transitions. | ||
|
||
```ts | ||
const store = createStore({ | ||
context: { | ||
count: 0 | ||
}, | ||
on: { | ||
incrementDelayed: (context, event, enq) => { | ||
enq.effect(async () => { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
store.send({ type: 'increment' }); | ||
}); | ||
|
||
return context; | ||
}, | ||
increment: (context) => ({ count: context.count + 1 }) | ||
} | ||
}); | ||
``` |
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,17 @@ | ||
--- | ||
'@xstate/store': major | ||
--- | ||
|
||
The `fromStore(config)` function now only supports a single config object argument. | ||
|
||
```ts | ||
const storeLogic = fromStore({ | ||
context: (input: { initialCount: number }) => ({ count: input.initialCount }), | ||
on: { | ||
inc: (ctx, ev: { by: number }) => ({ | ||
...ctx, | ||
count: ctx.count + ev.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,24 @@ | ||
--- | ||
'@xstate/store': minor | ||
--- | ||
|
||
Added `store.trigger` API for sending events with a fluent interface: | ||
|
||
```ts | ||
const store = createStore({ | ||
context: { count: 0 }, | ||
on: { | ||
increment: (ctx, event: { by: number }) => ({ | ||
count: ctx.count + event.by | ||
}) | ||
} | ||
}); | ||
|
||
// Instead of manually constructing event objects: | ||
store.send({ type: 'increment', by: 5 }); | ||
|
||
// You can now use the fluent trigger API: | ||
store.trigger.increment({ by: 5 }); | ||
``` | ||
|
||
The `trigger` API provides full type safety for event names and payloads, making it easier and safer to send events to the store. |
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,32 @@ | ||
--- | ||
'@xstate/store': major | ||
--- | ||
|
||
The `createStoreWithProducer(…)` function now only accepts two arguments: a `producer` and a config (`{ context, on }`) object. | ||
|
||
```ts | ||
// Before | ||
// createStoreWithProducer( | ||
// producer, | ||
// { | ||
// count: 0 | ||
// }, | ||
// { | ||
// increment: (context) => { | ||
// context.count++; | ||
// } | ||
// } | ||
// ); | ||
|
||
// After | ||
createStoreWithProducer(producer, { | ||
context: { | ||
count: 0 | ||
}, | ||
on: { | ||
increment: (context) => { | ||
context.count++; | ||
} | ||
} | ||
}); | ||
``` |
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,19 @@ | ||
--- | ||
'@xstate/store': major | ||
--- | ||
|
||
Only complete assigner functions that replace the `context` fully are supported. This is a breaking change that simplifies the API and provides more type safety. | ||
|
||
```diff | ||
const store = createStore({ | ||
context: { | ||
items: [], | ||
count: 0 | ||
}, | ||
on: { | ||
- increment: { count: (context) => context.count + 1 } | ||
- increment: (context) => ({ count: context.count + 1 }) | ||
+ increment: (context) => ({ ...context, count: context.count + 1 }) | ||
} | ||
}) | ||
``` |
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,24 @@ | ||
--- | ||
'@xstate/store': major | ||
--- | ||
|
||
Emitted event types are now specified in functions on the `emits` property of the store definition: | ||
|
||
```ts | ||
const store = createStore({ | ||
// … | ||
emits: { | ||
increased: (payload: { upBy: number }) => { | ||
// You can execute a side-effect here | ||
// or leave it empty | ||
} | ||
}, | ||
on: { | ||
inc: (ctx, ev: { by: number }, enq) => { | ||
enq.emit.increased({ upBy: ev.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
Oops, something went wrong.