sigsel
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

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);
  }
})();

Package Sidebar

Install

npm i sigsel

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

4.29 kB

Total Files

6

Last publish

Collaborators

  • rliang