core3-utils

@wixc3/patterns on Github

Home > @wixc3/patterns > Signal

Signal class

Signal is a simple event emitter for one type of event.

Signature:

export declare class Signal<T> 

Remarks

Use Signals a public api for emitting events. Naming a signal is like naming the event the it triggers. If the name sounds like a property try to add a on prefix or Change/Signal suffix. All methods are bound to the Signal instance

Notice that the Signals are public. We don’t need to implement specific subscriptions on the class, unless we need to expose it as a remote service.

Example 1

const foodArrived = new Signal<Food>();

foodArrived.subscribe(() => {
  console.log('Food arrived!');
});

foodArrived.notify(new Food('pizza'));

Example 2

Usage in a class:

class LoginService {
    public onLoginSuccess = new Signal<User>();
    public onLoginFailure = new Signal<Error>();
    public onLoginStatusChange = new Signal<Status>();
}

Constructors

Constructor Modifiers Description
[(constructor)(handlers)](/core3-utils/patterns.signal._constructor_.html) Constructs a new instance of the `Signal` class

Properties

Property Modifiers Type Description
[notify](/core3-utils/patterns.signal.notify.html) (data: T) => void Notify all subscribers with arg data
[once](/core3-utils/patterns.signal.once.html) (handler: [Listener](/core3-utils/patterns.listener.html)<T>) => void Subscribe to only the next notification
[size](/core3-utils/patterns.signal.size.html) `readonly` number
[subscribe](/core3-utils/patterns.signal.subscribe.html) (handler: [Listener](/core3-utils/patterns.listener.html)<T>) => void Subscribe a notification callback
[unsubscribe](/core3-utils/patterns.signal.unsubscribe.html) (handler: [Listener](/core3-utils/patterns.listener.html)<T>) => void Unsubscribe an existing callback

Methods

Method Modifiers Description
[clear()](/core3-utils/patterns.signal.clear.html)
[has(value)](/core3-utils/patterns.signal.has.html)