webpack/lib/MultiWatching.js

78 lines
1.4 KiB
JavaScript
Raw Normal View History

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
2018-07-30 23:08:51 +08:00
"use strict";
2018-02-11 12:27:09 +08:00
const asyncLib = require("neo-async");
2018-12-10 18:34:59 +08:00
/** @typedef {import("./MultiCompiler")} MultiCompiler */
/** @typedef {import("./Watching")} Watching */
/**
* @template T
* @callback Callback
2022-01-11 19:41:14 +08:00
* @param {(Error | null)=} err
2018-12-10 18:34:59 +08:00
* @param {T=} result
*/
class MultiWatching {
2018-12-10 18:34:59 +08:00
/**
* @param {Watching[]} watchings child compilers' watchers
* @param {MultiCompiler} compiler the compiler
*/
constructor(watchings, compiler) {
this.watchings = watchings;
this.compiler = compiler;
}
invalidate(callback) {
if (callback) {
asyncLib.each(
this.watchings,
(watching, callback) => watching.invalidate(callback),
callback
);
} else {
for (const watching of this.watchings) {
watching.invalidate();
}
2018-01-22 20:52:43 +08:00
}
}
suspend() {
for (const watching of this.watchings) {
watching.suspend();
}
}
resume() {
for (const watching of this.watchings) {
watching.resume();
}
}
2018-12-10 18:34:59 +08:00
/**
* @param {Callback<void>} callback signals when the watcher is closed
* @returns {void}
*/
close(callback) {
2018-02-25 09:00:20 +08:00
asyncLib.forEach(
this.watchings,
(watching, finishedCallback) => {
watching.close(finishedCallback);
},
err => {
this.compiler.hooks.watchClose.call();
if (typeof callback === "function") {
this.compiler.running = false;
2018-02-25 09:00:20 +08:00
callback(err);
}
2018-01-22 20:52:43 +08:00
}
2018-02-25 09:00:20 +08:00
);
}
}
module.exports = MultiWatching;