Skip to content

Commit

Permalink
Add function action type to pure (#4130)
Browse files Browse the repository at this point in the history
* Add function action type to pure

* Changeset

* Revert unrelated changes

---------

Co-authored-by: Mateusz Burzyński <[email protected]>
  • Loading branch information
davidkpiano and Andarist authored Jul 10, 2023
1 parent bdcc3b0 commit e659fac
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
12 changes: 12 additions & 0 deletions .changeset/big-coats-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'xstate': patch
---

The `pure(...)` action creator is now properly typed so that it allows function actions:

```ts
actions: pure(() => [
// now allowed!
(context, event) => { ... }
])
```
1 change: 1 addition & 0 deletions packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ export function pure<
| BaseActionObject
| BaseActionObject['type']
| ActionObject<TContext, TExpressionEvent, TEvent>
| ActionFunction<TContext, TExpressionEvent>
>
| undefined
): PureAction<TContext, TExpressionEvent, TEvent> {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,9 @@ export interface PureAction<
event: TEvent
) =>
| SingleOrArray<
ActionObject<TContext, TEvent> | ActionObject<TContext, TEvent>['type']
| ActionObject<TContext, TEvent>
| ActionObject<TContext, TEvent>['type']
| ActionFunction<TContext, TEvent>
>
| undefined;
}
Expand Down
23 changes: 19 additions & 4 deletions packages/core/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@ describe('purely defined actions', () => {
idle: {
on: {
SINGLE: {
actions: pure<any, any>((ctx, e) => {
actions: pure((ctx, e) => {
if (ctx.items.length > 0) {
return {
type: 'SINGLE_EVENT',
Expand All @@ -1885,7 +1885,7 @@ describe('purely defined actions', () => {
})
},
NONE: {
actions: pure<any, any>((ctx, e) => {
actions: pure((ctx, e) => {
if (ctx.items.length > 5) {
return {
type: 'SINGLE_EVENT',
Expand All @@ -1896,7 +1896,7 @@ describe('purely defined actions', () => {
})
},
EACH: {
actions: pure<any, any>((ctx) =>
actions: pure((ctx) =>
ctx.items.map((item: any, index: number) => ({
type: 'EVENT',
item,
Expand All @@ -1905,7 +1905,7 @@ describe('purely defined actions', () => {
)
},
AS_STRINGS: {
actions: pure<any, any>(() => ['SOME_ACTION'])
actions: pure(() => ['SOME_ACTION'])
}
}
}
Expand Down Expand Up @@ -1969,6 +1969,21 @@ describe('purely defined actions', () => {

expect(nextState.actions).toEqual([{ type: 'SOME_ACTION' }]);
});

it('should allow function actions in pure', () => {
let called = false;
const machine = createMachine({
entry: pure(() => [
() => {
called = true;
}
])
});

interpret(machine).start();

expect(called).toBeTruthy();
});
});

describe('forwardTo()', () => {
Expand Down

0 comments on commit e659fac

Please sign in to comment.