diff --git a/package.json b/package.json index c16008d..efb47f4 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "spitroast", - "version": "1.4.2", + "version": "1.4.3", "description": "A simple JavaScript function patcher.", "main": "dist/cjs.js", "module": "dist/esm/index.js", "types": "dist/types/index.d.ts", "scripts": { - "test": "mocha", + "test": "npm run prepublish && mocha", "prepublish": "ttsc && esbuild --bundle src/index.ts --outfile=dist/cjs.js --format=cjs" }, "repository": { diff --git a/src/hook.ts b/src/hook.ts index 1242053..38cc599 100644 --- a/src/hook.ts +++ b/src/hook.ts @@ -26,18 +26,16 @@ export default function ( } // Instead patches - let insteadPatchedFunc = (...args: unknown[]) => - isConstruct - ? Reflect.construct(patch.o, args, ctxt) - : patch.o.apply(ctxt, args); - - for (const callback of patch.i.values()) { - const oldPatchFunc = insteadPatchedFunc; - - insteadPatchedFunc = (...args) => callback.call(ctxt, args, oldPatchFunc); - } - - let workingRetVal = insteadPatchedFunc(...funcArgs); + let workingRetVal = [...patch.i.values()].reduce( + (prev, current) => + (...args: unknown[]) => + current.call(ctxt, args, prev), + // This calls the original function + (...args: unknown[]) => + isConstruct + ? Reflect.construct(patch.o, args, ctxt) + : patch.o.apply(ctxt, args) + )(...funcArgs); // After patches for (const hook of patch.a.values())