npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gochan

v0.1.2

Published

golang inspired channels for doing CSP-style concurrency in node.js

Downloads

10

Readme

gochan

golang inspired channels for doing CSP-style concurrency in node.js.

This library is to give node.js without generators the ability to use golang style channels for doing concurrent programming.

build status

Installation

This module is installed via npm:

$ npm install gochan

Example Usage

Create a channel and synchronize two processes

// simple.js

// Create a new channel
var gochan = require('gochan');

// This function sends messages to the channel
function builder(ch) {
  console.log('about to build');
  ch.put(42, function () {
    console.log('after building');
  });
}

// This function waits and receives a message from the channel
function consumer(ch) {
  ch.get(function (err, value) {
    console.log('received ' + value);
  });
}

var ch = gochan();
consumer(ch);
builder(ch);

This will result in the following output:

$ node examples/simple.js
about to build
received 42
after building

Note how the order of execution is deterministic. Even though the consumer was run first, it happily waited until there was a message in the channel.

Similarly, the builder can't continue after putting something into the channel unless there is somone to read it.

Listen to multiple channels, and only proceed when there is a message

This emulates the golang "select" primitive:

// two channels
var a = gochan();
var b = gochan();

// wait until there is a message on any of the channels
gochan.select([a, b], function (err, ch) {
  // the channel ch, will be the "a" channel
});

// put something on the "a" channel
a.put(42);

API

var gochan = require('gochan');

Creates a new channel factory that can make channels.

gochan(size)

Creates a new instance of a channel.

  • size - The size of the channel. By default the size is 0, which is essentially infinite. To get golang-like behaviour where the size of a channel is only one, and thus forces channel getters and setters to synchronize around channel communication, set this value to be 1.

Eg:

var gochan = require('gochan');
var ch = gochan();

ch.put(val, cb)

Puts a raw value, a callback function, or a promise on the channel.

  • val - A raw value, a callback function (signature function(err, value)) or a promise.
  • cb - If provided, this callback will be called. If ommitted, then a promise will be returned.

NB: If the channel is a fixed size and there isn't remove for the message, then the put operation will essentially stall until there is space for the message. So the callback won't be called into there is room.

var gochan = require('gochan');
var ch = gochan();
ch.put(42, function () {
  // do something after put
});

var fs = require('fs');
ch.put(function (cb) {
    fs.readFile('/etc/passwd', { encoding: 'utf8' }, cb);
  },
  function () {
    // do something after put
  });

ch.put(42).then(function () {
  // ommit callback and get a promise
});

ch.get(cb)

Gets a value from the channel. If there is nothing in the channel, then the callback won't be called until there is a message on the channel.

  • cb - If provided, this callback will be called. If ommitted, then a promise will be returned.

NB: Even though node callback functions and values and promises can be put onto the channel, the final value that gets retrieved will be a final value.

var gochan = require('gochan');
var ch = gochan();
var fs = require('fs');
ch.put(function (cb) {
    fs.readFile('/etc/passwd', { encoding: 'utf8' }, cb);
  },
  function () {
    // do something after put
  });

ch.get(function (err, data) {
  // data will contain the contents of the file
});

gochan.select(arrChannels, cb)

Waits until there is data on an array of channels. This function attempts to emultate the golang select primitive.

  • arrChannels - An array of channels that will be monitored until there is activity on the channel. Once there is, the callback will be called.
  • cb - Called when there is data on a channel. The first argument is an error, and the second parameter is the channel with the data. You'll still need to call .get() on the channel to get the data.