Skip to main content
Module

x/effection/mod.ts>Signal

Structured concurrency and effects for JavaScript
Latest
interface Signal
implements Stream<T, TClose>
import { type Signal } from "https://deno.land/x/effection@3.0.3/mod.ts";

Convert plain JavaScript function calls into a Stream that can be consumed within an operation. If no operation is subscribed to a signal's stream, then sending messages to it is a no-op.

Signals are particularly suited to be installed as event listeners.

Examples

Example 1

import { createSignal, each } from "effection";

export function* logClicks(function*(button) {
  let clicks = createSignal<MouseEvent>();

  button.addEventListener("click", clicks.send);

  try {
    for (let click of yield* each(clicks)) {
      console.log(`click:`, click);
      yield* each.next();
    }
  } finally {
    button.removeEventListener("click", clicks.send);
  }
})

Methods

send(value: T): void

Send a value to all the consumers of this signal.

close(value: TClose): void

Send the final value of this signal to all its consumers.