| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { AsyncParallelHook, AsyncSeriesBailHook, SyncHook } = require("tapable"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-28 03:57:54 +08:00
										 |  |  | /** @typedef {(result: any, callback: (err?: Error) => void) => void} GotHandler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | class Cache { | 
					
						
							|  |  |  | 	constructor() { | 
					
						
							|  |  |  | 		this.hooks = { | 
					
						
							|  |  |  | 			/** @type {AsyncSeriesBailHook<string, string>} */ | 
					
						
							| 
									
										
										
										
											2018-10-11 16:46:48 +08:00
										 |  |  | 			get: new AsyncSeriesBailHook(["identifier", "etag"]), | 
					
						
							|  |  |  | 			/** @type {AsyncParallelHook<string, string, any>} */ | 
					
						
							| 
									
										
										
										
											2018-12-28 03:57:54 +08:00
										 |  |  | 			got: new AsyncParallelHook(["identifier", "etag", "data"]), | 
					
						
							|  |  |  | 			/** @type {AsyncParallelHook<string, string, any>} */ | 
					
						
							| 
									
										
										
										
											2018-10-11 16:46:48 +08:00
										 |  |  | 			store: new AsyncParallelHook(["identifier", "etag", "data"]), | 
					
						
							| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | 			/** @type {SyncHook} */ | 
					
						
							|  |  |  | 			beginIdle: new SyncHook([]), | 
					
						
							|  |  |  | 			/** @type {AsyncParallelHook} */ | 
					
						
							|  |  |  | 			endIdle: new AsyncParallelHook([]), | 
					
						
							|  |  |  | 			/** @type {AsyncParallelHook} */ | 
					
						
							|  |  |  | 			shutdown: new AsyncParallelHook([]) | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-11 16:46:48 +08:00
										 |  |  | 	get(identifier, etag, callback) { | 
					
						
							| 
									
										
										
										
											2018-12-28 03:57:54 +08:00
										 |  |  | 		this.hooks.get.callAsync(identifier, etag, (err, result) => { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				callback(err); | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			this.hooks.got.callAsync(identifier, etag, result, err => { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					callback(err); | 
					
						
							|  |  |  | 					return; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				callback(null, result); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-09 20:30:59 +08:00
										 |  |  | 	store(identifier, etag, data, callback) { | 
					
						
							|  |  |  | 		this.hooks.store.callAsync(identifier, etag, data, callback); | 
					
						
							| 
									
										
										
										
											2018-09-27 13:22:19 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	beginIdle() { | 
					
						
							|  |  |  | 		this.hooks.beginIdle.call(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	endIdle(callback) { | 
					
						
							|  |  |  | 		this.hooks.endIdle.callAsync(callback); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	shutdown(callback) { | 
					
						
							|  |  |  | 		this.hooks.shutdown.callAsync(callback); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = Cache; |