Skip to content

component-lifecycle


component-lifecycle / Monitor

Class: Monitor<P>

Monitor class for observing component lifecycle events at the document level.

Example

typescript
// Create a monitor for components with prefix "my-component"
const monitor = new Monitor("my-component").start();

// Enable debug tracing for entire application: all component events appear in console.
monitor.setLevel('DEBUG');

// Switch to production mode (silent)
monitor.setLevel('ERROR');

// Check current level
const level = monitor.getLevel(); // "ERROR"

// Stopping monitor
monitor.stop();

Type Parameters

P

P extends string = "component"

The event prefix to monitor. Defaults to "component".

Constructors

Constructor

new Monitor<P>(prefix): Monitor<P>

Creates a new Monitor instance for the given event prefix.

Parameters

prefix

P

The event prefix to monitor (e.g., "component", "my-component")

Returns

Monitor<P>

Methods

getLevel()

getLevel(): LogLevels | undefined

Returns the current logging level.

Returns

LogLevels | undefined

The current log level or undefined if the monitor is not started


on()

protected on(eventName, handler): this

Adds a single document event listener and tracks it.

This method should be use for automatic cleanup of event listeners in level change or stop.

Parameters

eventName

string

The base event name (without prefix)

handler

(event) => void

The event handler function

Returns

this

The Monitor instance for chaining


setLevel()

setLevel(level): this

Sets the logging level for this monitor.

Parameters

level

LogLevels

The log level to set ("DEBUG", "INFO", "WARN", or "ERROR")

Returns

this

The Monitor instance for chaining

Remarks

  • ERROR: No listeners attached, zero overhead.
  • WARN: Previous level + Listens to transition events (see TransitionEventMap).
  • INFO: Previous level + (no additional events at this level).
  • DEBUG: Previous level + Listens to all lifecycle events (see LifecycleEventMap).

When level changes, all existing listeners are removed and new ones are added based on the new level.

Throws

An error if the monitor has not been started


setupDebug()

protected setupDebug(): void

Hooks that are called to set up debug level listeners.

Returns

void

Remarks

This method is intended to be overridden in subclasses calling super.setupDebug() to preserve base functionality.


setupError()

protected setupError(): void

Hooks that are called to set up error level listeners.

Returns

void

Remarks

This method is intended to be overridden in subclasses calling super.setupError() to preserve base functionality.


setupInfo()

protected setupInfo(): void

Hooks that are called to set up info level listeners.

Returns

void

Remarks

This method is intended to be overridden in subclasses calling super.setupInfo() to preserve base functionality.


setupWarn()

protected setupWarn(): void

Hooks that are called to set up warn level listeners.

Returns

void

Remarks

This method is intended to be overridden in subclasses calling super.setupWarn() to preserve base functionality.


start()

start(level?): this

Initializes the monitor with the specified log level and attaches the appropriate listeners.

Parameters

level?

LogLevels = LogLevels.ERROR

The log level to start the monitor with (defaults to ERROR if not specified)

Returns

this

The Monitor instance for chaining

Throws

An error if the monitor has already been started


stop()

stop(): this

Removes all currently active document listeners.

Returns

this

The Monitor instance for chaining

Remarks

This method is intended to be called when the monitor is no longer needed to clean up resources.

Throws

An error if the monitor has not been started