5.0.0 • Published 1 year ago

signo v5.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

Signo

Simple Typed Signals.

NPM

Designed as a simple way to communicate between functional code and a user interface.

Provides two classes:

  • Signal is for communicating values from the sender to one or more receivers. Note that the value type can be omitted (void).

  • SignalWithResult is for also returning a result to the sender.

Signals can be sent (send), awaited (await), subscribed (on), and unsubscribed (off).

Examples

Signal without value or result:

import { Signal } from "signo";

const exampleSignal = new Signal();

exampleSignal.on(() => {
    console.log("Hello world!");
});

exampleSignal.send();

Expected output:

Hello world!

Signal with value:

import { Signal } from "signo";

const exampleSignal = new Signal<string>();

exampleSignal.on(value => {
    console.log(value);
});

exampleSignal.on(async value => {
    console.log(`Async ${value}`);
});

exampleSignal.send("Hello world!", () => {
    console.log("Done!")
});

exampleSignal.await("Async!").then(() => {
    console.log("Async done!");
}).catch(console.error);

Expected output:

Hello world!
Async!
Async Hello world!
Done!
Async Async!
Async done!

Signal with result:

import { SignalWithResult } from "signo";

const exampleSignal = new SignalWithResult<string>();

exampleSignal.on(() => {
    // Undefined results are ignored
    return undefined;
});

exampleSignal.on(() => {
    // The first non-undefined result is used
    return `Alpha`;
});

exampleSignal.on(() => {
    // Subsequent results are ignored
    return `Beta`;
});

exampleSignal.send(void, result => {
    console.log(result);
});

Expected output:

Alpha

Signal with value and result:

import { SignalWithResult } from "signo";

const exampleSignal = new SignalWithResult<string, number>();

exampleSignal.on(value => {
    // Undefined results are ignored
    return undefined;
});

exampleSignal.on(value => {
    // The first non-undefined result is used
    return `Is your number ${value}?`;
});

exampleSignal.on(value => {
    // Subsequent results are ignored
    return `Hello ${value}!`;
});

exampleSignal.send(42, result => {
    console.log(result);
});

Expected output:

Is your number 42?
5.0.0

1 year ago

4.1.0

1 year ago

4.0.3

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.2

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.0

2 years ago