Giter VIP home page Giter VIP logo

communist's Introduction

Communist

communist

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

API

Create a onetime use worker:

var comrade = 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:

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

Create a reusable worker:

var comrade = communist(function);//returns object
var manifesto = comrade.data(data);//returns promise
comrade.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 comrade = //returns object
comrade.data(data);//will call the callback with the result
//this is chainable
communist(function,callback).data(data1).data(data2);
//will call the callback twice, once for each result.

next up comes the fancy stuff, map reduce

var comrade = communist(threads);//returns object threads is the number of map workers, reducer will be an additional thread
comrade.data([array of data]);//can be called multiple times, the arrays will be concated
comrade.map(function);//function to be called once on each member of the array
//can be async but only call the callback once
comrade.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 comrade = communist(4);
//returns object
comrade.data([1,2,3]);
//object
comrade.map(function(x){return x*x;});
//object
comrade.reduce(function(a,b){return a+b;});
//returns promise
//the object is chainable, and data can be called more then once so....
communist(4)
	.data([1,2,3])
	.map(function(x){return x*x;})
	.data([4,5,6])
	.reduce(function(a,b){return a+b;})
	.then(function(a){console.log(a)});
//prints 91
//remember that once all three data,map and reduced are called it runs, so the following will give you an error:
communist(4)
	.data([1,2,3])
	.map(function(x){return x*x;})
	.reduce(function(a,b){return a+b;})
	.data([4,5,6])
	.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 comrade = communist(4, true);
comrade.data([1,2,3]);
comrade.map(function(x){return x*x;});
comrade.reduce(function(a,b){return a+b;});
comrade.data([4,5,6]);
comrade.fetch().then(function(a){console.log(a)});
//prints 91
comrade.data([6,7,8]).fetch().then(function(a){console.log(a)});
//prints 240
//fetch takes an argument "now", if it's undefined then 
comrade.data([6,7,8]).fetch(true).then(function(a){console.log(a)});
//also returns 240
comrade.close().then(function(a){console.log(a)});
//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. 

communist's People

Contributors

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