Skip to content

obsidian/components/monkey-around-component

Improved type-safe wrapper of https://www.npmjs.com/package/monkey-around library.

ClassDescription
MonkeyAroundComponentA component that manages monkey-patches with lifecycle-bound cleanup. All patches registered via this component are automatically uninstalled when the component unloads.
InterfaceDescription
MonkeyAroundComponentRegisterFunctionPatchParamsParameters passed to MonkeyAroundComponent#registerFunctionPatch.
MonkeyAroundComponentRegisterMethodPatchParamsParameters passed to MonkeyAroundComponent#registerMethodPatch.
PatchHandlerParamsParameters passed to a PatchHandlerFunction callback.
PostPatchHandlerParamsParameters passed to a PostPatchHandlerFunction callback.
FunctionDescription
aroundApplies a patch to the object. Better strongly-typed version of monkey-around.
hasPatchTokenChecks if a function has a specific patch token. A token is carried by the method a MonkeyAroundComponent#registerMethodPatch call INSTALLED, so the intended caller is a later patch of the same method, checking the originalMethod it wrapped in order to defer to the patch already in place: ts patchHandler: (\{ fallback, originalMethod \}) => \{ if (hasPatchToken(originalMethod, PATCH_TOKEN)) \{ return fallback(); \} ... \} The check is made per call, so it stops reporting the earlier patch once that patch’s component unloads.
TypeDescription
FactoriesA type of the factories to apply to the object.
FunctionKeysKeys of Obj whose value is callable — a method, or a function-like object such as a Debouncer. These are the members accepted by MonkeyAroundComponent#registerFunctionPatch.
MethodKeysKeys of Obj whose value is a plain function (a “real method”). Function-like objects that carry extra members are excluded — e.g. a Debouncer (which adds cancel/run) belongs to MonkeyAroundComponent#registerFunctionPatch, not MonkeyAroundComponent#registerMethodPatch. The check is whether the member has any own keys beyond a bare function’s (Exclude<keyof T, keyof GenericFunction> is never).
PatchHandlerFunctionA patch handler function that intercepts calls to a method on an object.
PostPatchHandlerFunctionA post-patch handler function that runs after a patch is applied.