| 
									
										
										
										
											2019-06-05 20:17:15 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const InitFragment = require("../InitFragment"); | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | const RuntimeGlobals = require("../RuntimeGlobals"); | 
					
						
							| 
									
										
										
										
											2019-06-05 20:17:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | /** @typedef {import("webpack-sources").Source} Source */ | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | /** @typedef {import("../Generator").GenerateContext} GenerateContext */ | 
					
						
							| 
									
										
										
										
											2019-06-05 20:17:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class AwaitDependenciesInitFragment extends InitFragment { | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 	 * @param {Set<string>} promises the promises that should be awaited | 
					
						
							| 
									
										
										
										
											2019-06-05 20:17:15 +08:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(promises) { | 
					
						
							|  |  |  | 		super( | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 			undefined, | 
					
						
							| 
									
										
										
										
											2019-06-05 20:17:15 +08:00
										 |  |  | 			InitFragment.STAGE_ASYNC_DEPENDENCIES, | 
					
						
							|  |  |  | 			0, | 
					
						
							|  |  |  | 			"await-dependencies" | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		this.promises = promises; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	merge(other) { | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 		const promises = new Set(this.promises); | 
					
						
							|  |  |  | 		for (const p of other.promises) { | 
					
						
							|  |  |  | 			promises.add(p); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return new AwaitDependenciesInitFragment(promises); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 	 * @param {GenerateContext} generateContext context for generate | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 	 * @returns {string|Source} the source code that will be included as initialization code | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2019-08-27 02:21:07 +08:00
										 |  |  | 	getContent({ runtimeRequirements }) { | 
					
						
							|  |  |  | 		runtimeRequirements.add(RuntimeGlobals.module); | 
					
						
							| 
									
										
										
										
											2019-08-23 20:07:01 +08:00
										 |  |  | 		const promises = this.promises; | 
					
						
							|  |  |  | 		if (promises.size === 0) { | 
					
						
							|  |  |  | 			return ""; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (promises.size === 1) { | 
					
						
							|  |  |  | 			for (const p of promises) { | 
					
						
							|  |  |  | 				return `${p} = await Promise.resolve(${p});\n`; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		const sepPromises = Array.from(promises).join(", "); | 
					
						
							|  |  |  | 		return `([${sepPromises}] = await Promise.all([${sepPromises}]));\n`; | 
					
						
							| 
									
										
										
										
											2019-06-05 20:17:15 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = AwaitDependenciesInitFragment; |