all files / lib/ Component.js

100% Statements 68/68
100% Branches 26/26
100% Functions 13/13
100% Lines 68/68
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170    110×                           201× 201× 201× 201× 201× 201×                 278×             278×             278×                   194× 187× 187×       278×               278× 269×     269× 269× 110×   269×       278×   278× 441× 441×   432× 432×         288× 269×     269×       269×   269×   597×     597× 265×       332×     332× 23×       309× 141×       269×               278× 278×   278× 278× 269×   269×   269×            
"use strict";
var Resolver = require('./Resolver');
var Resolution = require('./Resolution');
var ResolutionContext = require('./ResolutionContext');
var ResolutionError = require('./ResolutionError');
 
var ComponentResolver = new Resolver(function(ctx, res) {
  res.resolve(ctx.component());
});
 
/**
 * Captures a component registration with a {@link Container}.
 * Instances are created during {@link Container#register} calls.
 *
 * @param {String} key
 * @param {*} instance
 * @param {Container} container
 * @param {Array.<Resolver>} [containerResolvers]
 * @constructor
 * @classdesc Private to junkie internals.
 */
function Component(key, instance, container, containerResolvers) {
  this._key = key;
  this._instance = instance;
  this._container = container;
  this._containerResolvers = containerResolvers || [];
  this._resolvers = [];
  this._store = {};
}
 
/** @lends Component# */
var C = Component.prototype;
 
/**
 * Obtain the component key.
 * @returns {String}
 */
C.key = function() {
  return this._key;
};
 
/**
 * Obtain the user-provided component instance.
 * @returns {*}
 */
C.instance = function() {
  return this._instance;
};
 
/**
 * Obtain the data store for this component.
 * @returns {Object}
 */
C.store = function() {
  return this._store;
};
 
/**
 * Use the given resolver middleware.
 * @param {String|Function} resolver The resolver to use. Supplying a String attempts to locate a standard resolver
 *        by name. Supplying a Function uses the Function itself as the resolver implementation.
 * @see Resolver
 * @returns {Component} <code>this</code>.
 */
C.use = function(resolver, args) {
  resolver = Resolver.normalize(resolver, args);
  this._resolvers.push(resolver);
  return this;
};
 
 
C._createContext = function(options) {
  var ctx = new ResolutionContext({
    previous: options.resolutionContext,
    container: this._container,
    key: this.key(),
    component: this.instance(),
    store: this.store()
  });
 
  this._checkCircularDeps(ctx);
  return ctx;
};
 
C._resolverChain = function() {
  var resolvers = this._containerResolvers.concat(this._resolvers);
  if (this._resolvers.length === 0) {
    resolvers.push(ComponentResolver);
  }
  return resolvers;
};
 
C._checkCircularDeps = function(ctx) {
  // Check for circular dependencies
  var seenKeys = {},
      key;
  do {
    key = ctx.key();
    if (seenKeys[key]) {
      throw new ResolutionError("Circular dependency: " + key);
    }
    seenKeys[key] = true;
    ctx = ctx.previous();
  } while (ctx);
};
 
C._commitResolution = function(res, options) {
  // A commit may be attempted twice if the resolver fails the resolution and calls next
  if (!res._committed) {
    if (!options.optional && !res.failed() && !res.resolved()) {
      res.fail(new ResolutionError("Resolver chain failed to resolve a component instance"));
    }
 
    res._commit();
  }
};
 
C._callResolverChain = function(resolvers, res, ctx, options) {
  var i = 0;
 
  var next = function() {
    // Grab the next resolver
    var r = resolvers[i++];
 
    // No resolver? -> commit and quit
    if (!r || res.failed() || res.isDone()) {
      return this._commitResolution(res, options);
    }
 
    // Execute the resolver
    r.resolve(ctx, res, next);
 
    // Failed or finished? -> commit and quit
    if (res.failed() || res.isDone()) {
      return this._commitResolution(res, options);
    }
 
    // Resolver didn't call next? -> Do it for 'em
    if (!r.acceptsNextArg()) {
      next();
    }
  }.bind(this);
 
  next();
};
 
/**
 * Resolve an instance for this component.
 * @param {Object} options The optional resolution options.
 * @returns {Promise}
 */
C.resolve = function(options) {
  try {
    options = options || {};
 
    var res = new Resolution();
    var ctx = this._createContext(options);
    var resolvers = this._resolverChain();
 
    this._callResolverChain(resolvers, res, ctx, options);
 
    return res.committed();
 
  } catch (e) {
    return Promise.reject(e);
  }
};
 
module.exports = Component;