Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required input for spawn when defined inside referenced actor #5139

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .changeset/big-pumas-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'xstate': patch
---

Make `spawn` input required when defined inside referenced actor:

```ts
const childMachine = createMachine({
types: { input: {} as { value: number } }
});

const machine = createMachine({
types: {} as { context: { ref: ActorRefFrom<typeof childMachine> } },
context: ({ spawn }) => ({
ref: spawn(
childMachine,
// Input is now required!
{ input: { value: 42 } }
)
})
});
```
99 changes: 57 additions & 42 deletions packages/core/src/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
IsNotNever,
ProvidedActor,
RequiredActorOptions,
TODO
TODO,
type RequiredLogicInput
} from './types.ts';
import { resolveReferencedActor } from './utils.ts';

Expand All @@ -36,42 +37,56 @@ type SpawnOptions<
>
: never;

export type Spawner<TActor extends ProvidedActor> = IsLiteralString<
TActor['src']
> extends true
? {
<TSrc extends TActor['src']>(
logic: TSrc,
...[options]: SpawnOptions<TActor, TSrc>
): ActorRefFromLogic<GetConcreteByKey<TActor, 'src', TSrc>['logic']>;
<TLogic extends AnyActorLogic>(
src: TLogic,
options?: {
id?: never;
systemId?: string;
input?: InputFrom<TLogic>;
syncSnapshot?: boolean;
}
): ActorRefFromLogic<TLogic>;
}
: <TLogic extends AnyActorLogic | string>(
src: TLogic,
options?: {
id?: string;
systemId?: string;
input?: TLogic extends string ? unknown : InputFrom<TLogic>;
syncSnapshot?: boolean;
export type Spawner<TActor extends ProvidedActor> =
IsLiteralString<TActor['src']> extends true
? {
<TSrc extends TActor['src']>(
logic: TSrc,
...[options]: SpawnOptions<TActor, TSrc>
): ActorRefFromLogic<GetConcreteByKey<TActor, 'src', TSrc>['logic']>;
<TLogic extends AnyActorLogic>(
src: TLogic,
...[options]: ConditionalRequired<
[
options?: {
id?: never;
systemId?: string;
input?: InputFrom<TLogic>;
syncSnapshot?: boolean;
} & { [K in RequiredLogicInput<TLogic>]: unknown }
],
IsNotNever<RequiredLogicInput<TLogic>>
>
): ActorRefFromLogic<TLogic>;
}
) => TLogic extends AnyActorLogic ? ActorRefFromLogic<TLogic> : AnyActorRef;
: <TLogic extends AnyActorLogic | string>(
src: TLogic,
...[options]: ConditionalRequired<
[
options?: {
id?: string;
systemId?: string;
input?: TLogic extends string ? unknown : InputFrom<TLogic>;
syncSnapshot?: boolean;
} & (TLogic extends AnyActorLogic
? { [K in RequiredLogicInput<TLogic>]: unknown }
: {})
],
IsNotNever<
TLogic extends AnyActorLogic ? RequiredLogicInput<TLogic> : never
>
>
) => TLogic extends AnyActorLogic
? ActorRefFromLogic<TLogic>
: AnyActorRef;

export function createSpawner(
actorScope: AnyActorScope,
{ machine, context }: AnyMachineSnapshot,
event: AnyEventObject,
spawnedChildren: Record<string, AnyActorRef>
): Spawner<any> {
const spawn: Spawner<any> = (src, options = {}) => {
const { systemId, input } = options;
const spawn: Spawner<any> = ((src, options) => {
if (typeof src === 'string') {
const logic = resolveReferencedActor(machine, src);

Expand All @@ -82,38 +97,38 @@ export function createSpawner(
}

const actorRef = createActor(logic, {
id: options.id,
id: options?.id,
parent: actorScope.self,
syncSnapshot: options.syncSnapshot,
syncSnapshot: options?.syncSnapshot,
input:
typeof input === 'function'
? input({
typeof options?.input === 'function'
? options.input({
context,
event,
self: actorScope.self
})
: input,
: options?.input,
src,
systemId
systemId: options?.systemId
}) as any;

spawnedChildren[actorRef.id] = actorRef;

return actorRef;
} else {
const actorRef = createActor(src, {
id: options.id,
id: options?.id,
parent: actorScope.self,
syncSnapshot: options.syncSnapshot,
input: options.input,
syncSnapshot: options?.syncSnapshot,
input: options?.input,
src,
systemId
systemId: options?.systemId
});

return actorRef;
}
};
return (src, options) => {
}) as Spawner<any>;
return ((src, options) => {
const actorRef = spawn(src, options) as TODO; // TODO: fix types
spawnedChildren[actorRef.id] = actorRef;
actorScope.defer(() => {
Expand All @@ -123,5 +138,5 @@ export function createSpawner(
actorRef.start();
});
return actorRef;
};
}) as Spawner<any>;
Copy link
Contributor Author

@SandroMaglione SandroMaglione Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type errors without this bypass. I think this is expected given that typescript has not enough information to correctly type-check all possible configurations.

All tests are passing, so I think it shouldn’t be an issue (the update is mostly on the types).

}
15 changes: 9 additions & 6 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import type { MachineSnapshot } from './State.ts';
import type { StateMachine } from './StateMachine.ts';
import type { StateNode } from './StateNode.ts';
import { AssignArgs } from './actions/assign.ts';
import { ExecutableRaiseAction } from './actions/raise.ts';
import { ExecutableSendToAction } from './actions/send.ts';
import { PromiseActorLogic } from './actors/promise.ts';
import { Guard, GuardPredicate, UnknownGuard } from './guards.ts';
import type { Actor, ProcessingStatus } from './createActor.ts';
import { Guard, GuardPredicate, UnknownGuard } from './guards.ts';
import { InspectionEvent } from './inspection.ts';
import { Spawner } from './spawn.ts';
import { AnyActorSystem, Clock } from './system.js';
import { InspectionEvent } from './inspection.ts';
import { ExecutableRaiseAction } from './actions/raise.ts';
import { ExecutableSendToAction } from './actions/send.ts';

export type Identity<T> = { [K in keyof T]: T[K] };

Expand Down Expand Up @@ -805,8 +805,8 @@ export type InvokeConfig<
>
>;
/**
* The transition to take upon the invoked child machine sending an
* error event.
* The transition to take upon the invoked child machine sending an error
* event.
*/
onError?:
| string
Expand Down Expand Up @@ -2452,6 +2452,9 @@ export type RequiredActorOptions<TActor extends ProvidedActor> =
| (undefined extends TActor['id'] ? never : 'id')
| (undefined extends InputFrom<TActor['logic']> ? never : 'input');

export type RequiredLogicInput<TLogic extends AnyActorLogic> =
undefined extends InputFrom<TLogic> ? never : 'input';

type ExtractLiteralString<T extends string | undefined> = T extends string
? string extends T
? never
Expand Down
18 changes: 18 additions & 0 deletions packages/core/test/spawn.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ActorRefFrom, createActor, createMachine } from '../src';

describe('spawn inside machine', () => {
it('input is required when defined in actor', () => {
const childMachine = createMachine({
types: { input: {} as { value: number } }
});
const machine = createMachine({
types: {} as { context: { ref: ActorRefFrom<typeof childMachine> } },
context: ({ spawn }) => ({
ref: spawn(childMachine, { input: { value: 42 }, systemId: 'test' })
})
});

const actor = createActor(machine).start();
expect(actor.system.get('test')).toBeDefined();
});
});
49 changes: 49 additions & 0 deletions packages/core/test/spawn.types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ActorRefFrom, assign, createMachine } from '../src';

describe('spawn inside machine', () => {
it('input is required when defined in actor', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't really check that it's required here, u just pass an input as an argument but the same test would pass without the patch introduced by this PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be inverted with a @ts-expect-error instead? Or is there another better way to test types?

const childMachine = createMachine({
types: { input: {} as { value: number } }
});
createMachine({
types: {} as { context: { ref: ActorRefFrom<typeof childMachine> } },
context: ({ spawn }) => ({
ref: spawn(childMachine, { input: { value: 42 } })
}),
initial: 'idle',
states: {
Idle: {
on: {
event: {
actions: assign(({ spawn }) => ({
ref: spawn(childMachine, { input: { value: 42 } })
}))
}
}
}
}
});
});

it('input is not required when not defined in actor', () => {
const childMachine = createMachine({});
createMachine({
types: {} as { context: { ref: ActorRefFrom<typeof childMachine> } },
context: ({ spawn }) => ({
ref: spawn(childMachine)
}),
initial: 'idle',
states: {
Idle: {
on: {
some: {
actions: assign(({ spawn }) => ({
ref: spawn(childMachine)
}))
}
}
}
}
});
});
});
Loading