Home Manual Reference Source
import SignalHandlerSingleton from 'ievv_jsbase/lib/SignalHandlerSingleton'
public class | source

SignalHandlerSingleton

Signal handler singleton for global communication.

Example:

Basic example
let signalHandler = new SignalHandlerSingleton();
signalHandler.addReceiver('myapp.mysignal', 'myotherapp.MyReceiver', (receivedSignalInfo) => {
    console.log('Signal received. Data:', receivedSignalInfo.data);
});
signalHandler.send('myapp.mysignal', {'the': 'data'});
Recommended signal and receiver naming

// In myapp/menu/MenuComponent.js
class MenuComponent {
    constructor(menuName) {
        this.menuName = menuName;
        let signalHandler = new SignalHandlerSingleton();
        signalHandler.addReceiver(
            `toggleMenu#${this.menuName}`,
            'myapp.menu.MenuComponent',
            (receivedSignalInfo) => {
                 this.toggle();
            }
        );
    }
    toggle() {
        // Toggle the menu
    }
}

// In myotherapp/widgets/MenuToggle.js
class MenuToggle {
    constructor(menuName) {
        this.menuName = menuName;
    }
    toggle() {
        let signalHandler = new SignalHandlerSingleton();
        signalHandler.send(`toggleMenu#${this.menuName}`);
    }
}
Multiple receivers
let signalHandler = new SignalHandlerSingleton();
signalHandler.addReceiver('myapp.mysignal', 'myotherapp.MyFirstReceiver', (receivedSignalInfo) => {
    console.log('Signal received by receiver 1!');
});
signalHandler.addReceiver('myapp.mysignal', 'myotherapp.MySecondReceiver', (receivedSignalInfo) => {
    console.log('Signal received by receiver 1!');
});
signalHandler.send('myapp.mysignal', {'the': 'data'});
Debugging
let signalHandler = new SignalHandlerSingleton();
signalHandler.addReceiver('mysignal', 'MyReceiver', (receivedSignalInfo) => {
    console.log('received signal:', receivedSignalInfo.toString());
});
signalHandler.send('myapp.mysignal', {'the': 'data'}, (sentSignalInfo) => {
    console.log('sent signal info:', sentSignalInfo.toString());
});

Constructor Summary

Public Constructor
public

Method Summary

Public Methods
public

addReceiver(signalName: string, receiverName: string, callback: *)

Add a receiver for a specific signal.

public

Remove all receivers for all signals.

public

Remove all receivers for a specific signal.

public

hasReceiver(signalName: string, receiverName: string): boolean

Check if a signal has a specific receiver.

public

Remove all signals registered for a receiver.

public

removeReceiver(signalName: string, receiverName: string)

Remove a receiver for a signal added with SignalHandlerSingleton#addReceiver.

public

send(signalName: string, data: *, infoCallback: *)

Send a signal.

Public Constructors

public constructor source

Public Methods

public addReceiver(signalName: string, receiverName: string, callback: *) source

Add a receiver for a specific signal.

Params:

NameTypeAttributeDescription
signalName string

The name of the signal. Typically something like toggleMenu or myapp.toggleMenu.

 What if we have multiple objects listening for this ``toggleMenu``
 signal, and we only want to toggle a specific menu? You need
 to ensure the signalName is unique for each menu. We recommend
 that you do this by adding ``#<context>``. For example
 ``toggleMenu#mainmenu``. This is shown in one of the examples
 above.
receiverName string

The name of the receiver. Must be unique for the signal. We recommend that you use a very explicit name for your signals. It should normally be the full path to the method or function receiving the signal. So if you have a class named myapp/menu/MenuComponent.js that receives a signal to toggle the menu, the receiverName should be myapp.menu.MenuComponent.

callback *

The callback to call when the signal is sent. The callback is called with a single argument - a ReceivedSignalInfo object.

public clearAllReceiversForAllSignals() source

Remove all receivers for all signals.

Useful for debugging and tests, but should not be used for production code.

public clearAllReceiversForSignal(signalName: string) source

Remove all receivers for a specific signal.

Params:

NameTypeAttributeDescription
signalName string

The name of the signal to remove.

public hasReceiver(signalName: string, receiverName: string): boolean source

Check if a signal has a specific receiver.

Params:

NameTypeAttributeDescription
signalName string

The name of the signal.

receiverName string

The name of the receiver.

Return:

boolean

public removeAllSignalsFromReceiver(receiverName: string) source

Remove all signals registered for a receiver.

Params:

NameTypeAttributeDescription
receiverName string

The name of the receiver.

public removeReceiver(signalName: string, receiverName: string) source

Remove a receiver for a signal added with SignalHandlerSingleton#addReceiver.

Params:

NameTypeAttributeDescription
signalName string

The name of the signal.

receiverName string

The name of the receiver.

public send(signalName: string, data: *, infoCallback: *) source

Send a signal.

Params:

NameTypeAttributeDescription
signalName string

The name of the signal to send.

data *

Data to send to the callback of all receivers registered for the signal.

infoCallback *

An optional callback that receives information about the signal. Useful for debugging what actually received the signal. The infoCallback is called with a single argument - a SentSignalInfo object.