@thi.ng/csp
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

@thi.ng/csp

npm version npm downloads Mastodon Follow

[!NOTE] This is one of 192 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.

🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️

About

Primitives & operators for Communicating Sequential Processes based on async/await and async iterables.

This package was temporarily deprecated (throughout most of 2023), but meanwhile has been reanimated in the form of a complete rewrite, using a new, more simple and more modern approach afforded by contemporary ES language features (and widespread support for them).

This new/current implementation is in most cases NOT compatible with earlier versions.

Provided are:

  • CSP Channel primitive supporting a choice of buffer behaviors (fifo, sliding, dropping, see thi.ng/buffers for options)
  • Composable channel operators (see list below)
  • Mult for channel multiplexing (one-to-many splitting) and dynamic add/removal of subscribers
  • PubSub for topic-based subscriptions, each topic implemented as Mult

Status

BETA - possibly breaking changes forthcoming

Search or submit any issues for this package

Related packages

  • @thi.ng/fibers - Process hierarchies & operators for cooperative multitasking
  • @thi.ng/rstream - Reactive streams & subscription primitives for constructing dataflow graphs / pipelines
  • @thi.ng/transducers-async - Async versions of various highly composable transducers, reducers and iterators

Installation

yarn add @thi.ng/csp

ESM import:

import * as csp from "@thi.ng/csp";

Browser ESM import:

<script type="module" src="https://esm.run/@thi.ng/csp"></script>

JSDelivr documentation

For Node.js REPL:

const csp = await import("@thi.ng/csp");

Package sizes (brotli'd, pre-treeshake): ESM: 1.77 KB

Dependencies

API

Generated API docs

PubSub

import { channel, consumeWith, pubsub } from "@thi.ng/csp";

// input channel (optional)
const src = channel<string>({ id: "users" });

// publisher with a topic function
// (topic here is the first character of each received string)
const pub = pubsub<string>(src, (x) => x[0]);

// create topic subscriptions (channel & debug consumer)
// under the hood each topic is a Mult (multiplexed channel)
// subscription channels are automatically named:
// `<src-id>-<topic>-tap<tapid>` (see below)
for (let i of "abc") {
    consumeWith(pub.subscribeTopic(i), (x, ch) => console.log(ch.id, x));
}

// start processing
for (let x of ["alice", "bert", "bella", "charlie", "arthur"]) {
    await src.write(x);
}
// users-a-tap0 alice
// users-b-tap1 bert
// users-b-tap1 bella
// users-c-tap2 charlie
// users-a-tap0 arthur

// pubsubs & mults are closed recursively once we close the input channel
src.close();

Authors

If this project contributes to an academic publication, please cite it as:

@misc{thing-csp,
  title = "@thi.ng/csp",
  author = "Karsten Schmidt",
  note = "https://thi.ng/csp",
  year = 2016
}

License

© 2016 - 2024 Karsten Schmidt // Apache License 2.0

Dependents (3)

Package Sidebar

Install

npm i @thi.ng/csp

Weekly Downloads

215

Version

3.0.0

License

Apache-2.0

Unpacked Size

45.2 kB

Total Files

18

Last publish

Collaborators

  • thi.ng