From e659fac5d82e283e1298122814763c59af9a2375 Mon Sep 17 00:00:00 2001 From: David Khourshid Date: Mon, 10 Jul 2023 04:53:18 -0400 Subject: [PATCH] Add function action type to pure (#4130) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add function action type to pure * Changeset * Revert unrelated changes --------- Co-authored-by: Mateusz BurzyƄski --- .changeset/big-coats-fetch.md | 12 ++++++++++++ packages/core/src/actions.ts | 1 + packages/core/src/types.ts | 4 +++- packages/core/test/actions.test.ts | 23 +++++++++++++++++++---- 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 .changeset/big-coats-fetch.md diff --git a/.changeset/big-coats-fetch.md b/.changeset/big-coats-fetch.md new file mode 100644 index 0000000000..b177fca79e --- /dev/null +++ b/.changeset/big-coats-fetch.md @@ -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) => { ... } +]) +``` diff --git a/packages/core/src/actions.ts b/packages/core/src/actions.ts index 97d298c219..b158047aa9 100644 --- a/packages/core/src/actions.ts +++ b/packages/core/src/actions.ts @@ -609,6 +609,7 @@ export function pure< | BaseActionObject | BaseActionObject['type'] | ActionObject + | ActionFunction > | undefined ): PureAction { diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 14cf844585..c592d0d1d8 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -1453,7 +1453,9 @@ export interface PureAction< event: TEvent ) => | SingleOrArray< - ActionObject | ActionObject['type'] + | ActionObject + | ActionObject['type'] + | ActionFunction > | undefined; } diff --git a/packages/core/test/actions.test.ts b/packages/core/test/actions.test.ts index 7fc9384c61..1b8a62bddf 100644 --- a/packages/core/test/actions.test.ts +++ b/packages/core/test/actions.test.ts @@ -1874,7 +1874,7 @@ describe('purely defined actions', () => { idle: { on: { SINGLE: { - actions: pure((ctx, e) => { + actions: pure((ctx, e) => { if (ctx.items.length > 0) { return { type: 'SINGLE_EVENT', @@ -1885,7 +1885,7 @@ describe('purely defined actions', () => { }) }, NONE: { - actions: pure((ctx, e) => { + actions: pure((ctx, e) => { if (ctx.items.length > 5) { return { type: 'SINGLE_EVENT', @@ -1896,7 +1896,7 @@ describe('purely defined actions', () => { }) }, EACH: { - actions: pure((ctx) => + actions: pure((ctx) => ctx.items.map((item: any, index: number) => ({ type: 'EVENT', item, @@ -1905,7 +1905,7 @@ describe('purely defined actions', () => { ) }, AS_STRINGS: { - actions: pure(() => ['SOME_ACTION']) + actions: pure(() => ['SOME_ACTION']) } } } @@ -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()', () => {