GithubHelp home page GithubHelp logo

jschannels's Introduction

JsChannels

Build Status

A minimal JavaScript Channels library (1.3k minified). Inspired by Clojure's core.async library.

Documentation | Unit Tests | Examples

A Channel provides the following methods

  • write(value1, value2, ...) - Write the values[s] to the Channel
  • read(callback) - Assign a reader to the Channel
  • unread(callback) - Remove a reader from the Channel
  • block() - Block the Channel
  • unblock() - Unblock the Channel

The Channel class provides two static utility methods:

  • Channel.alts(callback, Channel, Channel, ...) - Associated a callback with many channels. Callbacks passed to alts will receive the Channel object as the first argument, followed by the arguments passed to Channel.write()
  • Channel.select(callback, Channel, Channel, ...) - Associated a callback with many channels, for which the reader will execute on the first Channel to be wrtten to, and no others. Callbacks passed to select will receive the Channel object as the first argument, followed by the arguments passed to Channel.write()

When a channel is written to, all associated reader callbacks will be invoked, in order of registration, with the values written to the channel.

If a no readers are available at the time of a write, the write (and subsequent writes) will be queued on the Channel until the first reader is registered.

Reader callbacks are executed with the Channel object as the 'this' context

Example Usage:

// Simple read/write
var c1 = new Channel();
c1.read(function (val) { console.log("Read from Channel1: " + val); });
c1.write(1);


// Simple read/write, with extra data
var c2 = new Channel();
c2.read(function (v1, v2, v3, v4, v5) {
    console.log("Read from Channel2: " + arguments);
});
c2.write(1, 2, 3, 4, 5);


// If no readers are available, the write will wait in the channel until a
// reader comes along
var c3 = new Channel();
function read(val) {
    console.log("Read from Channel3: " + val);
}
c3.write(1);
setTimeout(function() { c3.read(read); }, 1000);


// Many readers will execute in order
var c4 = new Channel();
c4.read(function (val) { console.log("Read from Channel4: " + val); });
c4.read(function (val) { console.log("Read again from Channel4: " + val); });
c4.write(1);


// Many writers will execute in order
var c5 = new Channel();
c5.read(function (val) { console.log("Read from Channel5: " + val); });
c5.write(1);
c5.write(2);
c5.write(3);


// Readers can block the channel if needed
var c6 = new Channel();
c6.read(function (val) {
    console.log("Blocking reading on this channel6 for 1s");
    this.block();
    setTimeout(this.unblock, 1000);
});
c6.read(function (val) {
    console.log("The last reader on channel 6 unblocked, my turn!");
});
c6.write(1);

// Writers can block the channel if needed
var c7 = new Channel();
c7.read(function (val) { console.log("Read from Channel7: " + val); });
c7.write(1);
c7.block();
c7.write(2);
setTimeout(c7.unblock, 1000);

githalytics.com alpha

jschannels's People

Contributors

brophdawg11 avatar johnmichel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.