#How to provide() implementations to spawned child actors?

1 messages · Page 1 of 1 (latest)

robust jungle
#

I'm building an actor system comprised of a manager actor which spawns a varied number of worker actors. Interaction with the system is done through a service which only sends events to the manager. I'd like to DI some actors and actions implementations to the worker actors. Is there an idiomatic way to do this? Before the manager was involved, I was doing this:

// some-service.ts (BEFORE)
const workerActor = createActor(
  workerMachine.provide({
    actors: workerActors,
    actions: workerActions,
  }),
  {
    input: { /* ... */ },
  },
).start();

But now that the manager is spawning them, I'm not sure. Currently here:

// some-service.ts (CURRENT)
const managerActor = createActor(managerMachine).start();
// manager-machine.ts
const managerMachine = setup({
  /* ... */
  actors: {
    workerMachine,
  },
  actions: {
    spawnWorkerActor: assign({
      workerActors: ({ event, spawn }) => {
        /* ... */
        const workerActor = spawn('workerMachine', {
          systemId: 'someSystemId',
          id: 'id-from-event',
          input: {
            /* stuff from event */
          },
        });
        /* ... */
      },
    }),
  },
}).createMachine({
  /* ... */
});

I'm probably too deep and just over-thinking it. Help? 🙂

#

Note: the action/actor implementations are not in scope in either of the machine definition modules (they are in some-service.ts) and I'd like to keep it that way if possible.

#

I'm assuming I should still provide them in the service somehow (to the manager this time), but then I need a way to pass them through to the spawned worker actors. Maybe provide() is no longer the right API for this - perhaps input or something?

lavish agate
#

Does this work for your use case?

createActor(
  managerMachine.provide({
    actors: {
      workerMachine: workerMachine.provide({
        actors: {
          /* ... */
        },
      }),
    },
  })
)