Skip to content

Commit

Permalink
Merge pull request #8 from revenge-mod/master
Browse files Browse the repository at this point in the history
feat: better inferred typings
  • Loading branch information
twnlink authored Sep 19, 2024
2 parents b9ceea5 + 72088cb commit 8266b19
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/getPatchFunc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// functional programming strikes again! -- sink

import hook from "./hook";
import { PatchType, patchedFunctions } from "./shared";
import { AnyObject, KeysWithFunctionValues, PatchType, PatchTypeToCallbackMap, patchedFunctions } from "./shared";
import { unpatch } from "./unpatch";

// creates a hook if needed, else just adds one to the patches array
export default <CallbackType extends Function>(patchType: PatchType) =>
(
funcName: string,
funcParent: any,
callback: CallbackType,
export default <T extends PatchType>(patchType: T) =>
<Name extends KeysWithFunctionValues<Parent>, Parent extends AnyObject>(
funcName: Name,
funcParent: Parent,
callback: PatchTypeToCallbackMap<Parent[Name]>[T],
oneTime = false
) => {
let origFunc = funcParent[funcName];
Expand Down
6 changes: 3 additions & 3 deletions src/hook.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// calls relevant patches and returns the final result
import { patchedFunctions } from "./shared";
import { AnyFunction, patchedFunctions } from "./shared";

export default function (
patchedFunc: Function,
origFunc: Function,
patchedFunc: AnyFunction,
origFunc: AnyFunction,
funcArgs: unknown[],
// the value of `this` to apply
ctxt: any,
Expand Down
10 changes: 3 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import getPatchFunc from "./getPatchFunc";

type BeforeCallback = (args: any[]) => void | undefined | any[];
type InsteadCallback = (args: any[], origFunc: Function) => any;
type AfterCallback = (args: any[], ret: any) => void | undefined | any;

const before = getPatchFunc<BeforeCallback>("b");
const instead = getPatchFunc<InsteadCallback>("i");
const after = getPatchFunc<AfterCallback>("a");
const before = getPatchFunc("b");
const instead = getPatchFunc("i");
const after = getPatchFunc("a");

export { instead, before, after };
export { resetPatches as unpatchAll } from "./shared";
26 changes: 20 additions & 6 deletions src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
export type PatchType = "a" | "b" | "i";

export type PatchTypeToCallbackMap<F extends AnyFunction> = {
a: (args: Parameters<F>, ret: ReturnType<F>) => ReturnType<F>;
b: (args: Parameters<F>) => Parameters<F> | void | undefined;
i: (args: Parameters<F>, origFunc: F) => ReturnType<F>;
}

// we use this array multiple times
export const patchTypes: PatchType[] = ["a", "b", "i"];

export type Patch = {
// function name
n: string;
// original function
o: Function;
o: AnyFunction;
// WeakRef to parent object
p: WeakRef<any>;
// cleanups
c: Function[];
// after hooks
a: Map<symbol, Function>;
a: Map<symbol, AnyFunction>;
// before hooks
b: Map<symbol, Function>;
b: Map<symbol, AnyFunction>;
// instead hooks
i: Map<symbol, Function>;
i: Map<symbol, AnyFunction>;
};

export let patchedFunctions: WeakMap<Function, Patch>;
export type AnyFunction = (...args: any[]) => any;

export type KeysWithFunctionValues<T extends AnyObject> = {
[K in Extract<keyof T, string>]: T[K] extends AnyFunction ? K : never
}[Extract<keyof T, string>];

export type AnyObject = Record<any, any>;

export let patchedFunctions: WeakMap<AnyFunction, Patch>;
export let resetPatches = () =>
(patchedFunctions = new WeakMap<Function, Patch>());
(patchedFunctions = new WeakMap<AnyFunction, Patch>());

// Manual minification is funny
resetPatches();

0 comments on commit 8266b19

Please sign in to comment.