Skip to content

CommandHandlerComponent

Registers CommandHandlers with Obsidian and manages their lifecycle.

Call registerCommandHandlers to register a batch of handlers on demand (as many times as needed while the component is alive); dispose the returned DisposableEx to unregister exactly those handlers — including any menu events they registered — or let the component unload to remove every command still registered through it.

Import:

import { CommandHandlerComponent } from 'obsidian-dev-utils/obsidian/command-handlers/command-handler-component';

Signature:

export class CommandHandlerComponent extends ComponentEx

Extends: ComponentEx

Constructor

new CommandHandlerComponent(params: CommandHandlerComponentConstructorParams)

Creates a new command handler component.

Properties

PropertyTypeDescription
activeFileProviderActiveFileProviderProvider for accessing the currently active file.
commandRegistrarCommandRegistrarRegistrar used to add and remove commands with Obsidian.
menuEventRegistrarMenuEventRegistrarRegistrar for menu event handlers.
pluginNamestringThe name of the plugin that owns the commands.

Methods

MethodReturnsDescription
[Symbol.dispose]()voidDisposes of the component.
(Inherited from ComponentEx)
addChild(component)TComponentAdds a child component.

Mirrors the native Component.addChild contract: if this component is already loaded, the child is loaded immediately, so child._loaded is set before this method returns even when this component has async load logic. The child's async tail (if any) is sequenced into the load promise so a later loadWithPromises call awaits it.
(Inherited from ComponentEx)
ensureLoaded()voidEnsures the component is loaded, throwing if it is not.

Use this to guard public methods that register teardown-bearing resources (via Component.register, Component.registerEvent, Component.registerDomEvent, etc.). Registering before load is unsafe: Component.unload is a no-op while the component is not loaded, so any teardown registered beforehand would never run if the component is unloaded without first being loaded.
(Inherited from ComponentEx)
load()voidLoads the component.
(Inherited from ComponentEx)
loadWithPromises()null | Promise<void>Loads the component with promises.

Unlike load, this method never rejects with an individual error: every failure raised by onloadAsync or a child's load is collected, and once everything settles the returned Promise rejects with a single AggregateError holding all of them. Non-Error throwables are normalized via ErrorWrapper.create.
(Inherited from ComponentEx)
onloadAsync()Promise<void>Asynchronously loads the component.

Override to add async load logic, which is executed after Component.onload.
(Inherited from ComponentEx)
registerCommandHandlers(commandHandlers)DisposableExRegisters the given command handlers with Obsidian and provides each its own runtime registration context. Each handler's command is added immediately; the returned DisposableEx removes the commands registered by this call — and any menu events those handlers registered via their context's MenuEventRegistrar — when disposed. Any command still registered when the component unloads is removed automatically.
registerDisposable(disposable)TDisposableRegisters a Disposable so it is disposed when this component unloads, and returns it unchanged.

The recurring "tie a disposable to the component's lifecycle, then keep using it" idiom: the disposable is disposed on Component.unload (or earlier, if the caller disposes it directly — dispose is expected to be idempotent). Guard with ensureLoaded at the call site when registering before load would be unsafe.
(Inherited from ComponentEx)
removeChild(component)TComponentRemoves a child component.
(Inherited from ComponentEx)