UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

38 lines (25 loc) 1.07 kB
const cache = new WeakMap(); let id_counter = 0; /** * * @param {function} previous * @param {function} next * @returns {function} */ export function composeCompile(previous, next) { let t1_cache = cache.get(previous); if (t1_cache === undefined) { // no caches starting from previous t1_cache = new WeakMap(); cache.set(previous, t1_cache); } let result_function = t1_cache.get(next); if (result_function === undefined) { // no matching function found for pair, let's create it // we create a custom function just to be able to name our function, this is because of how three.js caches onBeforeCompile by just turning it into the string, so changing the function name will result in altered cache key const f = new Function('previous, next', `return function f${id_counter++}(a,b,c,d){ previous(a,b,c,d); next(a,b,c,d); };`); result_function = f(previous, next); t1_cache.set(next, result_function); } return result_function; }