1.3.2 • Published 3 years ago
@plato/signal v1.3.2
@plato/signal
A Signal
is a specific event to which observers can subscribe to receive, with strong typing.
See the API reference for more information.
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!");