-
-
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?
Changes from all commits
a08f2ee
873ef21
a6f4f3e
352c3ce
697e1b0
8a7ea9e
29279f5
902db97
e59e6d3
a6b60bc
f6844f7
44bf275
831aeec
ba08e79
47991b5
bb770ab
36572e7
9a69517
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ import { symbolObservable } from './symbolObservable.ts'; | |||||||||||||||||
import { AnyActorSystem, Clock, createSystem } from './system.ts'; | ||||||||||||||||||
|
||||||||||||||||||
import type { | ||||||||||||||||||
ActorRefFrom, | ||||||||||||||||||
ActorScope, | ||||||||||||||||||
AnyActorLogic, | ||||||||||||||||||
AnyActorRef, | ||||||||||||||||||
|
@@ -177,6 +178,21 @@ export class Actor<TLogic extends AnyActorLogic> | |||||||||||||||||
} | ||||||||||||||||||
(child as any)._stop(); | ||||||||||||||||||
}, | ||||||||||||||||||
spawnChild: <T extends AnyActorLogic>( | ||||||||||||||||||
logic: T, | ||||||||||||||||||
actorOptions?: ActorOptions<T> | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||
) => { | ||||||||||||||||||
const actor = createActor(logic, { | ||||||||||||||||||
parent: this, | ||||||||||||||||||
...actorOptions | ||||||||||||||||||
}); | ||||||||||||||||||
|
||||||||||||||||||
if (this._processingStatus === ProcessingStatus.Running) { | ||||||||||||||||||
actor.start(); | ||||||||||||||||||
} | ||||||||||||||||||
Comment on lines
+190
to
+192
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about the actors spawned when |
||||||||||||||||||
|
||||||||||||||||||
return actor as ActorRefFrom<T>; | ||||||||||||||||||
}, | ||||||||||||||||||
emit: (emittedEvent) => { | ||||||||||||||||||
const listeners = this.eventListeners.get(emittedEvent.type); | ||||||||||||||||||
const wildcardListener = this.eventListeners.get('*'); | ||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2149,6 +2149,10 @@ export interface ActorScope< | |
emit: (event: TEmitted) => void; | ||
system: TSystem; | ||
stopChild: (child: AnyActorRef) => void; | ||
spawnChild: <T extends AnyActorLogic>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
logic: T, | ||
actorOptions?: ActorOptions<T> | ||
) => ActorRefFrom<T>; | ||
} | ||
|
||
export type AnyActorScope = ActorScope< | ||
|
@@ -2200,17 +2204,17 @@ export interface ActorLogic< | |
/** The initial setup/configuration used to create the actor logic. */ | ||
config?: unknown; | ||
/** | ||
* Transition function that processes the current state and an incoming | ||
* message to produce a new state. | ||
* Transition function that processes the current state and an incoming event | ||
* to produce a new state. | ||
* | ||
* @param snapshot - The current state. | ||
* @param message - The incoming message. | ||
* @param event - The incoming event. | ||
* @param actorScope - The actor scope. | ||
* @returns The new state. | ||
*/ | ||
transition: ( | ||
snapshot: TSnapshot, | ||
message: TEvent, | ||
event: TEvent, | ||
actorScope: ActorScope<TSnapshot, TEvent, TSystem, TEmitted> | ||
) => TSnapshot; | ||
/** | ||
|
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?