Injecting dependencies

Motivation

For testability reasons, we recommend that all functions used in a Kingly machine be pure. That means in particular that closures are not recommended. If you need to use a variable or function which is external to the machine, you need to make it available in the scope of a Kingly machine definition (guards, action factories, etc.).

Recipe

Kingly exposes a settings parameter for the createStateMachine machine factory. This parameter is multi-purpose and can be used to:

Sample code

import Chess from  "chess.js";

import gameFsmDef from "./fsm";
import "./index.css";

const eventEmitter = getEventEmitterAdapter(emitonoff);
const chessEngine = new Chess();
const gameFsm = createStateMachine(gameFsmDef, {
  debug: { console, checkContracts: null },
  // Injecting necessary dependencies
  eventEmitter,
  chessEngine
});