| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 17:06:45 +08:00
										 |  |  | const { AsyncSeriesHook, SyncHook } = require("tapable"); | 
					
						
							| 
									
										
										
										
											2021-04-12 15:09:02 +08:00
										 |  |  | const { makeWebpackError } = require("../HookWebpackError"); | 
					
						
							|  |  |  | const WebpackError = require("../WebpackError"); | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | const ArrayQueue = require("./ArrayQueue"); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | const QUEUED_STATE = 0; | 
					
						
							|  |  |  | const PROCESSING_STATE = 1; | 
					
						
							|  |  |  | const DONE_STATE = 2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-12 19:21:47 +08:00
										 |  |  | let inHandleResult = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-10 18:34:59 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							| 
									
										
										
										
											2020-04-23 16:48:36 +08:00
										 |  |  |  * @callback Callback | 
					
						
							| 
									
										
										
										
											2022-01-11 19:41:14 +08:00
										 |  |  |  * @param {(WebpackError | null)=} err | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  |  * @param {(T | null)=} result | 
					
						
							| 
									
										
										
										
											2025-08-22 06:40:41 +08:00
										 |  |  |  * @returns {void} | 
					
						
							| 
									
										
										
										
											2018-12-10 18:34:59 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  * @template K | 
					
						
							|  |  |  |  * @template R | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AsyncQueueEntry { | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {T} item the item | 
					
						
							|  |  |  | 	 * @param {Callback<R>} callback the callback | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(item, callback) { | 
					
						
							|  |  |  | 		this.item = item; | 
					
						
							|  |  |  | 		/** @type {typeof QUEUED_STATE | typeof PROCESSING_STATE | typeof DONE_STATE} */ | 
					
						
							|  |  |  | 		this.state = QUEUED_STATE; | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		/** @type {Callback<R> | undefined} */ | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 		this.callback = callback; | 
					
						
							|  |  |  | 		/** @type {Callback<R>[] | undefined} */ | 
					
						
							|  |  |  | 		this.callbacks = undefined; | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		/** @type {R | null | undefined} */ | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 		this.result = undefined; | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 		/** @type {WebpackError | null | undefined} */ | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 		this.error = undefined; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T, K | 
					
						
							| 
									
										
										
										
											2025-03-12 09:56:14 +08:00
										 |  |  |  * @typedef {(item: T) => K} getKey | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @template T, R | 
					
						
							| 
									
										
										
										
											2025-03-12 09:56:14 +08:00
										 |  |  |  * @typedef {(item: T, callback: Callback<R>) => void} Processor | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							| 
									
										
										
										
											2018-09-26 16:27:09 +08:00
										 |  |  |  * @template K | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  |  * @template R | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class AsyncQueue { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2024-06-11 21:09:50 +08:00
										 |  |  | 	 * @param {object} options options object | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	 * @param {string=} options.name name of the queue | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 	 * @param {number=} options.parallelism how many items should be processed at once | 
					
						
							| 
									
										
										
										
											2024-09-27 00:31:30 +08:00
										 |  |  | 	 * @param {string=} options.context context of execution | 
					
						
							| 
									
										
										
										
											2025-04-16 22:04:11 +08:00
										 |  |  | 	 * @param {AsyncQueue<EXPECTED_ANY, EXPECTED_ANY, EXPECTED_ANY>=} options.parent parent queue, which will have priority over this queue and with shared parallelism | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 	 * @param {getKey<T, K>=} options.getKey extract key from item | 
					
						
							|  |  |  | 	 * @param {Processor<T, R>} options.processor async function to process items | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2024-09-27 00:31:30 +08:00
										 |  |  | 	constructor({ name, context, parallelism, parent, processor, getKey }) { | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 		this._name = name; | 
					
						
							| 
									
										
										
										
											2024-09-27 00:31:30 +08:00
										 |  |  | 		this._context = context || "normal"; | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 		this._parallelism = parallelism || 1; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 		this._processor = processor; | 
					
						
							| 
									
										
										
										
											2018-09-26 16:27:09 +08:00
										 |  |  | 		this._getKey = | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 			getKey || | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 			/** @type {getKey<T, K>} */ ((item) => /** @type {T & K} */ (item)); | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 		/** @type {Map<K, AsyncQueueEntry<T, K, R>>} */ | 
					
						
							|  |  |  | 		this._entries = new Map(); | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 		/** @type {ArrayQueue<AsyncQueueEntry<T, K, R>>} */ | 
					
						
							|  |  |  | 		this._queued = new ArrayQueue(); | 
					
						
							| 
									
										
										
										
											2025-03-27 08:07:25 +08:00
										 |  |  | 		/** @type {AsyncQueue<T, K, R>[] | undefined} */ | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 		this._children = undefined; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 		this._activeTasks = 0; | 
					
						
							|  |  |  | 		this._willEnsureProcessing = false; | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 		this._needProcessing = false; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 		this._stopped = false; | 
					
						
							| 
									
										
										
										
											2025-03-27 08:07:25 +08:00
										 |  |  | 		/** @type {AsyncQueue<T, K, R>} */ | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 		this._root = parent ? parent._root : this; | 
					
						
							|  |  |  | 		if (parent) { | 
					
						
							|  |  |  | 			if (this._root._children === undefined) { | 
					
						
							|  |  |  | 				this._root._children = [this]; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				this._root._children.push(this); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		this.hooks = { | 
					
						
							| 
									
										
										
										
											2018-12-18 05:05:08 +08:00
										 |  |  | 			/** @type {AsyncSeriesHook<[T]>} */ | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			beforeAdd: new AsyncSeriesHook(["item"]), | 
					
						
							| 
									
										
										
										
											2018-12-18 05:05:08 +08:00
										 |  |  | 			/** @type {SyncHook<[T]>} */ | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			added: new SyncHook(["item"]), | 
					
						
							| 
									
										
										
										
											2018-12-18 05:05:08 +08:00
										 |  |  | 			/** @type {AsyncSeriesHook<[T]>} */ | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			beforeStart: new AsyncSeriesHook(["item"]), | 
					
						
							| 
									
										
										
										
											2018-12-18 05:05:08 +08:00
										 |  |  | 			/** @type {SyncHook<[T]>} */ | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			started: new SyncHook(["item"]), | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 			/** @type {SyncHook<[T, WebpackError | null | undefined, R | null | undefined]>} */ | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			result: new SyncHook(["item", "error", "result"]) | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		this._ensureProcessing = this._ensureProcessing.bind(this); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-09-27 00:31:30 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {string} context of execution | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	getContext() { | 
					
						
							|  |  |  | 		return this._context; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} value context of execution | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	setContext(value) { | 
					
						
							|  |  |  | 		this._context = value; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2021-04-09 21:50:25 +08:00
										 |  |  | 	 * @param {T} item an item | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	 * @param {Callback<R>} callback callback function | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	add(item, callback) { | 
					
						
							| 
									
										
										
										
											2021-04-12 15:09:02 +08:00
										 |  |  | 		if (this._stopped) return callback(new WebpackError("Queue was stopped")); | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		this.hooks.beforeAdd.callAsync(item, (err) => { | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2021-04-12 15:09:02 +08:00
										 |  |  | 				callback( | 
					
						
							|  |  |  | 					makeWebpackError(err, `AsyncQueue(${this._name}).hooks.beforeAdd`) | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-09-26 16:27:09 +08:00
										 |  |  | 			const key = this._getKey(item); | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 			const entry = this._entries.get(key); | 
					
						
							|  |  |  | 			if (entry !== undefined) { | 
					
						
							|  |  |  | 				if (entry.state === DONE_STATE) { | 
					
						
							| 
									
										
										
										
											2021-04-27 07:22:32 +08:00
										 |  |  | 					if (inHandleResult++ > 3) { | 
					
						
							|  |  |  | 						process.nextTick(() => callback(entry.error, entry.result)); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						callback(entry.error, entry.result); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					inHandleResult--; | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 				} else if (entry.callbacks === undefined) { | 
					
						
							|  |  |  | 					entry.callbacks = [callback]; | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					entry.callbacks.push(callback); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 			const newEntry = new AsyncQueueEntry(item, callback); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			if (this._stopped) { | 
					
						
							|  |  |  | 				this.hooks.added.call(item); | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 				this._root._activeTasks++; | 
					
						
							| 
									
										
										
										
											2019-11-12 19:21:47 +08:00
										 |  |  | 				process.nextTick(() => | 
					
						
							| 
									
										
										
										
											2021-04-12 15:09:02 +08:00
										 |  |  | 					this._handleResult(newEntry, new WebpackError("Queue was stopped")) | 
					
						
							| 
									
										
										
										
											2019-11-12 19:21:47 +08:00
										 |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 				this._entries.set(key, newEntry); | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 				this._queued.enqueue(newEntry); | 
					
						
							|  |  |  | 				const root = this._root; | 
					
						
							|  |  |  | 				root._needProcessing = true; | 
					
						
							|  |  |  | 				if (root._willEnsureProcessing === false) { | 
					
						
							|  |  |  | 					root._willEnsureProcessing = true; | 
					
						
							|  |  |  | 					setImmediate(root._ensureProcessing); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				this.hooks.added.call(item); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2021-04-09 21:50:25 +08:00
										 |  |  | 	 * @param {T} item an item | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	invalidate(item) { | 
					
						
							| 
									
										
										
										
											2018-09-26 16:27:09 +08:00
										 |  |  | 		const key = this._getKey(item); | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +08:00
										 |  |  | 		const entry = | 
					
						
							|  |  |  | 			/** @type {AsyncQueueEntry<T, K, R>} */ | 
					
						
							|  |  |  | 			(this._entries.get(key)); | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 		this._entries.delete(key); | 
					
						
							| 
									
										
										
										
											2019-01-05 17:50:11 +08:00
										 |  |  | 		if (entry.state === QUEUED_STATE) { | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 			this._queued.delete(entry); | 
					
						
							| 
									
										
										
										
											2019-01-05 17:50:11 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-09 21:50:25 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * Waits for an already started item | 
					
						
							|  |  |  | 	 * @param {T} item an item | 
					
						
							|  |  |  | 	 * @param {Callback<R>} callback callback function | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	waitFor(item, callback) { | 
					
						
							|  |  |  | 		const key = this._getKey(item); | 
					
						
							|  |  |  | 		const entry = this._entries.get(key); | 
					
						
							|  |  |  | 		if (entry === undefined) { | 
					
						
							|  |  |  | 			return callback( | 
					
						
							| 
									
										
										
										
											2021-04-12 15:09:02 +08:00
										 |  |  | 				new WebpackError( | 
					
						
							|  |  |  | 					"waitFor can only be called for an already started item" | 
					
						
							|  |  |  | 				) | 
					
						
							| 
									
										
										
										
											2021-04-09 21:50:25 +08:00
										 |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (entry.state === DONE_STATE) { | 
					
						
							|  |  |  | 			process.nextTick(() => callback(entry.error, entry.result)); | 
					
						
							|  |  |  | 		} else if (entry.callbacks === undefined) { | 
					
						
							|  |  |  | 			entry.callbacks = [callback]; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			entry.callbacks.push(callback); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	stop() { | 
					
						
							|  |  |  | 		this._stopped = true; | 
					
						
							| 
									
										
										
										
											2019-01-04 21:22:22 +08:00
										 |  |  | 		const queue = this._queued; | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 		this._queued = new ArrayQueue(); | 
					
						
							|  |  |  | 		const root = this._root; | 
					
						
							| 
									
										
										
										
											2019-01-04 21:22:22 +08:00
										 |  |  | 		for (const entry of queue) { | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 			this._entries.delete( | 
					
						
							|  |  |  | 				this._getKey(/** @type {AsyncQueueEntry<T, K, R>} */ (entry).item) | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 			root._activeTasks++; | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 			this._handleResult( | 
					
						
							|  |  |  | 				/** @type {AsyncQueueEntry<T, K, R>} */ (entry), | 
					
						
							|  |  |  | 				new WebpackError("Queue was stopped") | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	increaseParallelism() { | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 		const root = this._root; | 
					
						
							|  |  |  | 		root._parallelism++; | 
					
						
							| 
									
										
										
										
											2020-07-29 17:14:26 +08:00
										 |  |  | 		/* istanbul ignore next */ | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 		if (root._willEnsureProcessing === false && root._needProcessing) { | 
					
						
							|  |  |  | 			root._willEnsureProcessing = true; | 
					
						
							|  |  |  | 			setImmediate(root._ensureProcessing); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	decreaseParallelism() { | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 		const root = this._root; | 
					
						
							|  |  |  | 		root._parallelism--; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {T} item an item | 
					
						
							|  |  |  | 	 * @returns {boolean} true, if the item is currently being processed | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	isProcessing(item) { | 
					
						
							| 
									
										
										
										
											2018-09-26 16:27:09 +08:00
										 |  |  | 		const key = this._getKey(item); | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 		const entry = this._entries.get(key); | 
					
						
							|  |  |  | 		return entry !== undefined && entry.state === PROCESSING_STATE; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {T} item an item | 
					
						
							|  |  |  | 	 * @returns {boolean} true, if the item is currently queued | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	isQueued(item) { | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 		const key = this._getKey(item); | 
					
						
							|  |  |  | 		const entry = this._entries.get(key); | 
					
						
							|  |  |  | 		return entry !== undefined && entry.state === QUEUED_STATE; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {T} item an item | 
					
						
							|  |  |  | 	 * @returns {boolean} true, if the item is currently queued | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	isDone(item) { | 
					
						
							| 
									
										
										
										
											2018-09-26 16:27:09 +08:00
										 |  |  | 		const key = this._getKey(item); | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 		const entry = this._entries.get(key); | 
					
						
							|  |  |  | 		return entry !== undefined && entry.state === DONE_STATE; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	_ensureProcessing() { | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 		while (this._activeTasks < this._parallelism) { | 
					
						
							|  |  |  | 			const entry = this._queued.dequeue(); | 
					
						
							|  |  |  | 			if (entry === undefined) break; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			this._activeTasks++; | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 			entry.state = PROCESSING_STATE; | 
					
						
							|  |  |  | 			this._startProcessing(entry); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		this._willEnsureProcessing = false; | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 		if (this._queued.length > 0) return; | 
					
						
							|  |  |  | 		if (this._children !== undefined) { | 
					
						
							|  |  |  | 			for (const child of this._children) { | 
					
						
							|  |  |  | 				while (this._activeTasks < this._parallelism) { | 
					
						
							|  |  |  | 					const entry = child._queued.dequeue(); | 
					
						
							|  |  |  | 					if (entry === undefined) break; | 
					
						
							|  |  |  | 					this._activeTasks++; | 
					
						
							|  |  |  | 					entry.state = PROCESSING_STATE; | 
					
						
							|  |  |  | 					child._startProcessing(entry); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (child._queued.length > 0) return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (!this._willEnsureProcessing) this._needProcessing = false; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 	 * @param {AsyncQueueEntry<T, K, R>} entry the entry | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 	_startProcessing(entry) { | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		this.hooks.beforeStart.callAsync(entry.item, (err) => { | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2021-04-12 15:09:02 +08:00
										 |  |  | 				this._handleResult( | 
					
						
							|  |  |  | 					entry, | 
					
						
							|  |  |  | 					makeWebpackError(err, `AsyncQueue(${this._name}).hooks.beforeStart`) | 
					
						
							|  |  |  | 				); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-01-10 22:07:03 +08:00
										 |  |  | 			let inCallback = false; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			try { | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 				this._processor(entry.item, (e, r) => { | 
					
						
							| 
									
										
										
										
											2019-01-10 22:07:03 +08:00
										 |  |  | 					inCallback = true; | 
					
						
							| 
									
										
										
										
											2019-01-04 21:22:22 +08:00
										 |  |  | 					this._handleResult(entry, e, r); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} catch (err) { | 
					
						
							| 
									
										
										
										
											2019-01-10 22:07:03 +08:00
										 |  |  | 				if (inCallback) throw err; | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 				this._handleResult(entry, /** @type {WebpackError} */ (err), null); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 			this.hooks.started.call(entry.item); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 	 * @param {AsyncQueueEntry<T, K, R>} entry the entry | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 	 * @param {(WebpackError | null)=} err error, if any | 
					
						
							|  |  |  | 	 * @param {(R | null)=} result result, if any | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 	_handleResult(entry, err, result) { | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | 		this.hooks.result.callAsync(entry.item, err, result, (hookError) => { | 
					
						
							| 
									
										
										
										
											2021-04-12 15:09:02 +08:00
										 |  |  | 			const error = hookError | 
					
						
							|  |  |  | 				? makeWebpackError(hookError, `AsyncQueue(${this._name}).hooks.result`) | 
					
						
							|  |  |  | 				: err; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-06 11:08:48 +08:00
										 |  |  | 			const callback = /** @type {Callback<R>} */ (entry.callback); | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 			const callbacks = entry.callbacks; | 
					
						
							|  |  |  | 			entry.state = DONE_STATE; | 
					
						
							|  |  |  | 			entry.callback = undefined; | 
					
						
							|  |  |  | 			entry.callbacks = undefined; | 
					
						
							|  |  |  | 			entry.result = result; | 
					
						
							|  |  |  | 			entry.error = error; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-02 08:22:19 +08:00
										 |  |  | 			const root = this._root; | 
					
						
							|  |  |  | 			root._activeTasks--; | 
					
						
							|  |  |  | 			if (root._willEnsureProcessing === false && root._needProcessing) { | 
					
						
							|  |  |  | 				root._willEnsureProcessing = true; | 
					
						
							|  |  |  | 				setImmediate(root._ensureProcessing); | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-12 19:21:47 +08:00
										 |  |  | 			if (inHandleResult++ > 3) { | 
					
						
							|  |  |  | 				process.nextTick(() => { | 
					
						
							| 
									
										
										
										
											2019-11-08 19:22:42 +08:00
										 |  |  | 					callback(error, result); | 
					
						
							| 
									
										
										
										
											2019-11-12 19:21:47 +08:00
										 |  |  | 					if (callbacks !== undefined) { | 
					
						
							|  |  |  | 						for (const callback of callbacks) { | 
					
						
							|  |  |  | 							callback(error, result); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback(error, result); | 
					
						
							|  |  |  | 				if (callbacks !== undefined) { | 
					
						
							|  |  |  | 					for (const callback of callbacks) { | 
					
						
							|  |  |  | 						callback(error, result); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2019-01-04 01:29:21 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2019-11-08 19:22:42 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2019-11-12 19:21:47 +08:00
										 |  |  | 			inHandleResult--; | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-06-17 16:09:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	clear() { | 
					
						
							|  |  |  | 		this._entries.clear(); | 
					
						
							|  |  |  | 		this._queued.clear(); | 
					
						
							|  |  |  | 		this._activeTasks = 0; | 
					
						
							|  |  |  | 		this._willEnsureProcessing = false; | 
					
						
							|  |  |  | 		this._needProcessing = false; | 
					
						
							|  |  |  | 		this._stopped = false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-09-12 00:47:55 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = AsyncQueue; |