#How to access `system` or a spawned actor in general?

1 messages · Page 1 of 1 (latest)

deep oar
#

Hi. How can I access the values in this screenshot?

#

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.

sleek briar
#

Where do you want to access them from? Inside the machine?

deep oar
#

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);
sleek briar
#

When you spawn, you can access the spawned actors in context

#

Did you need to access it a different way?

deep oar
#

Am I supposed to start those actors somewhere or something? Maybe that's the problem?

sleek briar
#

The actor should already be started

#

How would you ideally like to access it?

deep oar
#

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.

deep oar
# sleek briar How would you ideally like to access it?

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.

deep oar
#
const mixerM = useMachine(mixerMachine);
console.log(mixerM.actors);
// track-1, track-2, ...
deep oar
# sleek briar How would you ideally like to access it?

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?

deep oar
#

I mean, was I doing it correctly before you asked me how I'd "ideally" like to access it? I guess I was.

deep oar
#

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.

sleek briar
deep oar
sleek briar
#

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()

deep oar
# sleek briar There's a simpler way to do that: the parent provides a reference to itself when...

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

deep oar
#

*When you get a chance. I'm sure you're busy. Thank you!

deep oar
#

I think I figured it out. Not 100% positive but it seems to be working. So nevermind, for now. Thanks.

deep oar
sleek briar
#

You can pass input to options: option={{ input: ... }}

#

Oh but I guess that doesn't help

deep oar