Skip to main content

Class: ObservableImpl

@ima/core.ObservableImpl

An Observable is a class that manages event listeners and allows distributing events to the registered listeners. It maintains a history of events and supports persistent events that are not cleared during route changes.

Remarks

  • The Observable class relies on a Dispatcher to handle the actual event distribution.
  • It maintains a history of events, which can be limited by a maximum history length.

Hierarchy

Constructors

constructor

new ObservableImpl(dispatcher, settings?)

Creates an instance of Observable.

Parameters

NameTypeDescription
dispatcherDispatcherThe dispatcher responsible for managing event listeners.
settings?ObjectOptional settings for the Observable instance.
settings.maxHistoryLength?number-

Overrides

Observable.constructor

Defined in

packages/core/src/event/ObservableImpl.ts:40

Properties

_activityHistory

Protected _activityHistory: Map<string, unknown[]>

Defined in

packages/core/src/event/ObservableImpl.ts:25


_dispatcher

Protected _dispatcher: Dispatcher

Defined in

packages/core/src/event/ObservableImpl.ts:23


_observers

Protected _observers: Map<string, Map<DispatcherListener<any>, Set<unknown>>>

Defined in

packages/core/src/event/ObservableImpl.ts:24


_persistentEvents

Protected _persistentEvents: Set<string>

Defined in

packages/core/src/event/ObservableImpl.ts:26


_settings

Protected _settings: undefined | { maxHistoryLength?: number }

Defined in

packages/core/src/event/ObservableImpl.ts:27


$dependencies

Static $dependencies: Dependencies

Defined in

packages/core/src/event/ObservableImpl.ts:29

Methods

_handleDispatcherEvent

_handleDispatcherEvent(event, data): void

Handles dispatcher events by updating the activity history and notifying observers. It also resets the activity history for non-persistent events on BEFORE_HANDLE_ROUTE ecvent.

Parameters

NameTypeDescription
eventstringThe name of the event being dispatched.
dataanyThe data associated with the event.

Returns

void

Defined in

packages/core/src/event/ObservableImpl.ts:148


clear

clear(): ObservableImpl

Clears all persistent events, observers, and activity history from the observable.

This method will remove all stored events, registered observers, and any recorded activity history, effectively resetting the observable to its initial state.

Returns

ObservableImpl

The instance of the Observable for chaining.

Overrides

Observable.clear

Defined in

packages/core/src/event/ObservableImpl.ts:72


destroy

destroy(): ObservableImpl

Destroys the observable by clearing its internal state and removing all event listeners.

Returns

ObservableImpl

The instance of the Observable for chaining.

Overrides

Observable.destroy

Defined in

packages/core/src/event/ObservableImpl.ts:62


init

init(): ObservableImpl

Initializes the observable.

Returns

ObservableImpl

The instance of the Observable for chaining.

Overrides

Observable.init

Defined in

packages/core/src/event/ObservableImpl.ts:52


registerPersistenEvent

registerPersistenEvent(event): ObservableImpl

Registers an event as persistent, meaning its history won't be cleared upon calling the clear method (route change).

Parameters

NameTypeDescription
eventstringThe name of the event to be registered as persistent. This can be a key from the DispatcherEventsMap or any string.

Returns

ObservableImpl

The instance of the Observable for chaining.

Overrides

Observable.registerPersistenEvent

Defined in

packages/core/src/event/ObservableImpl.ts:83


subscribe

subscribe(event, observer, scope?): ObservableImpl

Subscribes an observer to a specific event. When the event is dispatched, the observer will be notified and executed within the provided scope. If the event has already occurred, the observer will be immediately invoked with the historical data.

Parameters

NameTypeDescription
eventstringThe event to subscribe to.
observerDispatcherListener<any>The observer function to be called when the event is dispatched.
scope?unknownThe scope in which the observer function should be executed. This is optional.

Returns

ObservableImpl

The instance of the Observable for chaining.

Overrides

Observable.subscribe

Defined in

packages/core/src/event/ObservableImpl.ts:92


unsubscribe

unsubscribe(event, observer, scope?): ObservableImpl

Unsubscribes an observer from a specific event.

Parameters

NameTypeDescription
eventstringThe event name or key from the DispatcherEventsMap.
observerDispatcherListener<any>The observer (listener) to be unsubscribed.
scope?unknownOptional scope to be used for the observer.

Returns

ObservableImpl

The current instance for chaining.

Overrides

Observable.unsubscribe

Defined in

packages/core/src/event/ObservableImpl.ts:119