Source: filter/filterChainPromiseExecutor.js

  1. /**
  2. * @author Sloan Seaman
  3. * @copyright 2016 and on
  4. * @version .1
  5. * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  6. */
  7. /** @private */
  8. var AbstractPromiseExecutor = require('../executor/abstractPromiseExecutor.js');
  9. var log = require('winston-simple').getLogger('FilterChainPromiseExecutor');
  10. /**
  11. * The FilterChainExecutor is the 'engine' of SkillVC in that it controls the execution of
  12. * all the registered filters (including intent handling which is treated as a filter).
  13. *
  14. * This works by iterating over the list of registered filters and, when a `Promise` is discovered, waits
  15. * until the completion, and then continues over the loop
  16. *
  17. * @constructor
  18. * @see {@link Filter}
  19. * @param {Filter} [preFilters] The pre filters to execute
  20. * @param {Filter} [postFilters] The post filters to execute
  21. */
  22. function FilterChainPromiseExecutor(preFilters, postFilters) {
  23. this._pre = preFilters;
  24. this._post = postFilters;
  25. }
  26. FilterChainPromiseExecutor.prototype = Object.create(AbstractPromiseExecutor.prototype);
  27. FilterChainPromiseExecutor.prototype.constructor = FilterChainPromiseExecutor;
  28. /**
  29. * Start the execution
  30. *
  31. * @function
  32. * @param {SVContext} svContext The SVContext to use for execution
  33. * @returns {Promise} The `Promise` that can be used to wait until this completes
  34. */
  35. FilterChainPromiseExecutor.prototype.execute = function(svContext) {
  36. var fcm = this; // to control scoping in the promises
  37. return new Promise(function(resolve, reject) {
  38. log.info("Starting Filter Chain Execution");
  39. fcm._doExecute('executePre', fcm._pre, svContext)
  40. .then(fcm._doExecute.bind(this, 'executePost', fcm._post, svContext)) // use bind so I don't have to wrap with function()
  41. .then(function() {
  42. log.info("Filter Chain Execution completed");
  43. resolve();
  44. })
  45. .catch(function(err) {
  46. log.error("Error in chain. "+err);
  47. reject();
  48. });
  49. });
  50. };
  51. module.exports = FilterChainPromiseExecutor;