MultiWatching.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. /** @import MultiCompiler from "./MultiCompiler" */
  8. /** @import Watching from "./Watching" */
  9. /** @import { ErrorCallback } from "./webpack" */
  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. /** @type {Watching[]} */
  18. this.watchings = watchings;
  19. /** @type {MultiCompiler} */
  20. this.compiler = compiler;
  21. }
  22. /**
  23. * Processes the provided error callback.
  24. * @param {ErrorCallback=} callback signals when the build has completed again
  25. * @returns {void}
  26. */
  27. invalidate(callback) {
  28. if (callback) {
  29. asyncLib.each(
  30. this.watchings,
  31. (watching, callback) => watching.invalidate(callback),
  32. (err) => {
  33. callback(/** @type {Error | null} */ (err));
  34. }
  35. );
  36. } else {
  37. for (const watching of this.watchings) {
  38. watching.invalidate();
  39. }
  40. }
  41. }
  42. suspend() {
  43. for (const watching of this.watchings) {
  44. watching.suspend();
  45. }
  46. }
  47. resume() {
  48. for (const watching of this.watchings) {
  49. watching.resume();
  50. }
  51. }
  52. /**
  53. * Processes the provided error callback.
  54. * @param {ErrorCallback} callback signals when the watcher is closed
  55. * @returns {void}
  56. */
  57. close(callback) {
  58. asyncLib.each(
  59. this.watchings,
  60. (watching, finishedCallback) => {
  61. watching.close(finishedCallback);
  62. },
  63. (err) => {
  64. if (this.compiler.watching === this) this.compiler.watching = undefined;
  65. this.compiler.hooks.watchClose.call();
  66. if (typeof callback === "function") {
  67. this.compiler.running = false;
  68. callback(/** @type {Error | null} */ (err));
  69. }
  70. }
  71. );
  72. }
  73. }
  74. module.exports = MultiWatching;