Skip to main content

Creating Multiple Machines

Sometimes, you want to reuse a config to create multiple instances of the same machine — for example, managing independent counters.


🧪 Example

const counterMachine1 = retomus.createMachine({
...machineConfig,
id: 'counter-1',
});

const counterMachine2 = retomus.createMachine({
...machineConfig,
id: 'counter-2',
});

Each machine has:

  • Its own context and status
  • Separate lifecycles
  • Unique identity via id

⚠️ ID Must Be Unique

Avoid hardcoding same id values when cloning:

// ❌ BAD
retomus.createMachine(machineConfig);
retomus.createMachine(machineConfig); // same id = conflict

// ✅ GOOD
retomus.createMachine({ ...machineConfig, id: 'one' });
retomus.createMachine({ ...machineConfig, id: 'two' });