All Projects → f5io → csp

f5io / csp

Licence: MIT license
A library for Communicating Sequential Processes in Node.js, built on top of async/await

Programming Languages

typescript
32286 projects
shell
77523 projects

Projects that are alternatives of or similar to csp

Libcsp
A concurrency C library 10x faster than Golang.
Stars: ✭ 1,160 (+1866.1%)
Mutual labels:  csp, concurrency
pool
A highly flexible process pooling library for Node.js
Stars: ✭ 18 (-69.49%)
Mutual labels:  csp, concurrency
csp.js
📺 CSP for vanilla JavaScript
Stars: ✭ 45 (-23.73%)
Mutual labels:  csp, concurrency
So 5 5
SObjectizer: it's all about in-process message dispatching!
Stars: ✭ 87 (+47.46%)
Mutual labels:  csp, concurrency
Aiochan
CSP-style concurrency for Python
Stars: ✭ 116 (+96.61%)
Mutual labels:  csp, concurrency
Sobjectizer
An implementation of Actor, Publish-Subscribe, and CSP models in one rather small C++ framework. With performance, quality, and stability proved by years in the production.
Stars: ✭ 172 (+191.53%)
Mutual labels:  csp, concurrency
Chymyst Core
Declarative concurrency in Scala - The implementation of the chemical machine
Stars: ✭ 142 (+140.68%)
Mutual labels:  csp, concurrency
SOMns
SOMns: A Newspeak for Concurrency Research
Stars: ✭ 62 (+5.08%)
Mutual labels:  csp, concurrency
nuxt-security
Module for Nuxt.js to configure security headers and more
Stars: ✭ 46 (-22.03%)
Mutual labels:  csp
optimistic lock coupling rs
🍋: A General Lock following paper "Optimistic Lock Coupling: A Scalable and Efficient General-Purpose Synchronization Method"
Stars: ✭ 21 (-64.41%)
Mutual labels:  concurrency
java-multithread
Códigos feitos para o curso de Multithreading com Java, no canal RinaldoDev do YouTube.
Stars: ✭ 24 (-59.32%)
Mutual labels:  concurrency
python3-concurrency
Python3爬虫系列的理论验证,首先研究I/O模型,分别用Python实现了blocking I/O、nonblocking I/O、I/O multiplexing各模型下的TCP服务端和客户端。然后,研究同步I/O操作(依序下载、多进程并发、多线程并发)和异步I/O(asyncio)之间的效率差别
Stars: ✭ 49 (-16.95%)
Mutual labels:  concurrency
frontreport
Simple frontend logging collector written in Go
Stars: ✭ 23 (-61.02%)
Mutual labels:  csp
plg system httpheader
This is a Joomla Plugin that provides setting of HTTP Headers
Stars: ✭ 19 (-67.8%)
Mutual labels:  csp
scalable-concurrent-containers
High performance containers and utilities for concurrent and asynchronous programming
Stars: ✭ 101 (+71.19%)
Mutual labels:  concurrency
circular-buffer
A Lock Free Concurrent Circular Buffer
Stars: ✭ 14 (-76.27%)
Mutual labels:  concurrency
Polyel-Framework
⚡️ Voltis Core: A PHP framework based on Swoole from the ground up
Stars: ✭ 22 (-62.71%)
Mutual labels:  concurrency
CrySPY
CrySPY is a crystal structure prediction tool written in Python.
Stars: ✭ 58 (-1.69%)
Mutual labels:  csp
thread-pool
A modern thread pool implementation based on C++20
Stars: ✭ 104 (+76.27%)
Mutual labels:  concurrency
fcsl-pcm
Partial Commutative Monoids
Stars: ✭ 20 (-66.1%)
Mutual labels:  concurrency

@f5io/csp

A library for Communicating Sequential Processes in Node.js, built on top of async/await and AsyncIterable.

npm version

Installation

This library requires async/await support in your Node.js runtime, so ideally node>=7.4.

$ npm install --save @f5io/csp

or

$ yarn add @f5io/csp

Example Usage

Below is a trivial example of usage, that plays on the standard ping-pong example.

const { channel, put, take } = require('@f5io/csp');

const timeout = ms => new Promise(resolve => setTimeout(resolve, ms));

const wiff = channel();
const waff = channel();

const createBall = () => ({ hits: 0, status: '' });

const createBat = async (inbound, outbound) => {
  for await (const ball of inbound) {
    ball.hits++;
    ball.status = ball.status === 'wiff!' ? 'waff!' : 'wiff!';
    console.log(`🎾  Ball hit ${ball.hits} time(s), ${ball.status}`);
    await timeout(500); // assume it's going to take a bit to hit the ball
    await put(outbound, ball); // smash the ball back
  }
};

createBat(waff, wiff); // create a bat that will wiff waffs
createBat(wiff, waff); // create a bat that will waff wiffs

put(waff, createBall());

ping pong

API

This library exposes 5 functions and one factory.

channel()

This factory method constructs a new channel and returns it. A channel contains no publicly accessible properties, but contains information about interactions with the channel.

const chan = channel();

put(channel, message) -> Promise

The put function requires the channel on which to put the supplied message. The put method returns a Promise which can be optionally awaited and will resolve when something is ready to take the message from the channel.

const chan = channel();
put(chan, 42);

// ...or...

await put(chan, 42);

take(channel) -> Promise

The take function requires the channel to take from. The take method returns a Promise which should always be awaited and will resolve with a message, when a message is available.

const chan = channel();

put(chan, 42);

const msg = await take(chan); // will receive 42

alts(...channels) -> Promise

The alts function will race taking values from multiple channels.

const chan1 = channel();
const chan2 = channel();

put(chan2, 42);
const msg = await alts(chan1, chan2); // will receive 42

select(Map<*, channel>|Set<channel>|Array<channel>|Object<string, channel>) -> Promise

The select function will race taking values from multiple channels, similar to alts, but will also return the key of the channel that was selected.

const chan1 = channel();
const chan2 = channel();

put(chan2, 42);
const channels = [chan1, chan2];
const result = await select(channels); // will receive [1, 42]

Works with Map and Set as well as with plain-old javascript arrays and objects.

A more complex TypeScript example might look like the following:

type Error = { message: string; };
type Result = { success: boolean; };

const errors = channel<Error>();
const results = channel<Result>();
const channels = new Set([ errors, results ]);

for await (const [ chan, msg ] of select<Error | Result>(channels)) {
  switch (chan) {
    case errors: {
      const { message }: Error = msg;
      console.log(message);
      break;
    }
    case results: {
      const { success }: Result = msg;
      console.log(success);
      break;
    }
  }
}

drain(channel) -> Promise

The drain function requires a channel which it will drain all messages from until empty, returning an array of messages.

const chan = channel();
put(chan, 42);
put(chan, 41);
put(chan, 40);
put(chan, 39);

const msgs = await drain(chan); // will receive [ 42, 41, 40, 39 ]

Contributions

Contributions are welcomed and appreciated!

  1. Fork this repository.
  2. Make your changes, documenting your new code with comments.
  3. Submit a pull request with a sane commit message.

Feel free to get in touch if you have any questions.

License

Please see the LICENSE file for more information.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].