2.0.0 • Published 5 years ago

sigsel v2.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

sigsel.js

npm size deps

Selectable signals.

Inspired by Go's channels and Clojure's core.async, this library aims to make concurrent programming easier.

Since JS is single-threaded, there can be no data races across synchronous calls. This means we do not need channels and can just share variables. However, we still want a mechanism for synchronizing asynchronous operations. For that purpose, we use a signal object.

Installation

npm i sigsel

Usage

Counter

import { signal, select } from "sigsel";

(async () => {
  let counter = 0;
  const click = signal();
  const timeout = signal();
  for (;;) {
    const timer = setTimeout(timeout, 1000);
    render(<button onclick={click}>{counter}</button>);
    switch (await select(click, timeout)) {
      case click:
        counter++;
        break;
      case timeout:
        counter--;
        break;
    }
    clearTimeout(timer);
  }
})();