MultiWatching.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /** @typedef {import("./webpack").ErrorCallback} ErrorCallback */
  10. class MultiWatching {
  11. /**
  12. * Creates an instance of MultiWatching.
  13. * @param {Watching[]} watchings child compilers' watchers
  14. * @param {MultiCompiler} compiler the compiler
  15. */
  16. constructor(watchings, compiler) {
  17. this.watchings = watchings;
  18. this.compiler = compiler;
  19. }
  20. /**
  21. * Processes the provided error callback.
  22. * @param {ErrorCallback=} callback signals when the build has completed again
  23. * @returns {void}
  24. */
  25. invalidate(callback) {
  26. if (callback) {
  27. asyncLib.each(
  28. this.watchings,
  29. (watching, callback) => watching.invalidate(callback),
  30. (err) => {
  31. callback(/** @type {Error | null} */ (err));
  32. }
  33. );
  34. } else {
  35. for (const watching of this.watchings) {
  36. watching.invalidate();
  37. }
  38. }
  39. }
  40. suspend() {
  41. for (const watching of this.watchings) {
  42. watching.suspend();
  43. }
  44. }
  45. resume() {
  46. for (const watching of this.watchings) {
  47. watching.resume();
  48. }
  49. }
  50. /**
  51. * Processes the provided error callback.
  52. * @param {ErrorCallback} callback signals when the watcher is closed
  53. * @returns {void}
  54. */
  55. close(callback) {
  56. asyncLib.each(
  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(/** @type {Error | null} */ (err));
  66. }
  67. }
  68. );
  69. }
  70. }
  71. module.exports = MultiWatching;