| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							|  |  |  | const RuntimeModule = require("../RuntimeModule"); | 
					
						
							|  |  |  | const Template = require("../Template"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | /** @typedef {import("../Compilation")} Compilation */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 16:42:01 +08:00
										 |  |  | class OnChunksLoadedRuntimeModule extends RuntimeModule { | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 	constructor() { | 
					
						
							|  |  |  | 		super("chunk loaded"); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2023-06-17 01:13:03 +08:00
										 |  |  | 	 * @returns {string | null} runtime code | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	generate() { | 
					
						
							| 
									
										
										
										
											2023-06-13 01:24:59 +08:00
										 |  |  | 		const compilation = /** @type {Compilation} */ (this.compilation); | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 		const { runtimeTemplate } = compilation; | 
					
						
							|  |  |  | 		return Template.asString([ | 
					
						
							|  |  |  | 			"var deferred = [];", | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 			`${RuntimeGlobals.onChunksLoaded} = ${runtimeTemplate.basicFunction( | 
					
						
							|  |  |  | 				"result, chunkIds, fn, priority", | 
					
						
							|  |  |  | 				[ | 
					
						
							|  |  |  | 					"if(chunkIds) {", | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 					Template.indent([ | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 						"priority = priority || 0;", | 
					
						
							|  |  |  | 						"for(var i = deferred.length; i > 0 && deferred[i - 1][2] > priority; i--) deferred[i] = deferred[i - 1];", | 
					
						
							|  |  |  | 						"deferred[i] = [chunkIds, fn, priority];", | 
					
						
							|  |  |  | 						"return;" | 
					
						
							|  |  |  | 					]), | 
					
						
							|  |  |  | 					"}", | 
					
						
							|  |  |  | 					"var notFulfilled = Infinity;", | 
					
						
							|  |  |  | 					"for (var i = 0; i < deferred.length; i++) {", | 
					
						
							|  |  |  | 					Template.indent([ | 
					
						
							|  |  |  | 						runtimeTemplate.destructureArray( | 
					
						
							|  |  |  | 							["chunkIds", "fn", "priority"], | 
					
						
							|  |  |  | 							"deferred[i]" | 
					
						
							|  |  |  | 						), | 
					
						
							|  |  |  | 						"var fulfilled = true;", | 
					
						
							|  |  |  | 						"for (var j = 0; j < chunkIds.length; j++) {", | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 						Template.indent([ | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 							`if ((priority & 1 === 0 || notFulfilled >= priority) && Object.keys(${ | 
					
						
							|  |  |  | 								RuntimeGlobals.onChunksLoaded | 
					
						
							|  |  |  | 							}).every(${runtimeTemplate.returningFunction( | 
					
						
							|  |  |  | 								`${RuntimeGlobals.onChunksLoaded}[key](chunkIds[j])`, | 
					
						
							|  |  |  | 								"key" | 
					
						
							|  |  |  | 							)})) {`,
 | 
					
						
							|  |  |  | 							Template.indent(["chunkIds.splice(j--, 1);"]), | 
					
						
							|  |  |  | 							"} else {", | 
					
						
							|  |  |  | 							Template.indent([ | 
					
						
							|  |  |  | 								"fulfilled = false;", | 
					
						
							|  |  |  | 								"if(priority < notFulfilled) notFulfilled = priority;" | 
					
						
							|  |  |  | 							]), | 
					
						
							|  |  |  | 							"}" | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 						]), | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 						"}", | 
					
						
							|  |  |  | 						"if(fulfilled) {", | 
					
						
							| 
									
										
										
										
											2021-07-16 17:17:25 +08:00
										 |  |  | 						Template.indent([ | 
					
						
							|  |  |  | 							"deferred.splice(i--, 1)", | 
					
						
							|  |  |  | 							"var r = fn();", | 
					
						
							|  |  |  | 							"if (r !== undefined) result = r;" | 
					
						
							|  |  |  | 						]), | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 						"}" | 
					
						
							|  |  |  | 					]), | 
					
						
							|  |  |  | 					"}", | 
					
						
							| 
									
										
										
										
											2021-05-11 15:31:46 +08:00
										 |  |  | 					"return result;" | 
					
						
							|  |  |  | 				] | 
					
						
							|  |  |  | 			)};`
 | 
					
						
							| 
									
										
										
										
											2021-03-09 03:18:49 +08:00
										 |  |  | 		]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 16:42:01 +08:00
										 |  |  | module.exports = OnChunksLoadedRuntimeModule; |