MultiWatching.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. invalidate(callback) {
  25. if (callback) {
  26. asyncLib.each(
  27. this.watchings,
  28. (watching, callback) => watching.invalidate(callback),
  29. callback
  30. );
  31. } else {
  32. for (const watching of this.watchings) {
  33. watching.invalidate();
  34. }
  35. }
  36. }
  37. suspend() {
  38. for (const watching of this.watchings) {
  39. watching.suspend();
  40. }
  41. }
  42. resume() {
  43. for (const watching of this.watchings) {
  44. watching.resume();
  45. }
  46. }
  47. /**
  48. * @param {Callback<void>} callback signals when the watcher is closed
  49. * @returns {void}
  50. */
  51. close(callback) {
  52. asyncLib.forEach(
  53. this.watchings,
  54. (watching, finishedCallback) => {
  55. watching.close(finishedCallback);
  56. },
  57. err => {
  58. this.compiler.hooks.watchClose.call();
  59. if (typeof callback === "function") {
  60. this.compiler.running = false;
  61. callback(err);
  62. }
  63. }
  64. );
  65. }
  66. }
  67. module.exports = MultiWatching;