all files / lib/resolver/ caching.js

100% Statements 8/8
100% Branches 2/2
100% Functions 2/2
100% Lines 8/8
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                                                 
"use strict";
 
var cacheKey = 'caching_instance';
 
/**
 * Search the {@link Component#store} for a cached instance of a previously resolved component. If one
 * is found, it is resolved and the resolution is marked as done. Otherwise, the next resolvers are called,
 * and when they complete, the resulting resolved instance is cached such that subsequent resolves will
 * return this instance.
 *
 * @name caching
 * @function
 * @memberOf Resolver:caching#
 */
 
/**
 * Caches resolved instances so that subsequent resolutions immediately return the cached instance.
 *
 * @function
 * @exports Resolver:caching
 */
module.exports = function caching(ctx, res) {
  var cached = ctx.store(cacheKey);
  if (cached) {
    res.resolve(cached);
    return res.done();
  }
 
  res.once('committed', function() {
    ctx.store(cacheKey, res.instance());
  });
};