AsyncSeriesWaterfallHook.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const Hook = require("./Hook");
  7. const HookCodeFactory = require("./HookCodeFactory");
  8. class AsyncSeriesWaterfallHookCodeFactory extends HookCodeFactory {
  9. content({ onError, onResult, onDone }) {
  10. return this.callTapsSeries({
  11. onError: (i, err, next, doneBreak) => onError(err) + doneBreak(true),
  12. onResult: (i, result, next) => {
  13. let code = "";
  14. code += `if(${result} !== undefined) {\n`;
  15. code += `${this._args[0]} = ${result};\n`;
  16. code += `}\n`;
  17. code += next();
  18. return code;
  19. },
  20. onDone: () => onResult(this._args[0])
  21. });
  22. }
  23. }
  24. const factory = new AsyncSeriesWaterfallHookCodeFactory();
  25. const COMPILE = function(options) {
  26. factory.setup(this, options);
  27. return factory.create(options);
  28. };
  29. function AsyncSeriesWaterfallHook(args = [], name = undefined) {
  30. if (args.length < 1)
  31. throw new Error("Waterfall hooks must have at least one argument");
  32. const hook = new Hook(args, name);
  33. hook.constructor = AsyncSeriesWaterfallHook;
  34. hook.compile = COMPILE;
  35. hook._call = undefined;
  36. hook.call = undefined;
  37. return hook;
  38. }
  39. AsyncSeriesWaterfallHook.prototype = null;
  40. module.exports = AsyncSeriesWaterfallHook;