All files / src/impl StateMachineFactory.impl.ts

100% Statements 13/13
100% Branches 0/0
100% Functions 4/4
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42            12x 12x 12x             12x 267x             25x 25x 25x 25x       267x       267x 267x     267x      
import { Contracts } from "@jonloucks/contracts-ts/api/Contracts";
import { StateMachineFactory } from "@jonloucks/concurrency-ts/api/StateMachineFactory";
import { StateMachine, Config as StateMachineConfig } from "@jonloucks/concurrency-ts/api/StateMachine";
import { RequiredType } from "@jonloucks/concurrency-ts/api/Types";
import { Config as ConcurrencyConfig } from "@jonloucks/concurrency-ts/api/Concurrency";
 
import { create as createStateMachineImpl } from "./StateMachine.impl";
import { Internal } from "./Internal.impl";
import { presentCheck } from "../auxiliary/Checks";
 
/**
 * Create a new StateMachineFactory
 *
 * @return the new StateMachineFactory
 */
export function create(config?: ConcurrencyConfig): StateMachineFactory {
  return StateMachineFactoryImpl.internalCreate(config);
}
 
// ---- Implementation details below ----
 
class StateMachineFactoryImpl implements StateMachineFactory {
  createStateMachine<T>(config: StateMachineConfig<T>): RequiredType<StateMachine<T>> {
    const validConfig: StateMachineConfig<T> = presentCheck(config, "Config must be present.");
    const contracts: Contracts = Internal.resolveContracts(validConfig, this.#concurrencyConfig);
    const finalConfig: StateMachineConfig<T> = { ...validConfig, contracts: contracts };
    return createStateMachineImpl(finalConfig);
  }
 
  static internalCreate(config?: ConcurrencyConfig): StateMachineFactory {
    return new StateMachineFactoryImpl(config);
  }
 
  private constructor(config?: ConcurrencyConfig) {
    const contracts: Contracts = Internal.resolveContracts(config);
    this.#concurrencyConfig = { ...config, contracts: contracts };
  }
 
  readonly #concurrencyConfig?: ConcurrencyConfig;
};