depreciated, see catiline
Last updated 7 years ago by cwmma .
Original npm · Tarball · package.json
$ cnpm install communist 
SYNC missed versions from official npm registry.



A library all about workers that grew out of my work with earlier versions of this library and Parallel.js. You want Node.js and IE support (have that now) and for it not to blow up? go use Parallel.js, at the moment that's not what I'm going for here. Requires RSVP.


Create a onetime use worker:

var worker = communist(function,data);//returns promise

calls your function with the data as the first argument and a callback as the second in a worker, the function can either return a value, or call the callback, once it does that, the promise is fufiled and the worker is closed. , ie:

	return x*x;
//all together
communist(function(x){return x*x;},9).then(function(a){console.log(a)});
//prints 81.

Create a reusable worker:

var worker = communist(function);//returns object
var manifesto =;//returns promise
worker.close();//closes the worker

you can call data on multiple times and each time will return a new promise which will be fufiled based on your data, otherwise the same as the onetime worker.

If you don't feel like messing around with promises or you need the same callback called multiple times you can pass a callback function as the second argument, this gets called with the results each time.

var worker = communist(fun) = //returns object;//will call the callback with the result
//this is chainable
//will call the callback twice, once for each result.

next up comes the fancy stuff, map reduce

var worker = communist(threads);//returns object threads is the number of map workers, reducer will be an additional thread[array of data]);//can be called multiple times, the arrays will be concated;//function to be called once on each member of the array
//can be async but only call the callback once
worker.reduce(function);//reduce function of the function(a,b){return c};

this returns a chainable object until it has all it needs, then it returns a promise, e.g.

var worker = communist(4);
//returns object[1,2,3]);
//object{return x*x;});
worker.reduce(function(a,b){return a+b;});
//returns promise
//the object is chainable, and data can be called more then once so....
	.map(function(x){return x*x;})
	.reduce(function(a,b){return a+b;})
//prints 91
//remember that once all three data,map and reduced are called it runs, so the following will give you an error:
	.map(function(x){return x*x;})
	.reduce(function(a,b){return a+b;})
	.then(function(a){console.log("yay "+a)},function(a){console.log("boo "+a)});

Behind the scenes it's spinning a worker up for each thread plus one for the reducer, chrews through your data, then gives you result and cleans up.

you can also give a second argument after threads, if this is true than you have an incremental map reduce, we can keep on adding data, then we can call fetch() to get the promise, fetch waits until the data queue is empty and then invokes the primse, it takes an argument "now" that you can set to true if you don't want to wait, i.e. if data is continusly being added. it also has a close method which works like fetch but always waits, prevents you from adding more data and then cleans up the workers afterwards.

var worker = communist(4, true);[1,2,3]);{return x*x;});
worker.reduce(function(a,b){return a+b;});[4,5,6]);
//prints 91[6,7,8]).fetch().then(function(a){console.log(a)});
//prints 240
//fetch takes an argument "now", if it's undefined then[6,7,8]).fetch(true).then(function(a){console.log(a)});
//also returns 240
//returns 389

We also have communist.reducer, this is the internal function we use for the mapreduce stuff, give it two function, a reducer, and a callback, then give it .data() and it reduces it, call .fetch() to get it and call the callback and .close() which is like fetch but closes it after.

we also have communist.makeUrl(reletiveURL); returns an absolute url, and communist.worker([aray of strings]); returns worker made from those strings.

Lastly we have communist.ajax(); this is a demo function which uses the above tools (the first worker type actually) to create a function which opens up a worker, does an ajax request, can do some prosesing on it, and returns it.

var promise = communist.ajax(url,after,notjson);//returns promise obv
//after is an optional function you can add if you want to process the data in the other thread before returning it
//if notjson is true doesn't try to parse it as json which it does by default. 

If you want to use it in IE 10 then due to security issues you'll need to put the file IE.js somewhere on the same origin as your web page and call communist.IEpath = "../path/to/IE.js". Based off this work around in parallel.js

Current Tags

  • 10.0.0                                ...           latest (7 years ago)

19 Versions

  • 10.0.0                                ...           7 years ago
  • 2.3.1                                ...           7 years ago
  • 2.3.0                                ...           7 years ago
  • 2.2.1                                ...           7 years ago
  • 2.2.0                                ...           7 years ago
  • 2.1.0                                ...           7 years ago
  • 2.0.0                                ...           7 years ago
  • 1.7.4                                ...           7 years ago
  • 1.7.3                                ...           7 years ago
  • 1.7.2                                ...           7 years ago
  • 1.7.1                                ...           7 years ago
  • 1.7.0                                ...           7 years ago
  • 1.6.1                                ...           7 years ago
  • 1.6.0                                ...           7 years ago
  • 1.4.5                                ...           7 years ago
  • 1.4.4                                ...           7 years ago
  • 1.4.2                                ...           8 years ago
  • 0.1.0                                ...           8 years ago
  • 0.0.1                                ...           8 years ago
Maintainers (1)
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
Dev Dependencies (0)
Dependents (2)

Copyright 2014 - 2016 © |