import memoize from 'lodash.memoize'; import {invokeHookFlat, registerHooks} from 'scwp'; export const compilers = memoize(() => ( invokeHookFlat('behaviorCompilers') .reduce((r, results) => ({ ...r, ...results, }), {}) )); export default function compile(variant) { const {[variant.type]: compiler} = compilers(); if (!compiler) { return () => Promise.reject(new Error(`No compiler for '${variant.type}'`)); } return compiler(variant); } export function isCompilable(variant) { if ('object' !== typeof variant || null === variant) { return false; } if (!variant.type) { return false; } return !!compilers()[variant.type]; } export function makeCompilable(variant) { return isCompilable(variant) ? variant : {type: 'literal', value: variant}; } registerHooks({ autoreg$accept: (type, M) => { if ('hooks' === type && 'behaviorCompilers' in M) { compilers.cache.clear(); } }, }, module.id);