-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fixed an issue with actors not being reinstantiated correctly in a microstep #3374
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'xstate': patch | ||
--- | ||
|
||
Fixed an issue with actors not being reinstantiated correctly when an actor with the same ID was first stopped and then invoked/spawned again in the same microstep. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,14 +32,14 @@ export function stop< | |
{ | ||
actor | ||
}, | ||
({ params, type }, context, _event) => { | ||
const actorRefOrString = isFunction(params.actor) | ||
({ params, type }, context, _event, { state }) => { | ||
const actorRef = isFunction(params.actor) | ||
? params.actor(context, _event.data) | ||
: params.actor; | ||
: state.children[params.actor]; | ||
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. we have to resolve a string to the ref here, before we mutate the new children in Because that |
||
|
||
return { | ||
type, | ||
params: { actor: actorRefOrString } | ||
params: { actor: actorRef } | ||
} as StopActionObject; | ||
} | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -632,7 +632,7 @@ export class Interpreter< | |
[actionTypes.cancel]: (_ctx, _e, { action }) => { | ||
this.cancel((action as CancelActionObject).params.sendId); | ||
}, | ||
[actionTypes.invoke]: (_ctx, _e, { action, state }) => { | ||
[actionTypes.invoke]: (_ctx, _e, { action }) => { | ||
const { | ||
id, | ||
autoForward, | ||
|
@@ -651,26 +651,16 @@ export class Interpreter< | |
return; | ||
} | ||
ref._parent = this; // TODO: fix | ||
// If the actor will be stopped right after it's started | ||
// (such as in transient states) don't bother starting the actor. | ||
if ( | ||
state.actions.find((otherAction) => { | ||
return ( | ||
otherAction.type === actionTypes.stop && | ||
(otherAction as StopActionObject).params.actor === id | ||
); | ||
}) | ||
Comment on lines
-657
to
-662
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 was the main source of the problem because we had actions looking like this:
Stop action for |
||
) { | ||
// If the actor didn't end up being in the state | ||
// (eg. going through transient states could stop it) don't bother starting the actor. | ||
if (!this.state.children[id]) { | ||
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. Since |
||
return; | ||
} | ||
try { | ||
if (autoForward) { | ||
this.forwardTo.add(id); | ||
} | ||
|
||
// TODO: determine how this can be immutably updated | ||
this.state.children[id] = ref; | ||
|
||
ref.start?.(); | ||
} catch (err) { | ||
this.send(error(id, err)); | ||
|
@@ -679,11 +669,9 @@ export class Interpreter< | |
}, | ||
[actionTypes.stop]: (_ctx, _e, { action }) => { | ||
const { actor } = (action as StopActionObject).params; | ||
const actorRef = | ||
typeof actor === 'string' ? this.state.children[actor] : actor; | ||
|
||
if (actorRef) { | ||
this.stopChild(actorRef.name); | ||
if (actor) { | ||
this.stopChild(actor); | ||
} | ||
}, | ||
[actionTypes.log]: (_ctx, _e, { action }) => { | ||
|
@@ -746,16 +734,8 @@ export class Interpreter< | |
return undefined; | ||
} | ||
|
||
private stopChild(childId: string): void { | ||
const child = this.state.children[childId]; | ||
if (!child) { | ||
return; | ||
} | ||
|
||
this.forwardTo.delete(childId); | ||
// TODO: determine how this can be immutably updated | ||
delete this.state.children[childId]; | ||
|
||
private stopChild(child: ActorRef<any, any>): void { | ||
this.forwardTo.delete(child.name); | ||
if (isFunction(child.stop)) { | ||
child.stop(); | ||
} | ||
|
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.
The
preInitial
wasn't previously populated with children and thus without this change we were failing the adjusted logic here:https://github.com/statelyai/xstate/pull/3374/files#diff-fc91146eff717b2bcd6adbc0955ca0549346ad7bef9aa2278e0a75a9793a66b9R656