@lumino/signaling

  • Version 2.1.3
  • Published
  • 163 kB
  • 2 dependencies
  • BSD-3-Clause license

Install

npm i @lumino/signaling
yarn add @lumino/signaling
pnpm add @lumino/signaling

Overview

Lumino Signals and Slots

Index

Classes

class Signal

class Signal<T, U> implements ISignal<T, U> {}
  • A concrete implementation of ISignal.

    #### Example

    import { ISignal, Signal } from '@lumino/signaling';
    class SomeClass {
    constructor(name: string) {
    this.name = name;
    }
    readonly name: string;
    get valueChanged: ISignal<this, number> {
    return this._valueChanged;
    }
    get value(): number {
    return this._value;
    }
    set value(value: number) {
    if (value === this._value) {
    return;
    }
    this._value = value;
    this._valueChanged.emit(value);
    }
    private _value = 0;
    private _valueChanged = new Signal<this, number>(this);
    }
    function logger(sender: SomeClass, value: number): void {
    console.log(sender.name, value);
    }
    let m1 = new SomeClass('foo');
    let m2 = new SomeClass('bar');
    m1.valueChanged.connect(logger);
    m2.valueChanged.connect(logger);
    m1.value = 42; // logs: foo 42
    m2.value = 17; // logs: bar 17

constructor

constructor(sender: {});
  • Construct a new signal.

    Parameter sender

    The sender which owns the signal.

property sender

readonly sender: {};
  • The sender which owns the signal.

method connect

connect: (slot: Slot<T, U>, thisArg?: unknown) => boolean;
  • Connect a slot to the signal.

    Parameter slot

    The slot to invoke when the signal is emitted.

    Parameter thisArg

    The this context for the slot. If provided, this must be a non-primitive object.

    Returns

    true if the connection succeeds, false otherwise.

method disconnect

disconnect: (slot: Slot<T, U>, thisArg?: unknown) => boolean;
  • Disconnect a slot from the signal.

    Parameter slot

    The slot to disconnect from the signal.

    Parameter thisArg

    The this context for the slot. If provided, this must be a non-primitive object.

    Returns

    true if the connection is removed, false otherwise.

method emit

emit: (args: U) => void;
  • Emit the signal and invoke the connected slots.

    Parameter args

    The args to pass to the connected slots.

    #### Notes Slots are invoked synchronously in connection order.

    Exceptions thrown by connected slots will be caught and logged.

class Stream

class Stream<T, U> extends Signal<T, U> implements IStream<T, U> {}
  • A concrete implementation of IStream.

    #### Example

    import { IStream, Stream } from '@lumino/signaling';
    class SomeClass {
    constructor(name: string) {
    this.name = name;
    }
    readonly name: string;
    get pings(): IStream<this, string> {
    return this._pings;
    }
    ping(value: string) {
    this._pings.emit(value);
    }
    private _pings = new Stream<this, string>(this);
    }
    let m1 = new SomeClass('foo');
    m1.pings.connect((_, value: string) => {
    console.log('connect', value);
    });
    void (async () => {
    for await (const ping of m1.pings) {
    console.log('iterator', ping);
    }
    })();
    m1.ping('alpha'); // logs: connect alpha
    // logs: iterator alpha
    m1.ping('beta'); // logs: connect beta
    // logs: iterator beta

method [Symbol.asyncIterator]

[Symbol.asyncIterator]: () => AsyncIterableIterator<U>;
  • Return an async iterator that yields every emission.

method emit

emit: (args: U) => void;
  • Emit the signal, invoke the connected slots, and yield the emission.

    Parameter args

    The args to pass to the connected slots.

method stop

stop: () => void;
  • Stop the stream's async iteration.

Interfaces

interface ISignal

interface ISignal<T, U> {}
  • An object used for type-safe inter-object communication.

    #### Notes Signals provide a type-safe implementation of the publish-subscribe pattern. An object (publisher) declares which signals it will emit, and consumers connect callbacks (subscribers) to those signals. The subscribers are invoked whenever the publisher emits the signal.

