UNPKG

@qiwi/cyclone

Version:

"State machine" for basic purposes

20 lines (19 loc) 955 B
import { DEFAULT_OPTS as DEFAULT_MACHINE_OPTS, Machine } from './machine.js'; import { Registry } from './registry.js'; export const DEFAULT_TEMPLATE = {}; export const DEFAULT_TEMPLATE_REGISTRY = new Registry(); export const DEFAULT_MACHINE_REGISTRY = new Registry(); export const factory = (opts) => { // NOTE flowgen throws error on typed args destruction const { machine, template, templateRegistry = DEFAULT_TEMPLATE_REGISTRY, machineRegistry = DEFAULT_MACHINE_REGISTRY } = opts; const _template = getTemplate(template, templateRegistry); const machineOpts = Object.assign(Object.assign(Object.assign({}, DEFAULT_MACHINE_OPTS), _template), machine); const instance = new Machine(machineOpts); machineRegistry.add(instance.id, instance); return instance; }; export const getTemplate = (template, templateRegistry) => { return typeof template === 'string' ? templateRegistry.get(template) : template; };