SignalHandlerSingleton
Signal handler singleton for global communication.
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'});
// 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}`);
}
}
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'});
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 |
clearAllReceiversForSignal(signalName: string) Remove all receivers for a specific signal. |
|
public |
hasReceiver(signalName: string, receiverName: string): boolean Check if a signal has a specific receiver. |
|
public |
removeAllSignalsFromReceiver(receiverName: string) 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 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:
Name | Type | Attribute | Description |
signalName | string | The name of the signal.
Typically something like
|
|
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 |
|
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:
Name | Type | Attribute | Description |
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.
public removeAllSignalsFromReceiver(receiverName: string) source
Remove all signals registered for a receiver.
Params:
Name | Type | Attribute | Description |
receiverName | string | The name of the receiver. |
public removeReceiver(signalName: string, receiverName: string) source
Remove a receiver for a signal added with SignalHandlerSingleton#addReceiver.
public send(signalName: string, data: *, infoCallback: *) source
Send a signal.
Params:
Name | Type | Attribute | Description |
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 |