Skip to content

Commit

Permalink
Merge pull request #3545 from with-heart/pure-action-type-strings
Browse files Browse the repository at this point in the history
allow action `type` strings in `pure` return array
  • Loading branch information
davidkpiano committed Dec 3, 2022
2 parents fb4786b + 4a5aa27 commit b9995f0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
24 changes: 24 additions & 0 deletions .changeset/seven-ducks-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
'xstate': patch
---

pr: #3545
author: @with-heart

Updated `pure` action types to allow action `type` strings to be returned in the array.

```ts
const machine = createMachine(
{
entry: ['doStuff']
},
{
actions: {
doStuff: pure(() => ['someAction']),
someAction: () => console.log('executed by doStuff')
}
}
);
```

Returning action `type` strings were already handled by `xstate` and the types now correctly reflect that.
6 changes: 5 additions & 1 deletion packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,11 @@ export function pure<TContext, TEvent extends EventObject>(
getActions: (
context: TContext,
event: TEvent
) => SingleOrArray<ActionObject<TContext, TEvent>> | undefined
) =>
| SingleOrArray<
ActionObject<TContext, TEvent> | ActionObject<TContext, TEvent>['type']
>
| undefined
): PureAction<TContext, TEvent> {
return {
type: ActionTypes.Pure,
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,11 @@ export interface PureAction<TContext, TEvent extends EventObject>
get: (
context: TContext,
event: TEvent
) => SingleOrArray<ActionObject<TContext, TEvent>> | undefined;
) =>
| SingleOrArray<
ActionObject<TContext, TEvent> | ActionObject<TContext, TEvent>['type']
>
| undefined;
}

export interface ChooseAction<TContext, TEvent extends EventObject>
Expand Down
15 changes: 14 additions & 1 deletion packages/core/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1861,7 +1861,8 @@ describe('purely defined actions', () => {
type Events =
| { type: 'SINGLE'; id: number }
| { type: 'NONE'; id: number }
| { type: 'EACH' };
| { type: 'EACH' }
| { type: 'AS_STRINGS' };

const dynamicMachine = Machine<Ctx, Events>({
id: 'dynamic',
Expand Down Expand Up @@ -1902,6 +1903,9 @@ describe('purely defined actions', () => {
index
}))
)
},
AS_STRINGS: {
actions: pure<any, any>(() => ['SOME_ACTION'])
}
}
}
Expand Down Expand Up @@ -1956,6 +1960,15 @@ describe('purely defined actions', () => {
}
]);
});

it('should allow for purely defined action type strings', () => {
const nextState = dynamicMachine.transition(
dynamicMachine.initialState,
'AS_STRINGS'
);

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

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

0 comments on commit b9995f0

Please sign in to comment.