@@ -58,9 +58,14 @@ impl Project {
5858 /// Calculate MD5 hash of byte content (async for better thread scheduling)
5959 #[ wasm_bindgen( js_name = sigMd5) ]
6060 pub async fn sig_md5 ( content : Vec < u8 > ) -> Result < String , JsError > {
61- let result = tokio:: task:: spawn_blocking ( move || opfs_project:: pack:: sig_md5 ( & content) )
61+ let rt = TOKIO_RUNTIME
62+ . get ( )
63+ . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
64+
65+ let result = rt
66+ . spawn_blocking ( move || opfs_project:: pack:: sig_md5 ( & content) )
6267 . await
63- . map_err ( |e| JsError :: new ( & format ! ( "Task failed: {}" , e ) ) ) ?;
68+ . map_err ( to_js_error ) ?;
6469 Ok ( result)
6570 }
6671
@@ -85,9 +90,13 @@ impl Project {
8590 . map ( |f| PackFile :: new ( f. path , f. content ) )
8691 . collect ( ) ;
8792
88- let bytes = tokio:: task:: spawn_blocking ( move || opfs_project:: pack:: gzip ( & pack_files) )
89- . await
90- . map_err ( |e| JsError :: new ( & format ! ( "Task failed: {}" , e) ) ) ?
93+ let rt = TOKIO_RUNTIME
94+ . get ( )
95+ . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
96+
97+ let bytes = rt
98+ . spawn_blocking ( move || opfs_project:: pack:: gzip ( & pack_files) )
99+ . await ?
91100 . map_err ( to_js_error) ?;
92101 Ok ( js_sys:: Uint8Array :: from ( & bytes[ ..] ) )
93102 }
@@ -143,12 +152,11 @@ impl Project {
143152 None => return Err ( JsError :: new ( "invalid pack project" ) ) ,
144153 } ;
145154
146- TOKIO_RUNTIME
147- . with ( |rt| {
148- rt. get ( )
149- . expect ( "tokio runtime not found" )
150- . spawn ( async move { pack_project. build ( ) . await } )
151- } )
155+ let rt = TOKIO_RUNTIME
156+ . get ( )
157+ . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
158+
159+ rt. spawn ( async move { pack_project. build ( ) . await } )
152160 . await
153161 . map_err ( to_js_error) ?
154162 . map_or_else (
@@ -206,12 +214,11 @@ impl Project {
206214 ..Default :: default ( )
207215 } ;
208216
209- let pack_context = TOKIO_RUNTIME
210- . with ( |rt| {
211- rt. get ( )
212- . expect ( "tokio runtime not found" )
213- . spawn ( PackProject :: initialize ( options) )
214- } )
217+ let rt = TOKIO_RUNTIME
218+ . get ( )
219+ . ok_or_else ( || anyhow:: anyhow!( "tokio runtime not initialized" ) ) ?;
220+ let pack_context = rt
221+ . spawn ( PackProject :: initialize ( options) )
215222 . await
216223 . context ( "fail to initialize pack project" ) ??;
217224
0 commit comments