refactor(runtime): split up MainWorker and WebWorker's preload_module method into two separate methods (#15451)

This commit is contained in:
David Sherret 2022-08-10 18:10:51 -04:00 committed by GitHub
parent 578f12d531
commit 321a42d1fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 37 deletions

View file

@ -485,7 +485,7 @@ async fn install_command(
Default::default(), Default::default(),
); );
// First, fetch and compile the module; this step ensures that the module exists. // First, fetch and compile the module; this step ensures that the module exists.
worker.preload_module(&main_module, true).await?; worker.preload_main_module(&main_module).await?;
tools::installer::install(flags, install_flags)?; tools::installer::install(flags, install_flags)?;
Ok(0) Ok(0)
} }

View file

@ -514,24 +514,26 @@ impl WebWorker {
Ok(()) Ok(())
} }
/// Loads and instantiates specified JavaScript module /// Loads and instantiates specified JavaScript module as "main" module.
/// as "main" or "side" module. pub async fn preload_main_module(
pub async fn preload_module(
&mut self, &mut self,
module_specifier: &ModuleSpecifier, module_specifier: &ModuleSpecifier,
main: bool,
) -> Result<ModuleId, AnyError> { ) -> Result<ModuleId, AnyError> {
if main { self
self .js_runtime
.js_runtime .load_main_module(module_specifier, None)
.load_main_module(module_specifier, None) .await
.await }
} else {
self /// Loads and instantiates specified JavaScript module as "side" module.
.js_runtime pub async fn preload_side_module(
.load_side_module(module_specifier, None) &mut self,
.await module_specifier: &ModuleSpecifier,
} ) -> Result<ModuleId, AnyError> {
self
.js_runtime
.load_side_module(module_specifier, None)
.await
} }
/// Loads, instantiates and executes specified JavaScript module. /// Loads, instantiates and executes specified JavaScript module.
@ -542,7 +544,7 @@ impl WebWorker {
&mut self, &mut self,
module_specifier: &ModuleSpecifier, module_specifier: &ModuleSpecifier,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let id = self.preload_module(module_specifier, false).await?; let id = self.preload_side_module(module_specifier).await?;
let mut receiver = self.js_runtime.mod_evaluate(id); let mut receiver = self.js_runtime.mod_evaluate(id);
tokio::select! { tokio::select! {
biased; biased;
@ -700,7 +702,7 @@ pub fn run_web_worker(
} else { } else {
// TODO(bartlomieju): add "type": "classic", ie. ability to load // TODO(bartlomieju): add "type": "classic", ie. ability to load
// script instead of module // script instead of module
match worker.preload_module(&specifier, true).await { match worker.preload_main_module(&specifier).await {
Ok(id) => { Ok(id) => {
worker.start_polling_for_messages(); worker.start_polling_for_messages();
worker.execute_main_module(id).await worker.execute_main_module(id).await

View file

@ -217,24 +217,26 @@ impl MainWorker {
Ok(()) Ok(())
} }
/// Loads and instantiates specified JavaScript module /// Loads and instantiates specified JavaScript module as "main" module.
/// as "main" or "side" module. pub async fn preload_main_module(
pub async fn preload_module(
&mut self, &mut self,
module_specifier: &ModuleSpecifier, module_specifier: &ModuleSpecifier,
main: bool,
) -> Result<ModuleId, AnyError> { ) -> Result<ModuleId, AnyError> {
if main { self
self .js_runtime
.js_runtime .load_main_module(module_specifier, None)
.load_main_module(module_specifier, None) .await
.await }
} else {
self /// Loads and instantiates specified JavaScript module as "side" module.
.js_runtime pub async fn preload_side_module(
.load_side_module(module_specifier, None) &mut self,
.await module_specifier: &ModuleSpecifier,
} ) -> Result<ModuleId, AnyError> {
self
.js_runtime
.load_side_module(module_specifier, None)
.await
} }
/// Executes specified JavaScript module. /// Executes specified JavaScript module.
@ -242,6 +244,7 @@ impl MainWorker {
&mut self, &mut self,
id: ModuleId, id: ModuleId,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
self.wait_for_inspector_session();
let mut receiver = self.js_runtime.mod_evaluate(id); let mut receiver = self.js_runtime.mod_evaluate(id);
tokio::select! { tokio::select! {
// Not using biased mode leads to non-determinism for relatively simple // Not using biased mode leads to non-determinism for relatively simple
@ -266,8 +269,7 @@ impl MainWorker {
&mut self, &mut self,
module_specifier: &ModuleSpecifier, module_specifier: &ModuleSpecifier,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let id = self.preload_module(module_specifier, false).await?; let id = self.preload_side_module(module_specifier).await?;
self.wait_for_inspector_session();
self.evaluate_module(id).await self.evaluate_module(id).await
} }
@ -278,8 +280,7 @@ impl MainWorker {
&mut self, &mut self,
module_specifier: &ModuleSpecifier, module_specifier: &ModuleSpecifier,
) -> Result<(), AnyError> { ) -> Result<(), AnyError> {
let id = self.preload_module(module_specifier, true).await?; let id = self.preload_main_module(module_specifier).await?;
self.wait_for_inspector_session();
self.evaluate_module(id).await self.evaluate_module(id).await
} }