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

channel-light

v0.1.1

Published

light version of Channel.

Downloads

5

Readme

channel-light

go-language like Channel. CSP-style channel.

日本語

PREPARE

$ npm install channel-light --save

NPM NPM

USAGE

Channel = require('channel-light')

prepare Channel.

var Channel = require('channel-light');

channel = new Channel(callback,...)

make new channel.

var channel = Channel();

make new channel with 3 callbacks.

var channel = Channel(
	function (err, val) {},
	function (err, val) {},
	function (err, val) {}
);

callback(err, val)

callback format. this is channel itself.

function callback(err, val) {
	// use `this` as channel.
	// you can `throw` with error.
}

channel = channel(callback,...)

add callback into channel for receive values.

channel(function (err, val) {});
channel(
	function (err, val) {},
	function (err, val) {}
);

channel = channel(err, val)

send values into channel.

channel(null, 'val1');
channel(null, 'val2');
channel(new Error('error'));

normalize callback arguments.

channel('val1');           // -> callback(null, 'val1')
channel('elem1', 'elem2'); // -> callback(null, ['elem1', 'elem2'])
channel(0);                // -> callback(null, 0)
channel(false);            // -> callback(null, false)
channel(true);             // -> callback(null, true)

QUICK EXAMPLE

void function () {
	'use strict';

	// Channel
	var Channel = require('channel-light');

	// Channel() creates a new channel.
	var chan = Channel();

	// call channel with callback arguments.
	// channel returns itself, can be chained.
	// `this` is channel when callback.
	chan(function () {
		console.log('start!'); // start!
		setTimeout(this, 500, 'a');
	}, function (err, val) {
		// process a.
		console.log('a? ' + val);
		setTimeout(this, 500, 'b');
	}, function (err, val) {
		// process b.
		console.log('b? ' + val);
		setTimeout(this, 500, 'c');
	}, function (err, val) {
		// process c.
		console.log('c? ' + val);
		console.log('end');
	})();

}();
void function () {
	'use strict';

	// Channel
	var Channel = require('channel-light');

	// you don't need variable that keep a channel.
	Channel(function () {
		setTimeout(this, 3000, 'a2');
	}, function (err, val) {
		console.log('a2? ' + val);
		setTimeout(this, 500, 'b2');
	}, function (err, val) {
		console.log('b2? ' + val);
		setTimeout(this, 500, 'c2');
	}, function (err, val) {
		console.log('c2? ' + val);
		var next = this;
		setTimeout(function () {
			next('d2');
		}, 500);
	}, function (err, val) {
		console.log('d2? ' + val);
		// parallel processing. which one is first?
		var next = this, arr = [], chan2 = Channel(
			function (err, val) { console.log('e2 1st? ' + val); arr.push(val); },
			function (err, val) { console.log('e2 2nd? ' + val); arr.push(val); next(arr); });
		setTimeout(chan2, 300, 'e2X');
		setTimeout(chan2, 200, 'e2Y');
	}, function (err, arr) {
		console.log('e2 arr: ' + arr.join(', '));
		// you can get an array for multiple values.
		this('f21', 'f22', 'f23');
	}, function (err, val) {
		console.log('f2 values: ' + val.join(', '));
		// when you use Promise.
		this(new Promise(function (res, rej) {
			setTimeout(res, 500, 'g2');
		}));
	}, function (err, val) {
		console.log('g2? ' + val);
		console.log('end');
	})();

}();

EXAMPLE USING aa (async-await)

$ npm install aa channel-light

communicate with 2 threads.

	var Channel = require('channel-light');
	var aa = require('aa');

	aa(function *() {
		yield wait(100, 'a');
		var chan1 = Channel(), chan2 = Channel();
		yield [
			function *() {
				yield wait(100);
				chan2(null, 'request message'); // send message
				yield chan1; // receive message
				yield wait(100);
			},
			function *() {
				yield wait(200);
				yield chan2; // receive message
				yield wait(100);
				chan1(null, 'response message'); // send message
				yield wait(100);
			}
		];
	});
	function wait(ms, val) {
		return function (cb) { setTimeout(cb, ms, null, val); };
	}

LICENSE

MIT

SEE ALSO