Class: Signal

Signal

A typed event: listeners subscribe with connect and are called with the arguments passed to emit.

Listeners run in ascending order, then in the order they connected. A listener connected while the signal is emitting first runs on the next emit; one disconnected while it is emitting does not run.

new Signal ()

A typed event: listeners subscribe with connect and are called with the arguments passed to emit.

Example

 const onChange = new Signal<(value: number) => void>();
 const connection = onChange.connect((value) => console.log(value));

 onChange.emit(1); // logs 1
 connection.disconnect();

Methods

connect (callback, order) SignalConnection

Subscribes a listener.

Name Type Default Description
callback THandler

Called with the arguments of every emit.

order number 0

Listeners with a higher order run later. Defaults to 0.

Returns:
Type Description
SignalConnection

disconnect (callback) boolean

Unsubscribes the first listener connected with this callback.

Name Type Description
callback THandler

The callback passed to connect.

Returns:
Type Description
boolean false if it was not connected.

disconnectAll () void

Unsubscribes every listener.

emit (…args) void

Calls every enabled listener.

Name Type Description
args Parameters<THandler>

Passed to each listener.

getConnectionsCount () number

Returns:
Type Description
number The number of connected listeners.

hasConnections () boolean

Returns:
Type Description
boolean true if at least one listener is connected.