|
|
|
@ -1,12 +1,16 @@
|
|
|
|
|
type BeforeFunc = (args: any[]) => void
|
|
|
|
|
type InsteadFunc = (args: any[], orig: (...args: any) => any) => any
|
|
|
|
|
type AfterFunc = (args: any[], res: any) => any
|
|
|
|
|
type BeforeFunc = (args: any[]) => void;
|
|
|
|
|
type InsteadFunc = (args: any[], orig: (...args: any) => any) => any;
|
|
|
|
|
type AfterFunc = (args: any[], res: any) => any;
|
|
|
|
|
|
|
|
|
|
function wackyPatch(parentObj: any, name: string, patches: {
|
|
|
|
|
before?: BeforeFunc,
|
|
|
|
|
instead?: InsteadFunc,
|
|
|
|
|
after?: AfterFunc
|
|
|
|
|
}) {
|
|
|
|
|
function wackyPatch(
|
|
|
|
|
parentObj: any,
|
|
|
|
|
name: string,
|
|
|
|
|
patches: {
|
|
|
|
|
before?: BeforeFunc;
|
|
|
|
|
instead?: InsteadFunc;
|
|
|
|
|
after?: AfterFunc;
|
|
|
|
|
}
|
|
|
|
|
) {
|
|
|
|
|
if (typeof parentObj === "string") {
|
|
|
|
|
//assume parentObj and name are switched around (for backcompat/convienence)
|
|
|
|
|
const tmp = parentObj;
|
|
|
|
@ -18,12 +22,16 @@ function wackyPatch(parentObj: any, name: string, patches: {
|
|
|
|
|
const instead = patches["instead"];
|
|
|
|
|
const after = patches["after"];
|
|
|
|
|
const handler = {
|
|
|
|
|
apply: (target: () => any, thisArg: any, [ctx, args]: [ctx: any, args: []]) => {
|
|
|
|
|
apply: (
|
|
|
|
|
target: () => any,
|
|
|
|
|
thisArg: any,
|
|
|
|
|
[ctx, args]: [ctx: any, args: []]
|
|
|
|
|
) => {
|
|
|
|
|
if (before !== undefined) before.apply(ctx, [args]);
|
|
|
|
|
const res =
|
|
|
|
|
patches["instead"] !== undefined
|
|
|
|
|
//@ts-ignore; TS thinks that `instead` possibly being undefined is bad (meanwhile we literally check for that)
|
|
|
|
|
? instead.apply(ctx, [args, target.bind(ctx)])
|
|
|
|
|
? //@ts-ignore; TS thinks that `instead` possibly being undefined is bad (meanwhile we literally check for that)
|
|
|
|
|
instead.apply(ctx, [args, target.bind(ctx)])
|
|
|
|
|
: target.apply(ctx, args);
|
|
|
|
|
if (after === undefined) return res;
|
|
|
|
|
return after.apply(ctx, [args, res]);
|
|
|
|
|