SyncWaterfallHook.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 SyncWaterfallHookCodeFactory extends HookCodeFactory {
  9. content({ onError, onResult, resultReturns, rethrowIfPossible }) {
  10. return this.callTapsSeries({
  11. onError: (i, err) => onError(err),
  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. doneReturns: resultReturns,
  22. rethrowIfPossible
  23. });
  24. }
  25. }
  26. const factory = new SyncWaterfallHookCodeFactory();
  27. const TAP_ASYNC = () => {
  28. throw new Error("tapAsync is not supported on a SyncWaterfallHook");
  29. };
  30. const TAP_PROMISE = () => {
  31. throw new Error("tapPromise is not supported on a SyncWaterfallHook");
  32. };
  33. const COMPILE = function(options) {
  34. factory.setup(this, options);
  35. return factory.create(options);
  36. };
  37. function SyncWaterfallHook(args = [], name = undefined) {
  38. if (args.length < 1)
  39. throw new Error("Waterfall hooks must have at least one argument");
  40. const hook = new Hook(args, name);
  41. hook.constructor = SyncWaterfallHook;
  42. hook.tapAsync = TAP_ASYNC;
  43. hook.tapPromise = TAP_PROMISE;
  44. hook.compile = COMPILE;
  45. return hook;
  46. }
  47. SyncWaterfallHook.prototype = null;
  48. module.exports = SyncWaterfallHook;