feat: refactoring project

This commit is contained in:
Carlos
2024-11-23 14:56:07 -05:00
parent f0c2a50c18
commit 1c6db5818d
2351 changed files with 39323 additions and 60326 deletions

View File

@@ -20,7 +20,7 @@ let inHandleResult = 0;
* @template T
* @callback Callback
* @param {(WebpackError | null)=} err
* @param {T=} result
* @param {(T | null)=} result
*/
/**
@@ -37,15 +37,27 @@ class AsyncQueueEntry {
this.item = item;
/** @type {typeof QUEUED_STATE | typeof PROCESSING_STATE | typeof DONE_STATE} */
this.state = QUEUED_STATE;
/** @type {Callback<R> | undefined} */
this.callback = callback;
/** @type {Callback<R>[] | undefined} */
this.callbacks = undefined;
/** @type {R | null | undefined} */
this.result = undefined;
/** @type {WebpackError | undefined} */
/** @type {WebpackError | null | undefined} */
this.error = undefined;
}
}
/**
* @template T, K
* @typedef {function(T): K} getKey
*/
/**
* @template T, R
* @typedef {function(T, Callback<R>): void} Processor
*/
/**
* @template T
* @template K
@@ -56,16 +68,19 @@ class AsyncQueue {
* @param {object} options options object
* @param {string=} options.name name of the queue
* @param {number=} options.parallelism how many items should be processed at once
* @param {string=} options.context context of execution
* @param {AsyncQueue<any, any, any>=} options.parent parent queue, which will have priority over this queue and with shared parallelism
* @param {function(T): K=} options.getKey extract key from item
* @param {function(T, Callback<R>): void} options.processor async function to process items
* @param {getKey<T, K>=} options.getKey extract key from item
* @param {Processor<T, R>} options.processor async function to process items
*/
constructor({ name, parallelism, parent, processor, getKey }) {
constructor({ name, context, parallelism, parent, processor, getKey }) {
this._name = name;
this._context = context || "normal";
this._parallelism = parallelism || 1;
this._processor = processor;
this._getKey =
getKey || /** @type {(T) => K} */ (item => /** @type {any} */ (item));
getKey ||
/** @type {getKey<T, K>} */ (item => /** @type {T & K} */ (item));
/** @type {Map<K, AsyncQueueEntry<T, K, R>>} */
this._entries = new Map();
/** @type {ArrayQueue<AsyncQueueEntry<T, K, R>>} */
@@ -76,6 +91,7 @@ class AsyncQueue {
this._willEnsureProcessing = false;
this._needProcessing = false;
this._stopped = false;
/** @type {AsyncQueue<any, any, any>} */
this._root = parent ? parent._root : this;
if (parent) {
if (this._root._children === undefined) {
@@ -94,13 +110,27 @@ class AsyncQueue {
beforeStart: new AsyncSeriesHook(["item"]),
/** @type {SyncHook<[T]>} */
started: new SyncHook(["item"]),
/** @type {SyncHook<[T, Error, R]>} */
/** @type {SyncHook<[T, WebpackError | null | undefined, R | null | undefined]>} */
result: new SyncHook(["item", "error", "result"])
};
this._ensureProcessing = this._ensureProcessing.bind(this);
}
/**
* @returns {string} context of execution
*/
getContext() {
return this._context;
}
/**
* @param {string} value context of execution
*/
setContext(value) {
this._context = value;
}
/**
* @param {T} item an item
* @param {Callback<R>} callback callback function
@@ -202,9 +232,14 @@ class AsyncQueue {
this._queued = new ArrayQueue();
const root = this._root;
for (const entry of queue) {
this._entries.delete(this._getKey(entry.item));
this._entries.delete(
this._getKey(/** @type {AsyncQueueEntry<T, K, R>} */ (entry).item)
);
root._activeTasks++;
this._handleResult(entry, new WebpackError("Queue was stopped"));
this._handleResult(
/** @type {AsyncQueueEntry<T, K, R>} */ (entry),
new WebpackError("Queue was stopped")
);
}
}
@@ -308,7 +343,7 @@ class AsyncQueue {
});
} catch (err) {
if (inCallback) throw err;
this._handleResult(entry, err, null);
this._handleResult(entry, /** @type {WebpackError} */ (err), null);
}
this.hooks.started.call(entry.item);
});
@@ -316,8 +351,8 @@ class AsyncQueue {
/**
* @param {AsyncQueueEntry<T, K, R>} entry the entry
* @param {WebpackError=} err error, if any
* @param {R=} result result, if any
* @param {(WebpackError | null)=} err error, if any
* @param {(R | null)=} result result, if any
* @returns {void}
*/
_handleResult(entry, err, result) {
@@ -326,7 +361,7 @@ class AsyncQueue {
? makeWebpackError(hookError, `AsyncQueue(${this._name}).hooks.result`)
: err;
const callback = entry.callback;
const callback = /** @type {Callback<R>} */ (entry.callback);
const callbacks = entry.callbacks;
entry.state = DONE_STATE;
entry.callback = undefined;