UNPKG

obsidian-dev-utils

Version:

This is the collection of useful functions that you can use for your Obsidian plugin development

63 lines (62 loc) 2.54 kB
/** * @packageDocumentation * * Improved type-safe wrapper of {@link https://www.npmjs.com/package/monkey-around} library. */ import type { Component } from 'obsidian'; import type { ConditionalKeys } from 'type-fest'; /** * A type of the factories to apply to the object. * * @typeParam Obj - The object to patch. */ export type Factories<Obj extends object> = Partial<{ [Key in ConditionalKeys<Obj, Function | undefined>]: WrapperFactory<Extract<Obj[Key], Function | undefined>>; }>; type Uninstaller = () => void; type WrapperFactory<T extends Function | undefined> = (next: T) => T; /** * Applies a patch to the object. * Better strongly-typed version of `monkey-around`. * * @typeParam Obj - The object to patch. * @param obj - The object to patch. * @param factories - The factories to apply to the object. * @returns The uninstaller that removes the patch when called. */ export declare function around<Obj extends object>(obj: Obj, factories: Factories<Obj>): Uninstaller; /** * Invokes a function with a patch applied to the object. * A patch is automatically removed when the function returns. * * @typeParam Obj - The object to patch. * @typeParam Result - The type of the result of the function. * @param obj - The object to patch. * @param factories - The factories to apply to the object. * @param fn - The function to invoke. * @returns The result of the function. */ export declare function invokeWithPatch<Obj extends object, Result>(obj: Obj, factories: Factories<Obj>, fn: () => Result): Result; /** * Invokes an async function with a patch applied to the object. * A patch is automatically removed when the function returns. * * @typeParam Obj - The object to patch. * @typeParam Result - The type of the result of the function. * @param obj - The object to patch. * @param factories - The factories to apply to the object. * @param fn - The function to invoke. * @returns The result of the function. */ export declare function invokeWithPatchAsync<Obj extends object, Result>(obj: Obj, factories: Factories<Obj>, fn: () => Promise<Result>): Promise<Result>; /** * Registers a patch to the object. * * @typeParam Obj - The object to patch. * @param component - The component to register the patch to. * @param obj - The object to patch. * @param factories - The factories to apply to the object. * @returns The uninstaller. */ export declare function registerPatch<Obj extends object>(component: Component, obj: Obj, factories: Factories<Obj>): Uninstaller; export {};