SyncWaterfallHook.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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, onDone, 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. rethrowIfPossible
  22. });
  23. }
  24. }
  25. const factory = new SyncWaterfallHookCodeFactory();
  26. class SyncWaterfallHook extends Hook {
  27. constructor(args) {
  28. super(args);
  29. if(args.length < 1) throw new Error("Waterfall hooks must have at least one argument");
  30. }
  31. tapAsync() {
  32. throw new Error("tapAsync is not supported on a SyncWaterfallHook");
  33. }
  34. tapPromise() {
  35. throw new Error("tapPromise is not supported on a SyncWaterfallHook");
  36. }
  37. compile(options) {
  38. factory.setup(this, options);
  39. return factory.create(options);
  40. }
  41. }
  42. module.exports = SyncWaterfallHook;