1.0.3 • Published 8 years ago

missionworks v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
8 years ago

missionWorks.js

missionWorks.js is a light-weighted open-source nodejs module which provides some asynchronous programming features.

Features

  • Easy to create asynchronous programs.
  • Dynamic mission creation.
  • Automatically deal with dependencies (just like async.auto in Async.js).
  • Control the maxium missions running at the sametime.
  • Really light-weighted, less than 100 lines.

Install

npm install missionworks

Usage

Create a mission

var mission = require('missionworks').assign(name, func, callback, depend);

name is the name of this mission. The only usage of name is you can use mission.name to get the label in order to help yourself. It is prefectly fine to put a object or something else as the name. It is also no problem to have exactly same names. Name do not influence how missionWork works at all.

func is the function that will be called when the mission is ready to go. It will be called with a callback function. The func should call this callback with any parameter you want.

callback is the function that will be called when the callback in func is called. It is useful to use with other library such as Async.js.

depend is a mission or an array of missions that must be done before this mission is called.

This method will return a mission object which you can add to new mission's depend list.

Change maxium count of missions running at the same time

require('missionworks').setMaxParallel(limit);

limit is the count of maxium missions can run at the same time.

This method will return missionWorks self so you can do chaining stuff.

Define mission start or end functions

require('missionworks').setStartFunction(func); require('missionworks').setEndFunction(func);

When a mission is started or ended, the func you indicated will be called with the mission object.

Example

'use strict';

var mw = require('./missionWorks.js');
mw.setStartFunction(function(mission) {
	console.info(`Mission ${mission.name} started.`);
});
mw.setEndFunction(function(mission) {
	console.info(`Mission ${mission.name} completed.`);
});

//This step will create a mission named "SampleMission". In fact, the name for a mission
//does not influence anything except the "name" property of a mission instance.
var sample = mw.assign('SampleMission', function(callback) {
	console.info('Wow, the sample mission started! It will take 3 seconds to finish.');
	setTimeout(callback, 3000);
}, function() {
	console.info('Callback for the sample mission has been called.');
});

console.info(sample.name); //SampleMission

var waitGuy = mw.assign('WaitGuy', function(callback) {
	console.info('I will start after SampleMission is completed.');
	setTimeout(callback, 1000);
}, null, sample);

var missionList = [];

mw.setMaxParallel(3);
for (let i = 0; i < 8; i++) {
	missionList.push(mw.assign(`Mission-${i}`, function(callback) {
		setTimeout(callback, i * 1000); //The later the mission is, the longer it take.
	}, null, waitGuy));
}

var waitALOTGuy = mw.assign('waitALOTGuy', function(callback) {
	console.info('I will start after all mission list completed.');
	setTimeout(callback, 1000);
}, null, missionList);

License

MIT