Skip to content

components/monkey-around-component

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

Class Description
MonkeyAroundComponent A component that manages monkey-patches with lifecycle-bound cleanup. All patches registered via this component are automatically uninstalled when the component unloads.
Interface Description
MonkeyAroundComponentRegisterFunctionPatchParams Parameters passed to MonkeyAroundComponent#registerFunctionPatch.
MonkeyAroundComponentRegisterMethodPatchParams Parameters passed to MonkeyAroundComponent#registerMethodPatch.
PatchHandlerParams Parameters passed to a PatchHandlerFunction callback.
PostPatchHandlerParams Parameters passed to a PostPatchHandlerFunction callback.
Function Description
hasPatchToken Checks 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.
patchMethod Patches one method of an object for a scope, and returns a DisposableEx that restores it. The scoped counterpart of MonkeyAroundComponent#registerMethodPatch, for a patch whose lifetime is a block rather than a component: ts using _ = patchMethod(\{ $object: editor, methodName: 'transaction', patchHandler: (\{ fallback \}) => \{ // ... return fallback(); \} \}); It takes the same parameters, and runs the same implementation, through a MonkeyAroundComponent of its own that is loaded here and unloaded on dispose. So once, patchToken and postPatchHandler behave exactly as they do there, and a patch stacked on the same method unloads in any order. Disposing more than once is a no-op.
Type Description
Factories A type of the factories to apply to the object.
FunctionKeys Keys 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.
MethodKeys Keys 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).
PatchHandlerFunction A patch handler function that intercepts calls to a method on an object.
PatchMethodParams Parameters passed to patchMethod: the same as MonkeyAroundComponent#registerMethodPatch takes.
PostPatchHandlerFunction A post-patch handler function that runs after a patch is applied. It returns nothing and is not handed the patched method: the method installed is always the one the patch builds, so a post-patch handler cannot graft members onto it. A capability beyond the patched method’s own signature belongs on the plugin’s published API instead.