-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add ability to spawn actor from any logic #4724
base: main
Are you sure you want to change the base?
Conversation
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
if (this._processingStatus === ProcessingStatus.Running) { | ||
actor.start(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about the actors spawned when this._processingStatus === ProcessingStatus.NotStarted
?
}, | ||
spawnChild: <T extends AnyActorLogic>( | ||
logic: T, | ||
actorOptions?: ActorOptions<T> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should reuse the type tricks from here:
xstate/packages/core/src/createActor.ts
Lines 763 to 770 in 8a7ea9e
...[options]: ConditionalRequired< | |
[ | |
options?: ActorOptions<TLogic> & { | |
[K in RequiredOptions<TLogic>]: unknown; | |
} | |
], | |
IsNotNever<RequiredOptions<TLogic>> | |
> |
@@ -2106,6 +2106,10 @@ export interface ActorScope< | |||
defer: (fn: () => void) => void; | |||
system: TSystem; | |||
stopChild: (child: AnyActorRef) => void; | |||
spawnChild: <T extends AnyActorLogic>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -73,6 +75,7 @@ export type InvokeCallback< | |||
* the listener is then called whenever events are received by the callback actor | |||
*/ | |||
receive: Receiver<TEvent>; | |||
spawn: ActorScope<any, any>['spawnChild']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't all of those actor logics receive stopChild
as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a substantial problem with this PR that is somewhat mentioned in this comment here:
xstate/packages/core/src/createActor.ts
Lines 588 to 592 in 91549ce
// TODO: atm children don't belong entirely to the actor so | |
// in a way - it's not even super aware of them | |
// so we can't stop them from here but we really should! | |
// right now, they are being stopped within the machine's transition | |
// but that could throw and leave us with "orphaned" active actors |
When the parent actor gets stopped its children should get stopped too. There is no mechanism implemented here that would achieve this for this new functionality.
|
This PR adds the ability for any actor logic to spawn an actor, which is a core tenet of the actor model.