Skip to content

Latest commit

 

History

History
60 lines (51 loc) · 1.29 KB

no-auto-forward.md

File metadata and controls

60 lines (51 loc) · 1.29 KB

Forbid auto-forwarding events

Prefer sending events explicitly to child actors/services.

Rule Details

Avoid blindly forwarding all events to invoked services or spawned actors - it may lead to unexpected behavior or infinite loops. The official documentation suggests sending events explicitly with the forwardTo or send action creators.

Examples of incorrect code for this rule:

// ❌ auto-forwarding events to an invoked service
createMachine({
  states: {
    playing: {
      invoke: {
        src: 'game',
        autoForward: true,
      },
    },
  },
})

// ❌ auto-forwarding events to a spawned actor
createMachine({
  states: {
    initializing: {
      entry: assign({
        gameRef: () => spawn(game, { autoForward: true }),
      }),
    },
  },
})

Examples of correct code for this rule:

// ✅ no auto-forward
createMachine{{
  states: {
    playing: {
      invoke: {
        src: 'game',
      },
    },
  },
}}

// ✅ autoForward set to false
createMachine({
  states: {
    initializing: {
      entry: assign({
        gameRef: () => spawn(game, { autoForward: false }),
      }),
    },
  },
})