#Can you provide a running actor to a new actor?

1 messages · Page 1 of 1 (latest)

young loom
#

đź‘‹ Good morning

I'm wondering if there's a way that I can add an external running actor to a new system via provide?

I have 3 actors
A, B, C

A is already running globally
B is created and provided C

I would like to add A to B so C can target it within the system via system.get('a')
Is this possible?

scarlet trench
#

When you provide actors, you aren't instantiating them in anyway, just making the actor logic available for the machine to use to instantiate them. If A is already running and you need B & C to see in A's system, why not start B & C as part of A's system? Otherwise, you can just grab an ActorRef for A and supply it as input/context for B & C to reference and then you don't need them to be in the same system at all

young loom
#

The state of A is used to show a top navigation loader.
Which is visible all the time.

B and C don't exist until some indeterminate time in the future or never.
A doesn't need to know anything about B or C.
It just wants to know when certain things happen, which B and C can notify on.

#

My current implementation creates A at the root of my app.
B and C have actions provided which call A.send explicitly.

I want to make them agnostic and have this actions target something in their system rather than explicitly providing it in the actions list.

i.e send to system component if its present.

#

notifications.ts

const notifications = createMachine()
const busy = useSelector(notifications, hasTag('busy'))

child.ts

const child = createMachine().provide({
  actions: sendTo(({ system }) => system.get('notifier'), ...) 
})

system.ts

import child from './child'
import notifications from './notifications'
const root = createMachine().provide({
  actors: {
    child,
    notifications
  }
})

So far, it seems like I can only provide actors who's lifecycle is controlled by root.

Currently, this is what I'm doing.
If I was able to leave the default implementation as sendTo then there's no need to do this in every (N) child actor who can do this.

const deps = getDepsFromSomewhere()
const child = createMachine().provide(deps)