avocado-old/packages/behavior/compile.js

41 lines
966 B
JavaScript
Raw Normal View History

2020-06-23 11:19:59 -05:00
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);
}
2020-06-23 19:44:31 -05:00
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};
}
2020-06-23 11:19:59 -05:00
registerHooks({
autoreg$accept: (type, M) => {
if ('hooks' === type && 'behaviorCompilers' in M) {
compilers.cache.clear();
}
},
}, module.id);