Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SyncWaterfallHook.js 1.2KB

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