mirror of https://github.com/webpack/webpack.git
				
				
				
			
		
			
				
	
	
		
			127 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
			
		
		
	
	
			127 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
| /*
 | |
| 	MIT License http://www.opensource.org/licenses/mit-license.php
 | |
| 	Author Tobias Koppers @sokra
 | |
| */
 | |
| 
 | |
| "use strict";
 | |
| 
 | |
| const { AsyncParallelHook, AsyncSeriesBailHook, SyncHook } = require("tapable");
 | |
| const { makeWebpackError } = require("./HookWebpackError");
 | |
| 
 | |
| /** @typedef {import("./WebpackError")} WebpackError */
 | |
| 
 | |
| /**
 | |
|  * @template T
 | |
|  * @callback Callback
 | |
|  * @param {WebpackError=} err
 | |
|  * @param {T=} stats
 | |
|  * @returns {void}
 | |
|  */
 | |
| 
 | |
| /**
 | |
|  * @callback GotHandler
 | |
|  * @param {any} result
 | |
|  * @param {Callback<void>} stats
 | |
|  * @returns {void}
 | |
|  */
 | |
| 
 | |
| const needCalls = (times, callback) => {
 | |
| 	return err => {
 | |
| 		if (--times === 0) {
 | |
| 			return callback(err);
 | |
| 		}
 | |
| 		if (err && times > 0) {
 | |
| 			times = NaN;
 | |
| 			return callback(err);
 | |
| 		}
 | |
| 	};
 | |
| };
 | |
| 
 | |
| class Cache {
 | |
| 	constructor() {
 | |
| 		this.hooks = {
 | |
| 			/** @type {AsyncSeriesBailHook<[string, string, GotHandler[]], any>} */
 | |
| 			get: new AsyncSeriesBailHook(["identifier", "etag", "gotHandlers"]),
 | |
| 			/** @type {AsyncParallelHook<[string, string, any]>} */
 | |
| 			store: new AsyncParallelHook(["identifier", "etag", "data"]),
 | |
| 			/** @type {SyncHook<[]>} */
 | |
| 			beginIdle: new SyncHook([]),
 | |
| 			/** @type {AsyncParallelHook<[]>} */
 | |
| 			endIdle: new AsyncParallelHook([]),
 | |
| 			/** @type {AsyncParallelHook<[]>} */
 | |
| 			shutdown: new AsyncParallelHook([])
 | |
| 		};
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * @template T
 | |
| 	 * @param {string} identifier the cache identifier
 | |
| 	 * @param {string} etag the etag
 | |
| 	 * @param {Callback<T>} callback signals when the value is retrieved
 | |
| 	 * @returns {void}
 | |
| 	 */
 | |
| 	get(identifier, etag, callback) {
 | |
| 		const gotHandlers = [];
 | |
| 		this.hooks.get.callAsync(identifier, etag, gotHandlers, (err, result) => {
 | |
| 			if (err) {
 | |
| 				callback(makeWebpackError(err, "Cache.hooks.get"));
 | |
| 				return;
 | |
| 			}
 | |
| 			if (gotHandlers.length > 1) {
 | |
| 				const innerCallback = needCalls(gotHandlers.length, () =>
 | |
| 					callback(null, result)
 | |
| 				);
 | |
| 				for (const gotHandler of gotHandlers) {
 | |
| 					gotHandler(result, innerCallback);
 | |
| 				}
 | |
| 			} else if (gotHandlers.length === 1) {
 | |
| 				gotHandlers[0](result, () => callback(null, result));
 | |
| 			} else {
 | |
| 				callback(null, result);
 | |
| 			}
 | |
| 		});
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * @template T
 | |
| 	 * @param {string} identifier the cache identifier
 | |
| 	 * @param {string} etag the etag
 | |
| 	 * @param {T} data the value to store
 | |
| 	 * @param {Callback<void>} callback signals when the value is stored
 | |
| 	 * @returns {void}
 | |
| 	 */
 | |
| 	store(identifier, etag, data, callback) {
 | |
| 		this.hooks.store.callAsync(identifier, etag, data, callback);
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * @returns {void}
 | |
| 	 */
 | |
| 	beginIdle() {
 | |
| 		this.hooks.beginIdle.call();
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * @param {Callback<void>} callback signals when the call finishes
 | |
| 	 * @returns {void}
 | |
| 	 */
 | |
| 	endIdle(callback) {
 | |
| 		this.hooks.endIdle.callAsync(callback);
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * @param {Callback<void>} callback signals when the call finishes
 | |
| 	 * @returns {void}
 | |
| 	 */
 | |
| 	shutdown(callback) {
 | |
| 		this.hooks.shutdown.callAsync(callback);
 | |
| 	}
 | |
| }
 | |
| 
 | |
| Cache.STAGE_MEMORY = -10;
 | |
| Cache.STAGE_DEFAULT = 0;
 | |
| Cache.STAGE_DISK = 10;
 | |
| Cache.STAGE_NETWORK = 20;
 | |
| 
 | |
| module.exports = Cache;
 |