[!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! ❤️
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 asMult
BETA - possibly breaking changes forthcoming
Search or submit any issues for this package
- @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
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>
For Node.js REPL:
const csp = await import("@thi.ng/csp");
Package sizes (brotli'd, pre-treeshake): ESM: 1.77 KB
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();
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
}
© 2016 - 2024 Karsten Schmidt // Apache License 2.0