@plato/signal
A Signal
is a specific event to which observers can subscribe to receive, with strong typing.
Why?
Instead of an inheritable class (like Node's EventEmitter
), this library promotes composition over inheritance.
Usage
import { Signal } from "@plato/signal";
const onPing = new Signal();
onPing.receive(() => {
// Received the "onPing" signal
});
onPing.emit();
By default, a SignalReceiver
will have a generic type of (...args: any[]) => void
. However, you can strongly type receivers by passing a type variable when constructing a Signal
:
// Create a signal with a strongly typed receiver
const onMessage = new Signal<(message: string) => void>();
onMessage.receive((message) => {
// TODO: Do something with "message", which is a string
});
onMessage.emit("Hello Plato!");