AsyncSeriesWaterfallHook.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. function COMPILE(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. }
  33. const hook = new Hook(args, name);
  34. hook.constructor = AsyncSeriesWaterfallHook;
  35. hook.compile = COMPILE;
  36. hook._call = undefined;
  37. hook.call = undefined;
  38. return hook;
  39. }
  40. AsyncSeriesWaterfallHook.prototype = null;
  41. module.exports = AsyncSeriesWaterfallHook;