|
|
|
@ -1,4 +1,12 @@
|
|
|
|
|
function wackyPatch(parentObj, name, patches) {
|
|
|
|
|
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
|
|
|
|
|
}) {
|
|
|
|
|
if (typeof parentObj === "string") {
|
|
|
|
|
//assume parentObj and name are switched around (for backcompat/convienence)
|
|
|
|
|
const tmp = parentObj;
|
|
|
|
@ -10,10 +18,11 @@ function wackyPatch(parentObj, name, patches) {
|
|
|
|
|
const instead = patches["instead"];
|
|
|
|
|
const after = patches["after"];
|
|
|
|
|
const handler = {
|
|
|
|
|
apply: (target, thisArg, [ctx, 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)])
|
|
|
|
|
: target.apply(ctx, args);
|
|
|
|
|
if (after === undefined) return res;
|
|
|
|
@ -36,15 +45,15 @@ function wackyPatch(parentObj, name, patches) {
|
|
|
|
|
return unpatch;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function before(parentObj, name, func) {
|
|
|
|
|
function before(parentObj: any, name: string, func: BeforeFunc) {
|
|
|
|
|
return wackyPatch(parentObj, name, { before: func });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function instead(parentObj, name, func) {
|
|
|
|
|
function instead(parentObj: any, name: string, func: InsteadFunc) {
|
|
|
|
|
return wackyPatch(parentObj, name, { instead: func });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function after(parentObj, name, func) {
|
|
|
|
|
function after(parentObj: any, name: string, func: AfterFunc) {
|
|
|
|
|
return wackyPatch(parentObj, name, { after: func });
|
|
|
|
|
}
|
|
|
|
|
|