From e686ad2fe1ae1bbf22864dc68644650219c294d6 Mon Sep 17 00:00:00 2001 From: Bagel03 <70449196+Bagel03@users.noreply.github.com> Date: Tue, 18 Jan 2022 12:37:52 -0500 Subject: [PATCH] Update JSDoc for Replacing Methods (#1336) * upgraded types for overriding methods * updated comments Co-authored-by: Edward Badel --- src/js/mods/mod_interface.js | 61 +++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/src/js/mods/mod_interface.js b/src/js/mods/mod_interface.js index acf47caf..6fb54be1 100644 --- a/src/js/mods/mod_interface.js +++ b/src/js/mods/mod_interface.js @@ -27,6 +27,27 @@ import { THEMES } from "../game/theme"; import { ModMetaBuilding } from "./mod_meta_building"; import { BaseHUDPart } from "../game/hud/base_hud_part"; +/** + * @typedef {{new(...args: any[]): any, prototype: any}} constructable + */ + +/** + * @template {(...args: any[]) => any} F + * @template P + * @typedef {(...args: [P, Parameters]) => ReturnType} beforePrams IMPORTANT: this puts the original parameters into an array + */ + +/** + * @template {(...args: any[]) => any} F + * @template P + * @typedef {(...args: [...Parameters, P]) => ReturnType} afterPrams + */ + +/** + * @template {(...args: any[]) => any} F + * @typedef {(...args: [...Parameters, ...any]) => ReturnType} extendsPrams + */ + export class ModInterface { /** * @@ -364,28 +385,58 @@ export class ModInterface { } /** - * Patches a method on a given object + * Patches a method on a given class + * @template {constructable} C the class + * @template {C["prototype"]} P the prototype of said class + * @template {keyof P} M the name of the method we are overriding + * @template {extendsPrams} O the method that will override the old one + * @param {C} classHandle + * @param {M} methodName + * @param {beforePrams} override */ replaceMethod(classHandle, methodName, override) { const oldMethod = classHandle.prototype[methodName]; classHandle.prototype[methodName] = function () { + //@ts-ignore This is true I just cant tell it that arguments will be Arguments return override.call(this, oldMethod.bind(this), arguments); }; } + /** + * Runs before a method on a given class + * @template {constructable} C the class + * @template {C["prototype"]} P the prototype of said class + * @template {keyof P} M the name of the method we are overriding + * @template {extendsPrams} O the method that will run before the old one + * @param {C} classHandle + * @param {M} methodName + * @param {O} executeBefore + */ runBeforeMethod(classHandle, methodName, executeBefore) { const oldHandle = classHandle.prototype[methodName]; classHandle.prototype[methodName] = function () { - executeBefore.apply(this, arguments); - return oldHandle.apply(this, arguments); + //@ts-ignore Same as above + executeBefore.apply(this, ...arguments); + return oldHandle.apply(this, ...arguments); }; } + /** + * Runs after a method on a given class + * @template {constructable} C the class + * @template {C["prototype"]} P the prototype of said class + * @template {keyof P} M the name of the method we are overriding + * @template {extendsPrams} O the method that will run before the old one + * @param {C} classHandle + * @param {M} methodName + * @param {O} executeAfter + */ runAfterMethod(classHandle, methodName, executeAfter) { const oldHandle = classHandle.prototype[methodName]; classHandle.prototype[methodName] = function () { - const returnValue = oldHandle.apply(this, arguments); - executeAfter.apply(this, arguments); + const returnValue = oldHandle.apply(this, ...arguments); + //@ts-ignore + executeAfter.apply(this, ...arguments); return returnValue; }; }