MultiWatching.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const asyncLib = require("neo-async");
  7. /** @typedef {import("./MultiCompiler")} MultiCompiler */
  8. /** @typedef {import("./Watching")} Watching */
  9. /**
  10. * @template T
  11. * @callback Callback
  12. * @param {(Error | null)=} err
  13. * @param {T=} result
  14. */
  15. class MultiWatching {
  16. /**
  17. * @param {Watching[]} watchings child compilers' watchers
  18. * @param {MultiCompiler} compiler the compiler
  19. */
  20. constructor(watchings, compiler) {
  21. this.watchings = watchings;
  22. this.compiler = compiler;
  23. }
  24. /**
  25. * @param {Callback<void>=} callback signals when the build has completed again
  26. * @returns {void}
  27. */
  28. invalidate(callback) {
  29. if (callback) {
  30. asyncLib.each(
  31. this.watchings,
  32. (watching, callback) => watching.invalidate(callback),
  33. callback
  34. );
  35. } else {
  36. for (const watching of this.watchings) {
  37. watching.invalidate();
  38. }
  39. }
  40. }
  41. suspend() {
  42. for (const watching of this.watchings) {
  43. watching.suspend();
  44. }
  45. }
  46. resume() {
  47. for (const watching of this.watchings) {
  48. watching.resume();
  49. }
  50. }
  51. /**
  52. * @param {Callback<void>} callback signals when the watcher is closed
  53. * @returns {void}
  54. */
  55. close(callback) {
  56. asyncLib.forEach(
  57. this.watchings,
  58. (watching, finishedCallback) => {
  59. watching.close(finishedCallback);
  60. },
  61. err => {
  62. this.compiler.hooks.watchClose.call();
  63. if (typeof callback === "function") {
  64. this.compiler.running = false;
  65. callback(err);
  66. }
  67. }
  68. );
  69. }
  70. }
  71. module.exports = MultiWatching;