@@ -26,6 +26,24 @@ use std::sync::Once;
2626static GLOBAL_PACK_PROJECT : RwLock < Option < Arc < PackProject > > > = RwLock :: new ( None ) ;
2727static GLOBAL_THREAD_URL : RwLock < Option < String > > = RwLock :: new ( None ) ;
2828
29+ fn block_on < T > ( fut : impl std:: future:: Future < Output = T > + Send + ' static ) -> Result < T , JsError >
30+ where
31+ T : Send + ' static ,
32+ {
33+ let ( sender, receiver) = oneshot:: channel ( ) ;
34+ let rt = crate :: tokio_runtime:: TOKIO_RUNTIME
35+ . get ( )
36+ . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
37+
38+ rt. spawn ( async move {
39+ let _ = sender. send ( fut. await ) ;
40+ } ) ;
41+
42+ receiver
43+ . recv ( )
44+ . map_err ( |e| JsError :: new ( & format ! ( "Recv error: {}" , e) ) )
45+ }
46+
2947#[ wasm_bindgen]
3048pub struct Project ;
3149
@@ -365,47 +383,31 @@ impl Project {
365383
366384 #[ wasm_bindgen( js_name = readSync) ]
367385 pub fn read_sync ( path : String ) -> Result < js_sys:: Uint8Array , JsError > {
368- let ( sender, receiver) = oneshot:: channel ( ) ;
369- let rt = crate :: tokio_runtime:: TOKIO_RUNTIME
370- . get ( )
371- . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
372-
373386 let path_clone = path. clone ( ) ;
374- rt . spawn ( async move {
375- let result = turbo_tasks_fs:: wasm_fs_offload:: CLIENT
387+ let fut = async move {
388+ turbo_tasks_fs:: wasm_fs_offload:: CLIENT
376389 . read ( & path_clone)
377- . await ;
378- let _ = sender. send ( result) ;
379- } ) ;
380-
381- let bytes = receiver
382- . recv ( )
383- . context ( "Recv error" )
384- . and_then ( |res| res. with_context ( || format ! ( "Failed to read file: {}" , path) ) )
390+ . await
391+ } ;
392+
393+ let bytes = block_on ( fut) ?
394+ . with_context ( || format ! ( "Failed to read file: {}" , path) )
385395 . map_err ( to_js_error) ?;
386396
387397 Ok ( js_sys:: Uint8Array :: from ( & bytes[ ..] ) )
388398 }
389399
390400 #[ wasm_bindgen( js_name = readDirSync) ]
391401 pub fn read_dir_sync ( path : String ) -> Result < Vec < DirEntry > , JsError > {
392- let ( sender, receiver) = oneshot:: channel ( ) ;
393- let rt = crate :: tokio_runtime:: TOKIO_RUNTIME
394- . get ( )
395- . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
396-
397402 let path_clone = path. clone ( ) ;
398- rt . spawn ( async move {
399- let result = turbo_tasks_fs:: wasm_fs_offload:: CLIENT
403+ let fut = async move {
404+ turbo_tasks_fs:: wasm_fs_offload:: CLIENT
400405 . read_dir ( & path_clone)
401- . await ;
402- let _ = sender. send ( result) ;
403- } ) ;
404-
405- let read_dir = receiver
406- . recv ( )
407- . context ( "Recv error" )
408- . and_then ( |res| res. with_context ( || format ! ( "Failed to read directory: {}" , path) ) )
406+ . await
407+ } ;
408+
409+ let read_dir = block_on ( fut) ?
410+ . with_context ( || format ! ( "Failed to read directory: {}" , path) )
409411 . map_err ( to_js_error) ?;
410412
411413 let ret = read_dir
@@ -422,187 +424,123 @@ impl Project {
422424
423425 #[ wasm_bindgen( js_name = writeSync) ]
424426 pub fn write_sync ( path : String , content : Vec < u8 > ) -> Result < ( ) , JsError > {
425- let ( sender, receiver) = oneshot:: channel ( ) ;
426- let rt = crate :: tokio_runtime:: TOKIO_RUNTIME
427- . get ( )
428- . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
429-
430427 let path_clone = path. clone ( ) ;
431- rt . spawn ( async move {
432- let result = turbo_tasks_fs:: wasm_fs_offload:: CLIENT
428+ let fut = async move {
429+ turbo_tasks_fs:: wasm_fs_offload:: CLIENT
433430 . write ( & path_clone, & content)
434- . await ;
435- let _ = sender. send ( result) ;
436- } ) ;
437-
438- receiver
439- . recv ( )
440- . context ( "Recv error" )
441- . and_then ( |res| res. with_context ( || format ! ( "Failed to write file: {}" , path) ) )
431+ . await
432+ } ;
433+
434+ block_on ( fut) ?
435+ . with_context ( || format ! ( "Failed to write file: {}" , path) )
442436 . map_err ( to_js_error) ?;
443437
444438 Ok ( ( ) )
445439 }
446440
447441 #[ wasm_bindgen( js_name = createDirSync) ]
448442 pub fn create_dir_sync ( path : String ) -> Result < ( ) , JsError > {
449- let ( sender, receiver) = oneshot:: channel ( ) ;
450- let rt = crate :: tokio_runtime:: TOKIO_RUNTIME
451- . get ( )
452- . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
453-
454443 let path_clone = path. clone ( ) ;
455- rt . spawn ( async move {
456- let result = turbo_tasks_fs:: wasm_fs_offload:: CLIENT
444+ let fut = async move {
445+ turbo_tasks_fs:: wasm_fs_offload:: CLIENT
457446 . create_dir ( & path_clone)
458- . await ;
459- let _ = sender. send ( result) ;
460- } ) ;
461-
462- receiver
463- . recv ( )
464- . context ( "Recv error" )
465- . and_then ( |res| res. with_context ( || format ! ( "Failed to create directory: {}" , path) ) )
447+ . await
448+ } ;
449+
450+ block_on ( fut) ?
451+ . with_context ( || format ! ( "Failed to create directory: {}" , path) )
466452 . map_err ( to_js_error) ?;
453+
467454 Ok ( ( ) )
468455 }
469456
470457 #[ wasm_bindgen( js_name = createDirAllSync) ]
471458 pub fn create_dir_all_sync ( path : String ) -> Result < ( ) , JsError > {
472- let ( sender, receiver) = oneshot:: channel ( ) ;
473- let rt = crate :: tokio_runtime:: TOKIO_RUNTIME
474- . get ( )
475- . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
476-
477459 let path_clone = path. clone ( ) ;
478- rt . spawn ( async move {
479- let result = turbo_tasks_fs:: wasm_fs_offload:: CLIENT
460+ let fut = async move {
461+ turbo_tasks_fs:: wasm_fs_offload:: CLIENT
480462 . create_dir_all ( & path_clone)
481- . await ;
482- let _ = sender. send ( result) ;
483- } ) ;
484-
485- receiver
486- . recv ( )
487- . context ( "Recv error" )
488- . and_then ( |res| {
489- res. with_context ( || format ! ( "Failed to create directory recursively: {}" , path) )
490- } )
463+ . await
464+ } ;
465+
466+ block_on ( fut) ?
467+ . with_context ( || format ! ( "Failed to create directory recursively: {}" , path) )
491468 . map_err ( to_js_error) ?;
492469
493470 Ok ( ( ) )
494471 }
495472
496473 #[ wasm_bindgen( js_name = copyFileSync) ]
497474 pub fn copy_file_sync ( src : String , dst : String ) -> Result < ( ) , JsError > {
498- let ( sender, receiver) = oneshot:: channel ( ) ;
499- let rt = crate :: tokio_runtime:: TOKIO_RUNTIME
500- . get ( )
501- . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
502-
503475 let src_clone = src. clone ( ) ;
504476 let dst_clone = dst. clone ( ) ;
505- rt . spawn ( async move {
477+ let fut = async move {
506478 // Client doesn't seem to expose copy, so we implement it manually
507- let result = async {
508- let content = turbo_tasks_fs:: wasm_fs_offload:: CLIENT
509- . read ( & src_clone)
510- . await ?;
511- turbo_tasks_fs:: wasm_fs_offload:: CLIENT
512- . write ( & dst_clone, & content)
513- . await
514- }
515- . await ;
516- let _ = sender. send ( result) ;
517- } ) ;
518-
519- receiver
520- . recv ( )
521- . context ( "Recv error" )
522- . and_then ( |res| {
523- res. with_context ( || format ! ( "Failed to copy file from {} to {}" , src, dst) )
524- } )
479+ let content = turbo_tasks_fs:: wasm_fs_offload:: CLIENT
480+ . read ( & src_clone)
481+ . await ?;
482+ turbo_tasks_fs:: wasm_fs_offload:: CLIENT
483+ . write ( & dst_clone, & content)
484+ . await
485+ } ;
486+
487+ block_on ( fut) ?
488+ . with_context ( || format ! ( "Failed to copy file from {} to {}" , src, dst) )
525489 . map_err ( to_js_error) ?;
526490
527491 Ok ( ( ) )
528492 }
529493
530494 #[ wasm_bindgen( js_name = removeFileSync) ]
531495 pub fn remove_file_sync ( path : String ) -> Result < ( ) , JsError > {
532- let ( sender, receiver) = oneshot:: channel ( ) ;
533- let rt = crate :: tokio_runtime:: TOKIO_RUNTIME
534- . get ( )
535- . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
536-
537496 let path_clone = path. clone ( ) ;
538- rt . spawn ( async move {
539- let result = turbo_tasks_fs:: wasm_fs_offload:: CLIENT
497+ let fut = async move {
498+ turbo_tasks_fs:: wasm_fs_offload:: CLIENT
540499 . remove_file ( & path_clone)
541- . await ;
542- let _ = sender. send ( result) ;
543- } ) ;
544-
545- receiver
546- . recv ( )
547- . context ( "Recv error" )
548- . and_then ( |res| res. with_context ( || format ! ( "Failed to remove file: {}" , path) ) )
500+ . await
501+ } ;
502+
503+ block_on ( fut) ?
504+ . with_context ( || format ! ( "Failed to remove file: {}" , path) )
549505 . map_err ( to_js_error) ?;
550506
551507 Ok ( ( ) )
552508 }
553509
554510 #[ wasm_bindgen( js_name = removeDirSync) ]
555511 pub fn remove_dir_sync ( path : String , recursive : bool ) -> Result < ( ) , JsError > {
556- let ( sender, receiver) = oneshot:: channel ( ) ;
557- let rt = crate :: tokio_runtime:: TOKIO_RUNTIME
558- . get ( )
559- . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
560-
561512 let path_clone = path. clone ( ) ;
562- rt . spawn ( async move {
563- let result = if recursive {
513+ let fut = async move {
514+ if recursive {
564515 turbo_tasks_fs:: wasm_fs_offload:: CLIENT
565516 . remove_dir_all ( & path_clone)
566517 . await
567518 } else {
568519 turbo_tasks_fs:: wasm_fs_offload:: CLIENT
569520 . remove_dir ( & path_clone)
570521 . await
571- } ;
572- let _ = sender. send ( result) ;
573- } ) ;
522+ }
523+ } ;
574524
575- receiver
576- . recv ( )
577- . context ( "Recv error" )
578- . and_then ( |res| res. with_context ( || format ! ( "Failed to remove directory: {}" , path) ) )
525+ block_on ( fut) ?
526+ . with_context ( || format ! ( "Failed to remove directory: {}" , path) )
579527 . map_err ( to_js_error) ?;
580528
581529 Ok ( ( ) )
582530 }
583531
584532 #[ wasm_bindgen( js_name = metadataSync) ]
585533 pub fn metadata_sync ( path : String ) -> Result < Metadata , JsError > {
586- let ( sender, receiver) = oneshot:: channel ( ) ;
587- let rt = crate :: tokio_runtime:: TOKIO_RUNTIME
588- . get ( )
589- . ok_or_else ( || JsError :: new ( "tokio runtime not initialized" ) ) ?;
590-
591534 let path_clone = path. clone ( ) ;
592- rt . spawn ( async move {
593- let result = turbo_tasks_fs:: wasm_fs_offload:: CLIENT
535+ let fut = async move {
536+ turbo_tasks_fs:: wasm_fs_offload:: CLIENT
594537 . metadata ( & path_clone)
595- . await ;
596- let _ = sender. send ( result) ;
597- } ) ;
598-
599- receiver
600- . recv ( )
601- . context ( "Recv error" )
602- . and_then ( |res| {
603- res. and_then ( Metadata :: try_from)
604- . with_context ( || format ! ( "Failed to get metadata: {}" , path) )
605- } )
538+ . await
539+ } ;
540+
541+ block_on ( fut) ?
542+ . and_then ( Metadata :: try_from)
543+ . with_context ( || format ! ( "Failed to get metadata: {}" , path) )
606544 . map_err ( to_js_error)
607545 }
608546}
0 commit comments