@plato/signal
TypeScript icon, indicating that this package has built-in type declarations

1.3.2 • Public • Published

@plato/signal

npm version

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!");

Dependencies (0)

    Dev Dependencies (1)

    Package Sidebar

    Install

    npm i @plato/signal

    Weekly Downloads

    59

    Version

    1.3.2

    License

    MIT

    Unpacked Size

    4.76 kB

    Total Files

    7

    Last publish

    Collaborators

    • platobot
    • edgarplato
    • k4rliky
    • nickav
    • evgeny_trubish
    • jeremyjackson89
    • geoffblair
    • fralmei
    • platochris
    • hyperandroid
    • bennettstruttman
    • nazariglez