diff --git a/patcher.js b/patcher.js index e6c05d0..6286505 100644 --- a/patcher.js +++ b/patcher.js @@ -1,13 +1,14 @@ function monkeyPatch(name, parentObj, patches) { const injId = Symbol() - const before = (patches["before"] === undefined) ? (...args)=>args : patches["before"]; - const instead = (patches["instead"] === undefined) ? (res,...args)=>res(...args) : patches["instead"]; - const after = (patches["after"] === undefined) ? (args, res)=>res : patches["after"]; + const before = patches["before"]; + const instead = patches["instead"]; + const after = patches["after"]; const handler = { apply: (target, thisArg, [ctx, args]) => { - before.apply(ctx, args); - res = instead(target.bind(ctx), args) - return after.apply(ctx, args.concat([res])); + if (before !== undefined) before.apply(ctx, args); + res = (patches["instead"] !== undefined) ? instead.apply(ctx, [target.bind(ctx), ...args]) : target.apply(ctx, [...args]) + if (after === undefined) return res + return after.apply(ctx, [res].concat(args)); } }; const prox = new Proxy(parentObj[name], handler);