Module: procnet

Methods

(static) client(factories, config)

A client is nothing more than a consumer. A good example would be a server with a REST api trying to call remote services using procnet. It could of course also be a browser client trying to fetch data from remote servers.
Parameters:
Name Type Description
factories object Contains all of the instanciators for the service types.
config object Has the configuration for each of the services.
Source:
See:
  • loader

(static) dependencies(serviceFactory)

Returns the dependencies that need to be resoled and passed to the factory to create a service instance.
Parameters:
Name Type Description
serviceFactory object The factory which is used to create the service;
Source:
Returns:
array

(static) injectable()

Source:

(static) loader(factories, config)

Asynchronous dependency loader for setting up services. This will load up the service and return the precedures object that the service generates.
Parameters:
Name Type Description
factories object Is a map of service names which translate to a function accepting an option and a callback.
config object The configuration is used to store data which could be server-specific as well as other things such as ip addresses. Each factory is given the corresponding configuration needed to create the instance it is being asked to generate.
Source:
Returns:
function

(static) mockCluster(promise, leafs, branches)

This allows you to more or less do integration testing while still avoiding to make network calls.

var leafs = {
  postgres: function() {},
  math: procnet.mockRemote(math)
};

var recangle = procnet.service(['math'], function(math) {
  return {
    surface: function(w, h) {
      return math.multiply(w, w);
    }
  };
});

var services = procnet.mockCluster(leafs, { rectangle: rectangle });

services.rectangle.surface(10, 2).then(function(res) {
  assert.equal(res, 20);
});
Parameters:
Name Type Description
promise function Is yet again, a promise factory.
leafs object These are already loaded dependencies. For example, if you still want to have a real database connection, you can inject it into the cluster by adding its name and reference to the object.
branches object Are services which need to be mocked.
Source:
Returns:
object

(static) mocker(promise, mocks, service)

To unit test, one will need to use mocking due to the services having external dependencies. Since procnet is so simple, you can still call directly the procedures once the service is instantianted.
var rectangle = procnet.service(['math'], function(math) {
  return {
    surface: function(a, b) {
      return math.multiply(a, b);
    }
  };
});

var mock = procnet.mocker(promiseFactory);

var mocked = mock({
  math: {
    multiply: function(a, b) { return a + b; }
  }
}, rectangle);

mocked
  .surface(2, 5)
  .then(function(r) {
    assert.equal(r, 7);
  });
Parameters:
Name Type Description
promise function is the promise factory
mocks object is an object where the key is the name of the service.
service function is the function returning an object will all of the procedures.
Source:
Returns:
function

(static) mockFn(promise, fn)

Utility function for mocking service procedures for unit testing. It only makes the function always returna promise, which generated services don't always do.
Parameters:
Name Type Description
promise function Is a promise factory.
fn function Is the function to mock.
Source:
Returns:
function

(static) mockRemote(promise, mock)

Takes in a object with functions and ensures that they always return a promise. Useful for injecting them into services as fake networked remotes.
Parameters:
Name Type Description
promise function is a promise factory.
mock object is the set of functions to mock. Can also contain objects, where the functons inside of that object will be mocked.
Source:
Returns:
object

(static) resolve(toResolve) → {object|promise}

Similar to loader, but instead is synchronous. More useful if you're just using this library for dependency injection. Its really just synchronous by default instead of forcing all functions to return a promise.

var math = procnet.injectable(function() {
  return {
    add: function(a, b) { return a + b; },
    double: function(a) { return a * 2; }
  };
});

var rectangle = procnet.injectable(['math'], function(math) {
  return {
    perimeter: function(h, w) {
      return math.add(math.double(h), math.double(w));
    }
  };
});

var services = {
  'rectangle': rectangle,
  'math': math
};

var loaded = procnet.resolve(services);

console.log(loaded.perimeter(1, 1)); // => 4
This function also supports async dependency loading, where an injectable returns a promise instead of a instance. In this case the function will return a promise. This is useful for injectables which need to load data from databases and such.
var numberCache = procnet.injectable(function() {
  return Promise.resolve({
    1: 'One',
    2: 'Two',
    3: 'Three'
  });
});
var add = procnet.injectable(function(numberCache) {
  return function(x, y) { return x + y; };
});

var loadedAsync = procnet.resolve({
  numberCache: numberCache,
  add: add
});
loadedAsync.then(function(loaded) {
  loaded.add(1, 2); // => 'Three'
});
Parameters:
Name Type Description
toResolve object Is the list of services that are either already loaded or need to be loaded.
Source:
Returns:
All the services loaded. If some of the service instances are returned as promises then the object returned will also be a promise.
Type
object | promise

(static) rollingFn(promise, values, ln)

Function which will return each value specified in the array in sequence for each call. Each value will be promisified.
var values = [1, 2];

var roller = procnet.rollingFn(promise, values);

roller('foobar?'); // -> returns a resolved promise with the value 1.
roller('foobaz!'); // -> returns a resolved promise with the value 2.
roller('fooboo');  // -> returns a resolved promise with the value undefined.

This is useful for mocking functions such as database access functions.
Parameters:
Name Type Description
promise function Is a promise factory.
values array Are the values the function will return on each subsequent call.
ln number Is the length property of the function. This parameter is optional.
Source:

(static) service(dependencies, factory)

More like syntatic sugar for defining services. You can list your dependencies array to query them later.
var foo = procnet.service(['bar'], function(bar) {
  return {
    foobar: function() { return 'foo' + bar; }
  };
});
procnet.dependencies(foo); // => ['bar']
var instance = foo('bar');
instance.foobar(); // => 'foobar'
You also have the option of omitting the array and listing your dependencies directly just like with angular modules. It will infer what you mean automatically.
var rectangle = procnet.injectable(function(math) {
  return {
    perimeter: function(h, w) {
      return math.add(math.double(h), math.double(w));
    }
  };
});
procnet.dependencies(rectangle); // => ['math']
Parameters:
Name Type Description
dependencies array Is an array of strings where each string is the name of the service that the service requires to run.
factory function Is the funciton which returns the list of procedures.
Source:
Returns:
function