method connect

connect: (slot: Slot<T, U>, thisArg?: any) => boolean;
  • Connect a slot to the signal.

    Parameter slot

    The slot to invoke when the signal is emitted.

    Parameter thisArg

    The this context for the slot. If provided, this must be a non-primitive object.

    Returns

    true if the connection succeeds, false otherwise.

    #### Notes Slots are invoked in the order in which they are connected.

    Signal connections are unique. If a connection already exists for the given slot and thisArg, this method returns false.

    A newly connected slot will not be invoked until the next time the signal is emitted, even if the slot is connected while the signal is dispatching.

method disconnect

disconnect: (slot: Slot<T, U>, thisArg?: any) => boolean;
  • Disconnect a slot from the signal.

    Parameter slot

    The slot to disconnect from the signal.

    Parameter thisArg

    The this context for the slot. If provided, this must be a non-primitive object.

    Returns

    true if the connection is removed, false otherwise.

    #### Notes If no connection exists for the given slot and thisArg, this method returns false.

    A disconnected slot will no longer be invoked, even if the slot is disconnected while the signal is dispatching.

interface IStream

interface IStream<T, U> extends ISignal<T, U>, AsyncIterable<U> {}
  • An object that is both a signal and an async iterable.

Type Aliases

type Slot

type Slot<T, U> = (sender: T, args: U) => void;
  • A type alias for a slot function.

    Parameter sender

    The object emitting the signal.

    Parameter args

    The args object emitted with the signal.

    #### Notes A slot is invoked when a signal to which it is connected is emitted.

Namespaces

namespace Signal

namespace Signal {}
  • The namespace for the Signal class statics.

function clearData

clearData: (object: unknown) => void;
  • Clear all signal data associated with the given object.

    Parameter object

    The object for which the data should be cleared.

    #### Notes This removes all signal connections and any other signal data associated with the object.

function disconnectAll

disconnectAll: (object: unknown) => void;
  • Remove all connections where an object is the sender or receiver.

    Parameter object

    The object of interest.

    #### Notes If a thisArg is provided when connecting a signal, that object is considered the receiver. Otherwise, the slot is considered the receiver.

function disconnectBetween

disconnectBetween: (sender: unknown, receiver: unknown) => void;
  • Remove all connections between a sender and receiver.

    Parameter sender

    The sender object of interest.

    Parameter receiver

    The receiver object of interest.

    #### Notes If a thisArg is provided when connecting a signal, that object is considered the receiver. Otherwise, the slot is considered the receiver.

function disconnectReceiver

disconnectReceiver: (receiver: unknown) => void;
  • Remove all connections where the given object is the receiver.

    Parameter receiver

    The receiver object of interest.

    #### Notes If a thisArg is provided when connecting a signal, that object is considered the receiver. Otherwise, the slot is considered the receiver.

function disconnectSender

disconnectSender: (sender: unknown) => void;
  • Remove all connections where the given object is the sender.

    Parameter sender

    The sender object of interest.

function getExceptionHandler

getExceptionHandler: () => ExceptionHandler;
  • Get the signal exception handler.

    Returns

    The current exception handler.

    #### Notes The default exception handler is console.error.

function setExceptionHandler

setExceptionHandler: (handler: ExceptionHandler) => ExceptionHandler;
  • Set the signal exception handler.

    Parameter handler

    The function to use as the exception handler.

    Returns

    The old exception handler.

    #### Notes The exception handler is invoked when a slot throws an exception.

type ExceptionHandler

type ExceptionHandler = (err: Error) => void;
  • A type alias for the exception handler function.

Package Files (1)

Dependencies (2)

Dev Dependencies (19)

Peer Dependencies (0)

No peer dependencies.

Badge

To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@lumino/signaling.

  • Markdown
    [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@lumino/signaling)
  • HTML
    <a href="https://www.jsdocs.io/package/@lumino/signaling"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>