#How to access `system` or a spawned actor in general?
1 messages · Page 1 of 1 (latest)
Or how can I do this:
system.get('actorId')
That's what I've never understood about spawning actors. I'm able to spawn them but I don't know how to access them.
Where do you want to access them from? Inside the machine?
What's the difference between spawning and not spawning.
spawned - https://stackblitz.com/edit/github-aqxtxr?file=src%2Fcomponents%2FMixer%2FmixerMachine.ts
not spawned - https://stackblitz.com/edit/github-kxjequ?file=src%2Fcomponents%2FMixer%2FmixerMachine.ts
the code difference is:
buildMixer: assign(({ context }) => {
let players: Player[] = [];
let channels: Channel[] = [];
context.audioBuffers.forEach((buffer, i) => {
channels = [...channels, new Channel().toDestination()];
players = [
...players,
new Player(buffer)
.chain(channels[i])
.sync()
.start(0, context.sourceSong?.startPosition),
];
});
return {
sourceSong: context.sourceSong,
channels,
players,
};
}),
and
buildMixer: assign(({ context, spawn }) => {
let players: Player[] = [];
let channels: Channel[] = [];
let trackMachineRefs = [];
context.audioBuffers.forEach((buffer, i) => {
channels = [...channels, new Channel().toDestination()];
players = [
...players,
new Player(buffer)
.chain(channels[i])
.sync()
.start(0, context.sourceSong?.startPosition),
];
trackMachineRefs = [
spawn(trackMachine, {
systemId: `track-${i}`,
id: `track-${i}`,
input: {
channel: channels[i],
track: context.sourceSong!.tracks[i],
},
}),
...trackMachineRefs,
];
});
return {
trackMachineRefs,
};
}),
I'm trying to access it in the Mixer component. Right now I'm doing this:
const state = MixerContext.useSelector((s) => s);
const refs = state.context.trackMachineRefs;
const tracks = refs && refs.map((ref) => ref.options.input.track);
const channels = refs && refs.map((ref) => ref.options.input.channel);
When you spawn, you can access the spawned actors in context
Did you need to access it a different way?
So doing all that? I have to drill down into context like I did above? That doesn't make sense.
Am I supposed to start those actors somewhere or something? Maybe that's the problem?
system.get('track-1')
I'm not quite sure what the advantage of spawning these actors is. Its supposed to make the tracks a system? #1210007291630460978 message I'm totally confused.
Ideally? If I spawned a list of trackMachine actors from the parent machine mixerMachine and gave those spawned actors system ids that increased (ie: track-1, track-2), I could access them from inside a React component like this:
useActor("#mixerMachine.track2");
Does that makes sense? Is there anyway to do something like that?
Or maybe this would make more sense.
import mixerMachine from "./mixerMachine";
...
useActor({mixerMachine: "track-2"});
...something like that.
const mixerM = useMachine(mixerMachine);
console.log(mixerM.actors);
// track-1, track-2, ...
When you spawn a machine within the actor it's becomes the part of the system parent actor in.
Which is handy for referencing directly with id
if you directly create a machine, it will have its own independent system.
so you can not do things like. system.get('parent')
#1205309453918806036 message
What do you mean by "ideally"? What kind of question is that? Can you just show me how to do it? Please. 😁
If I'm in a React component. How can I access system?
I mean, was I doing it correctly before you asked me how I'd "ideally" like to access it? I guess I was.
Okay. I think I get it. If I want to access a parent machine's state from a child machine it needs to be part of a system—meaning it needs to have been spawned from that parent machine.
"Ideally" means what would an intuitive API look like to you? That helps us figure out the best API to design for this, if it doesn't exist yet.
- If I want to access a parent machine's state from a child machine it needs to be part of a system—meaning it needs to have been spawned from that parent machine.*
Was I right about that?
There's a simpler way to do that: the parent provides a reference to itself when creating the actor
spawn(someMachine, {
input: {
parent: self,
// ...
}
})
Then in someMachine:
const someMachine = setup({ ... }).createMachine({
context: ({ input }) => ({
parent: input.parent
})
})
And you can read that parent "state" (snapshot) via context.parent.getSnapshot()
Okay. Thanks. I was spawning the tracks instead of invoking them, but I went back to invoking them because the "disposeTracks" action does not work with spawned tracks for some reason. So when you choose one song, then choose a second one after that, they both play at once because the first one wasn't disposed.
Any clue why this is happening?
spawned - https://stackblitz.com/edit/github-aqxtxr?file=src%2Fcomponents%2FMixer%2FmixerMachine.ts
not spawned - https://stackblitz.com/edit/github-kxjequ?file=src%2Fcomponents%2FMixer%2FmixerMachine.ts
I've tried to make it as simple as possible to see what's going on. If you look at the console logs, you will see. Could you please take one second to look at those links above?
- Open the console
- Choose a song
- Then choose another one
- Read the logs
Why aren't the tracks available to dispose in the spawned one?
*When you get a chance. I'm sure you're busy. Thank you!
I think I figured it out. Not 100% positive but it seems to be working. So nevermind, for now. Thanks.
That doesn't work because I'm using:
<SomeContext.Provider options={...} />
so context.parent is always undefined.
You can pass input to options: option={{ input: ... }}
Oh but I guess that doesn't help
I think I figured this out.