All Projects → rads → csp.js

rads / csp.js

Licence: MIT License
📺 CSP for vanilla JavaScript

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to csp.js

Vue Concurrency
A library for encapsulating asynchronous operations and managing concurrency for Vue and Composition API.
Stars: ✭ 147 (+226.67%)
Mutual labels:  concurrency, generators
So 5 5
SObjectizer: it's all about in-process message dispatching!
Stars: ✭ 87 (+93.33%)
Mutual labels:  csp, concurrency
psched
Priority-based Task Scheduling for Modern C++
Stars: ✭ 59 (+31.11%)
Mutual labels:  concurrency, mit-license
queueable
Convert streams to async ⌛ iterables ➰
Stars: ✭ 43 (-4.44%)
Mutual labels:  concurrency, generators
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 (+282.22%)
Mutual labels:  csp, concurrency
Aiochan
CSP-style concurrency for Python
Stars: ✭ 116 (+157.78%)
Mutual labels:  csp, concurrency
Libcsp
A concurrency C library 10x faster than Golang.
Stars: ✭ 1,160 (+2477.78%)
Mutual labels:  csp, concurrency
csp
A library for Communicating Sequential Processes in Node.js, built on top of async/await
Stars: ✭ 59 (+31.11%)
Mutual labels:  csp, concurrency
Chymyst Core
Declarative concurrency in Scala - The implementation of the chemical machine
Stars: ✭ 142 (+215.56%)
Mutual labels:  csp, concurrency
Asynquence
Asynchronous flow control (promises, generators, observables, CSP, etc)
Stars: ✭ 1,737 (+3760%)
Mutual labels:  csp, generators
SOMns
SOMns: A Newspeak for Concurrency Research
Stars: ✭ 62 (+37.78%)
Mutual labels:  csp, concurrency
pool
A highly flexible process pooling library for Node.js
Stars: ✭ 18 (-60%)
Mutual labels:  csp, concurrency
sto
Software Transactional Objects
Stars: ✭ 40 (-11.11%)
Mutual labels:  concurrency
Async-Channel
Python async multi-task communication library. Used by OctoBot project.
Stars: ✭ 13 (-71.11%)
Mutual labels:  concurrency
DokoDemoPainter
DokoDemoPainter is a fast and easy texture painting solution for Unity and can paint on both regular and skinned meshes
Stars: ✭ 43 (-4.44%)
Mutual labels:  mit-license
co-sh
Using ES6 Proxies & Generators to run shell commands
Stars: ✭ 24 (-46.67%)
Mutual labels:  generators
envy
envy: Deserialize environment variables into type-safe structs
Stars: ✭ 64 (+42.22%)
Mutual labels:  mit-license
learn-wgpu
A guided introduction to using the wgpu crate.
Stars: ✭ 26 (-42.22%)
Mutual labels:  mit-license
await async
Provide await and async methods to Crystal Lang
Stars: ✭ 71 (+57.78%)
Mutual labels:  concurrency
KO--CSP
更新pat、csp以及研究生上机考试的刷题笔记
Stars: ✭ 23 (-48.89%)
Mutual labels:  csp

CSP.js

This library provides CSP primitives for vanilla JavaScript. If you've used Clojure's core.async before, picking up CSP.js is easy.

Much thanks to the contributors of core.async. Most of this library is a direct translation of core.async's Clojure code to JavaScript.

Example

The following example can be found in examples/search.js. You can try it out in the browser at this page (make sure to have your console open).

// This function simulates a search request to a remote source, such as an HTTP
// request to Google.
function fakeRemoteSearch(query) {
  var latency = (Math.random() * 200);
  return CSP.go(function*() {
    yield CSP.take(CSP.timeout(latency));
    return {query: query, latency: latency};
  });
}

function performSearch(n) {
  return CSP.go(function*() {
    // Closes after 100 ms.
    var timeout = CSP.timeout(100);
    // Provides a value after 0-200 ms.
    var search = fakeRemoteSearch('javascript ' + n);
    // Choose whatever channel provides a value or closes first.
    var result = yield CSP.alts([search, timeout]);

    if (result.chan === search) {
      return result.value;
    } else {
      return 'timeout';
    }
  });
}

// On average, half the searches will complete and half will time out.
CSP.go(function*() {
  for (var i = 0; i < 10; i++) {
    console.log(yield CSP.take(performSearch(i)));
  }
});

Usage

CSP.js requires ES6 generators, which are not supported in browsers or in Node 0.10.x or below. Firefox has built-in support only for an old version of generators which are not compatible with CSP.js.

If you want to use this library in the browser or any version of Node.js before 0.11.x, you'll have to compile your code with Regenerator and include its runtime. For example, if you want to run the search example, you use it like this:

regenerator examples/search.js > examples/build/search.js

You also have to include the Regenerator runtime on the page somewhere. The runtime file is bundled with this repository as regenerator.runtime.js and regenerator.runtime.min.js. These are merely copies of the runtime files in the Regenerator repository. CSP.js has been tested with Regenerator version 0.2.10. In the end, your script tags look something like this:

<script src="regenerator.runtime.min.js"></script>
<script src="csp.min.js"></script>
<script src="examples/search.js"></script>

Of course, if you're using this library in production, you'll want to concatenate all those into a single file.

If you want to use this library in Node 0.11.x or above, you don't need any compilation or an extra runtime, but you do need to set a command-line option:

node --harmony-generators examples/search.js

API

CSP.js is a faithful port of core.async in both API and implementation.

Documentation is still in-progress. For more details, check the unit tests and core.async API docs for now.

go(function*() { ... })

goLoop(function*() { ... })

Repeatedly executes the go block until a return statment is found. Equivalent to go(function*() { while(true) { ... } }).

yield take(channel)

yield put(channel, value)

yield alts(channels [, options])

takeAsync(channel [, onComplete [, onCaller]])

putAsync(channel, value [, onComplete [, onCaller]])

close(channel)

buffer(n)

droppingBuffer(n)

slidingBuffer(n)

timeout(duration)

pipe(from, to [, shouldClose])

mapPull(channel, fn)

mapPush(channel, fn)

map(channels, [bufOrN,] fn)

reduce(channel, init, fn)

merge(channel [, bufOrN])

intoArray(channel)

takeNum(channel, n [, bufOrN])

unique(channel [, bufOrN])

partition(channel, n [, bufOrN])

partitionBy(channel, [bufOrN,] fn)

filterPull(channel, [bufOrN,] fn)

filterPush(channel, fn)

removePull(channel, [bufOrN,] fn)

removePush(channel, fn)

mapcatPull(channel, [bufOrN,] fn)

mapcatPush(channel, [bufOrN,] fn)

split(channel, [passBufOrN, failBufOrN,] fn)

ontoChan(channel, array [, shouldClose])

toChan(array)

mult(channel)

tap(mult, channel [, shouldClose])

untap(mult, channel)

untapAll(mult, channel)

pub(channel, topicFn [, bufFn])

sub(pub, topic, channel [, shouldClose])

unsub(pub, topic, channel)

unsubAll(pub [, topic])

TODO

  • More documentation
  • More functions
    • mix, admix, unmix, unmixAll, toggle, soloMode
  • Optimize for speed and file size
  • Test for compatibility in IE

License

CSP.js is released under the MIT License.

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].