wardboss v1.3.0
wardboss
wardboss decides who gets what when they ask for it. In other words, it does basic dependency injection and currying.
wardboss has constituents and functions. When it calls those functions, different parameters are provided to those functions depending on the constituent for which it is being called.
So, if you call a function named showJobs for the constituent bigjoerusty, it will be passed the param 'committeeman'. However, if you call showJobs for the constituent vrdolyak, wardboss passes the params 'sanitation chief', 'zoning permits'.
Let's say you have a function that takes three params, like so:
function showJobs(jobs, owedfavors, done) {
console.log(jobs, owedfavors);
setTimeout(done, 0);
}You create a wardboss.
var boss = wardboss.createBoss();After that, you can register functions with wardboss, giving providers for each constituent. A provider takes a callback, to which it passes an array of (all or some of) the params that the function should get.
boss.addFn({
fn: showJobs,
providers: {
bigjoerusty: function provideShowJobsArgs(context, done) {
setTimeout(function callDone() {
done(null, [['committeeman']]);
},
0);
},
vrdolyak: function provideShowJobsArgs(context, done) {
setTimeout(function callDone() {
done(null, [['sanitation chief'], ['zoning permits']]);
},
0);
}
}
}Then, you call the function like so. You can pass along any params that are not provided by the providers.
boss.$.vrdolyak.showJobs({
context: {
discussionLocation: 'cityHall'
},
params: [
function doneShowingJobs(error, result) {
console.log('The jobs and favors will have been logged.');
}
]
});
boss.$.bigjoerusty.showJobs({
context: {
discussionLocation: 'alley'
},
params: [
['look the other way'],
function doneShowingJobs(error, result) {
console.log('The jobs and favors will have been logged.');
}
]
});Installation
npm install wardbossSpecification
createBoss =>
- Returns a
bossobject with the following methods and properties:- addConstituent
- addFn
- fns, an object with function names as the keys and functions as the values
- $, an object that has constituent names as keys and
constituentobjects as values.- Each
constituenthas:providers, a dictionary of function name keys and provider function values.
- Each
boss.addConstituent(constituentName) =>
- Adds a
constituenttoboss.$using constituentName as the key.
boss.addFn(opts)
- Where:
- opts is an object containing:
fn, a functionproviders, an object in which:- The keys correspond to
constituentnames - The values are functions that:
- Take a
contextand a callback,done. - Calls it with
error,params, where:paramsis an array of arguments to be passed tofn.
- Take a
- The keys correspond to
- opts is an object containing:
- =>
- Adds
fntoboss.fns. - Adds values of
providerstoconstituent.providersusing 'fn' name as key. - Adds a method with the name
fn.nameto eachconstituentwith a value that is:- A function that calls a function using a parameters from the appropriate provider.
- Adds
boss.$.<constituent c>.<function f>({context, params}) =>
- Gets provider
pfromc. - Calls
pwithcontextto get arguments, which it combines withparams(params override arguments) and passes to themf.
Tests
Run tests with make test.
License
MIT.