all files / lib/resolver/ method.js

100% Statements 24/24
100% Branches 10/10
100% Functions 6/6
100% Lines 24/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67    14×       10×                                                 18× 17× 17× 17×         16× 16×     16× 16×     16×   15× 14×          
"use strict";
var util = require('../util');
var ResolutionError = require('../ResolutionError');
 
function handleResult(result, opts, res, next) {
  if (result && util.isType(result, 'promise') && opts && opts.await) {
    result.then(function() {
      next();
    }).catch(function(err) {
      res.fail(err);
      next();
    });
  } else {
    next();
  }
}
 
/**
 * Invoke the specified method as part of resolving the instance on which the method is called, optionally configuring
 * dependencies to be passed as method parameters.
 *
 * @name method
 * @param {string} methodName The name of the method to invoke.
 * @param {...string} [dependencyKey] A dependency key to resolve and pass as an argument to the method.
 * @param {object} [options] An options object.
 * @param {boolean} [options.await] When <code>true</code>, and a <code>Promise</code> is returned from the specified
 *        method, the resolution of the instance on which the method is being called will not complete until the
 *        <code>Promise</code> resolves or fails.
 * @function
 * @memberOf Resolver:method#
 */
 
/**
 * Injects dependencies by calling a method on the component instance.
 *
 * @function
 * @exports Resolver:method
 */
module.exports = function method(ctx, res, next) {
  var instance = res.instance(true);
  var targetMethod = this.arg(0, "Method resolver: must supply target method name");
  var m = instance[targetMethod];
  util.assert.type(m,
    'function',
    "Method resolver: Method not found: " + targetMethod,
    ResolutionError);
 
  var deps = this.args();
  deps.shift(); // Remove targetField
 
  // Extract optional options
  var opts = deps[deps.length - 1];
  if (opts && typeof opts === "object") {
    deps.pop();
  }
 
  ctx.resolve(deps)
    .then(function(resolvedDeps) {
      var result = m.apply(instance, resolvedDeps.list);
      handleResult(result, opts, res, next);
    })
    .catch(function(err) {
      res.fail(err);
      next();
    });
};