/** * @file * * Mock of Obsidian's `Component`, the base of everything with a load/unload lifecycle. */ import type { Component as ComponentOriginal, EventRef as EventRefOriginal } from 'obsidian'; /** * Mock of Obsidian's `Component`, with a working load/unload lifecycle. * * Loading loads children; unloading unloads children in reverse order, runs registered cleanups in reverse order * (detaching DOM listeners and event refs, clearing intervals) and then calls {@link Component.onunload}. */ export declare class Component { /** * The child components, unloaded when this component unloads. */ _children: Component[]; /** * The event refs registered with {@link Component.registerEvent}, detached when this component unloads. */ _events: EventRefOriginal[]; /** * Whether this component is currently loaded. */ _loaded: boolean; /** * Mock-only: the callbacks registered with {@link Component.register}, run in reverse order on unload. */ cleanups__: (() => unknown)[]; /** * Mock-only: the interval ids registered with {@link Component.registerInterval}, cleared on unload. */ intervals__: number[]; /** * Creates an unloaded component. */ constructor(); /** * Mock-only factory: creates a component, spyable via `vi.spyOn(Component, 'create__')`. * * @returns The new component. */ static create__(): Component; /** * Mock-only: views a value typed as Obsidian's `Component` as this mock. * * @param value - The value typed as the original `Component`. * @returns The same object, typed as the mock. */ static fromOriginalType__(value: ComponentOriginal): Component; /** * Adds a child component, loading it if this component is already loaded. * * @typeParam T - The type of the child component. * @param component - The component to add. * @returns The added component. */ addChild(component: T): T; /** * Mock-only: views this mock as Obsidian's `Component` type. * * @returns The same object, typed as the original `Component`. */ asOriginalType__(): ComponentOriginal; /** * Mock-only construction hook, called at the end of the constructor; a no-op meant for * `vi.spyOn(Component.prototype, 'constructor__')`. */ constructor__(): void; /** * Loads this component and then its children; does nothing if it is already loaded. */ load(): void; /** * Called when the component loads; override it to set the component up. A no-op by default. */ onload(): void; /** * Called when the component unloads; override it to tear the component down. A no-op by default. */ onunload(): void; /** * Registers a callback to run when this component unloads. * * @param callback - The cleanup callback. */ register(callback: () => unknown): void; /** * Adds a DOM event listener to a window, removed when this component unloads. * * @typeParam K - The event name. * @param el - The window to listen on. * @param type - The event name. * @param callback - The listener. * @param options - The listener options, also used to remove it. */ registerDomEvent(el: Window, type: K, callback: (this: HTMLElement, $event: WindowEventMap[K]) => unknown, options?: AddEventListenerOptions | boolean): void; /** * Adds a DOM event listener to a document, removed when this component unloads. * * @typeParam K - The event name. * @param el - The document to listen on. * @param type - The event name. * @param callback - The listener. * @param options - The listener options, also used to remove it. */ registerDomEvent(el: Document, type: K, callback: (this: HTMLElement, $event: DocumentEventMap[K]) => unknown, options?: AddEventListenerOptions | boolean): void; /** * Adds a DOM event listener to an element, removed when this component unloads. * * @typeParam K - The event name. * @param el - The element to listen on. * @param type - The event name. * @param callback - The listener. * @param options - The listener options, also used to remove it. */ registerDomEvent(el: HTMLElement, type: K, callback: (this: HTMLElement, $event: HTMLElementEventMap[K]) => unknown, options?: AddEventListenerOptions | boolean): void; /** * Registers an event ref to be detached, through its owning `Events` object's `offref`, when this component * unloads. * * @param ref - The event ref returned by an `on` call. */ registerEvent(ref: EventRefOriginal): void; /** * Registers an interval to be cleared with `clearInterval` when this component unloads. * * @param id - The id returned by `window.setInterval`. * @returns The same id. */ registerInterval(id: number): number; /** * Removes a child component, if present, and unloads it. * * @typeParam T - The type of the child component. * @param component - The component to remove. * @returns The removed component. */ removeChild(component: T): T; /** * Unloads this component: unloads and forgets its children, runs its registered cleanups, clears the tracked * events and intervals, then calls {@link Component.onunload}. Does nothing if it is not loaded. */ unload(): void; }