Skip to content

Commit

Permalink
[core] Add self and system to enqueueActions(...) (#4597)
Browse files Browse the repository at this point in the history
* Add `self` to `enqueueActions(...)`

* Changeset

* improve types and make `system` available too

* Update changeset

---------

Co-authored-by: Mateusz Burzyński <[email protected]>
  • Loading branch information
davidkpiano and Andarist authored Dec 17, 2023
1 parent 92958fd commit ae0b05f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 51 deletions.
12 changes: 12 additions & 0 deletions .changeset/strong-jobs-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'xstate': patch
---

Update the argument object of `enqueueActions(...)` to include the `self` and `system` properties:

```ts
// ...
entry: enqueueActions(({ self, system }) => {
// ...
});
```
122 changes: 71 additions & 51 deletions packages/core/src/actions/enqueueActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
EventObject,
MachineContext,
ParameterizedObject,
ProvidedActor
ProvidedActor,
UnifiedArg
} from '../types.ts';
import { assign } from './assign.ts';
import { cancel } from './cancel.ts';
Expand Down Expand Up @@ -79,39 +80,22 @@ interface ActionEnqueuer<
}

function resolveEnqueueActions(
_: AnyActorScope,
actorScope: AnyActorScope,
snapshot: AnyMachineSnapshot,
args: ActionArgs<any, any, any>,
_actionParams: ParameterizedObject['params'] | undefined,
{
collect
}: {
collect: ({
context,
event,
enqueue,
check
}: {
context: MachineContext;
event: EventObject;
enqueue: ActionEnqueuer<
MachineContext,
EventObject,
EventObject,
ProvidedActor,
ParameterizedObject,
ParameterizedObject,
string
>;
check: (
guard: Guard<
MachineContext,
EventObject,
undefined,
ParameterizedObject
>
) => boolean;
}) => void;
collect: CollectActions<
MachineContext,
EventObject,
EventObject,
ProvidedActor,
ParameterizedObject,
ParameterizedObject,
string
>;
}
) {
const actions: any[] = [];
Expand Down Expand Up @@ -144,7 +128,9 @@ function resolveEnqueueActions(
event: args.event,
enqueue,
check: (guard) =>
evaluateGuard(guard, snapshot.context, args.event, snapshot)
evaluateGuard(guard, snapshot.context, args.event, snapshot),
self: actorScope.self,
system: actorScope.system
});

return [snapshot, undefined, actions];
Expand All @@ -167,6 +153,53 @@ export interface EnqueueActionsAction<
_out_TDelay?: TDelay;
}

interface CollectActionsArg<
TContext extends MachineContext,
TExpressionEvent extends EventObject,
TEvent extends EventObject,
TActor extends ProvidedActor,
TAction extends ParameterizedObject,
TGuard extends ParameterizedObject,
TDelay extends string
> extends UnifiedArg<TContext, TExpressionEvent, TEvent> {
check: (
guard: Guard<TContext, TExpressionEvent, undefined, TGuard>
) => boolean;
enqueue: ActionEnqueuer<
TContext,
TExpressionEvent,
TEvent,
TActor,
TAction,
TGuard,
TDelay
>;
}

type CollectActions<
TContext extends MachineContext,
TExpressionEvent extends EventObject,
TEvent extends EventObject,
TActor extends ProvidedActor,
TAction extends ParameterizedObject,
TGuard extends ParameterizedObject,
TDelay extends string
> = ({
context,
event,
check,
enqueue,
self
}: CollectActionsArg<
TContext,
TExpressionEvent,
TEvent,
TActor,
TAction,
TGuard,
TDelay
>) => void;

export function enqueueActions<
TContext extends MachineContext,
TExpressionEvent extends EventObject,
Expand All @@ -176,27 +209,15 @@ export function enqueueActions<
TGuard extends ParameterizedObject = ParameterizedObject,
TDelay extends string = string
>(
collect: ({
context,
event,
check,
enqueue
}: {
context: TContext;
event: TExpressionEvent;
check: (
guard: Guard<TContext, TExpressionEvent, undefined, TGuard>
) => boolean;
enqueue: ActionEnqueuer<
TContext,
TExpressionEvent,
TEvent,
TActor,
TAction,
TGuard,
TDelay
>;
}) => void
collect: CollectActions<
TContext,
TExpressionEvent,
TEvent,
TActor,
TAction,
TGuard,
TDelay
>
): EnqueueActionsAction<
TContext,
TExpressionEvent,
Expand All @@ -217,7 +238,6 @@ export function enqueueActions<

enqueueActions.type = 'xstate.enqueueActions';
enqueueActions.collect = collect;

enqueueActions.resolve = resolveEnqueueActions;

return enqueueActions;
Expand Down
10 changes: 10 additions & 0 deletions packages/core/test/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2807,6 +2807,16 @@ describe('enqueueActions', () => {
]
`);
});
it('should provide self', () => {
expect.assertions(1);
const machine = createMachine({
entry: enqueueActions(({ self }) => {
expect(self.send).toBeDefined();
})
});

createActor(machine).start();
});
});

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

0 comments on commit ae0b05f

Please sign in to comment.