@@ -10,6 +10,12 @@ use crate::{JsDeferred, JsUnknown, NapiValue};
10
10
11
11
#[ cfg( not( feature = "noop" ) ) ]
12
12
fn create_runtime ( ) -> Runtime {
13
+ if let Some ( user_defined_rt) = USER_DEFINED_RT
14
+ . get ( )
15
+ . and_then ( |rt| rt. write ( ) . ok ( ) . and_then ( |mut rt| rt. take ( ) ) )
16
+ {
17
+ return user_defined_rt;
18
+ }
13
19
#[ cfg( any(
14
20
all( target_family = "wasm" , tokio_unstable) ,
15
21
not( target_family = "wasm" )
@@ -57,19 +63,25 @@ pub fn create_custom_tokio_runtime(rt: Runtime) {
57
63
pub fn create_custom_tokio_runtime ( _: Runtime ) { }
58
64
59
65
#[ cfg( not( feature = "noop" ) ) ]
60
- /// Ensure that the Tokio runtime is initialized.
61
- /// In Node.js the Tokio runtime will be dropped when Node env exits.
66
+ /// Start the async runtime (Currently is tokio).
67
+ ///
68
+ /// In Node.js native targets the async runtime will be dropped when Node env exits.
62
69
/// But in Electron renderer process, the Node env will exits and recreate when the window reloads.
63
- /// So we need to ensure that the Tokio runtime is initialized when the Node env is created.
64
- pub ( crate ) fn ensure_runtime ( ) {
65
- let mut rt = RT . write ( ) . unwrap ( ) ;
66
- if rt. is_none ( ) {
67
- * rt = Some ( create_runtime ( ) ) ;
70
+ /// So we need to ensure that the async runtime is initialized when the Node env is created.
71
+ ///
72
+ /// In wasm targets, the async runtime will not been shutdown automatically due to the limitation of the wasm runtime.
73
+ /// So, you need to call `shutdown_async_runtime` function to manually shutdown the async runtime.
74
+ /// In some scenarios, you may want to start the async runtime again like in tests.
75
+ pub fn start_async_runtime ( ) {
76
+ if let Ok ( mut rt) = RT . write ( ) {
77
+ if rt. is_none ( ) {
78
+ * rt = Some ( create_runtime ( ) ) ;
79
+ }
68
80
}
69
81
}
70
82
71
83
#[ cfg( not( feature = "noop" ) ) ]
72
- pub fn shutdown_tokio_runtime ( ) {
84
+ pub fn shutdown_async_runtime ( ) {
73
85
if let Some ( rt) = RT . write ( ) . ok ( ) . and_then ( |mut rt| rt. take ( ) ) {
74
86
rt. shutdown_background ( ) ;
75
87
}
0 commit comments