-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from revenge-mod/master
feat: better inferred typings
- Loading branch information
Showing
4 changed files
with
32 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |