File size: 2,392 Bytes
bc20498 |
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
import { registry } from './interpreters.js';
const beforeRun = 'BeforeRun';
const afterRun = 'AfterRun';
export const code = [
`code${beforeRun}`,
`code${beforeRun}Async`,
`code${afterRun}`,
`code${afterRun}Async`,
];
export const js = [
'onWorker',
'onReady',
`on${beforeRun}`,
`on${beforeRun}Async`,
`on${afterRun}`,
`on${afterRun}Async`,
];
/* c8 ignore start */
// create a copy of the resolved wrapper with the original
// run and runAsync so that, if used within onBeforeRun/Async
// or onAfterRun/Async polluted entries won't matter and just
// the native utilities will be available without seppuku.
// The same applies if called within `onReady` worker hook.
export function patch(resolved, interpreter) {
const { run, runAsync } = registry.get(this.type);
return {
...resolved,
run: run.bind(this, interpreter),
runAsync: runAsync.bind(this, interpreter)
};
}
/**
* Created the wrapper to pass along hooked callbacks.
* @param {object} module the details module
* @param {object} ref the node or reference to pass as second argument
* @param {boolean} isAsync if run should be async
* @param {function?} before callback to run before
* @param {function?} after callback to run after
* @returns {object}
*/
export const polluteJS = (module, resolved, ref, isAsync, before, after) => {
if (before || after) {
const patched = patch.bind(module, resolved);
const name = isAsync ? 'runAsync' : 'run';
const method = module[name];
module[name] = isAsync ?
async function (interpreter, code, ...args) {
if (before) await before.call(this, patched(interpreter), ref);
const result = await method.call(
this,
interpreter,
code,
...args
);
if (after) await after.call(this, patched(interpreter), ref);
return result;
} :
function (interpreter, code, ...args) {
if (before) before.call(this, patched(interpreter), ref);
const result = method.call(this, interpreter, code, ...args);
if (after) after.call(this, patched(interpreter), ref);
return result;
}
;
}
};
/* c8 ignore stop */
|