@@ -9,7 +9,6 @@ use std::{
99
1010use crate :: { editor:: get_ast_time, weewuh} ;
1111use leptos:: * ;
12- use leptos_query:: { create_query, QueryOptions } ;
1312use uiua:: { Handle , Report , SysBackend , EXAMPLE_TXT , EXAMPLE_UA } ;
1413use wasm_bindgen:: prelude:: * ;
1514use wasm_bindgen_futures:: JsFuture ;
@@ -32,7 +31,6 @@ struct VirtualStream {
3231
3332thread_local ! {
3433 static GLOBAL_FILES : RefCell <HashMap <PathBuf , Vec <u8 >>> = Default :: default ( ) ;
35- static REQ : RefCell <Option <FetchReq >> = Default :: default ( ) ;
3634}
3735
3836pub fn drop_file ( path : PathBuf , contents : Vec < u8 > ) {
@@ -370,20 +368,24 @@ impl SysBackend for WebBackend {
370368 url = format ! ( "{url}/main/lib.ua" ) ;
371369 }
372370 thread_local ! {
373- static CACHE : RefCell <HashMap <String , Result <String , String >>> = Default :: default ( ) ;
371+ static CACHE : RefCell <HashMap <String , Result <String , String >>> = Default :: default ( ) ;
374372 }
375373 let res = CACHE . with ( |cache| {
376- let mut cache = cache. borrow_mut ( ) ;
377- if let Some ( res ) = cache . get ( & url ) {
374+ if let Some ( res ) = cache. borrow ( ) . get ( & url ) {
375+ logging :: log! ( "Using cached module for {url:?}" ) ;
378376 Some ( res. clone ( ) )
379377 } else {
380- logging:: log!( "url: {url}" ) ;
381- let res = try_fetch_sync ( & url) ?;
382- logging:: log!( "res: {res:?}" ) ;
383- cache. insert ( url. clone ( ) , res. clone ( ) ) ;
384- Some ( res)
378+ logging:: log!( "Fetching url: {url}" ) ;
379+ spawn_local ( async move {
380+ let res = fetch ( & url) . await ;
381+ CACHE . with ( |cache| {
382+ cache. borrow_mut ( ) . insert ( url. clone ( ) , res. clone ( ) ) ;
383+ } ) ;
384+ } ) ;
385+ None
385386 }
386387 } ) ;
388+
387389 match res {
388390 Some ( Ok ( text) ) => {
389391 let contents = text. as_bytes ( ) . to_vec ( ) ;
@@ -396,41 +398,6 @@ impl SysBackend for WebBackend {
396398 }
397399}
398400
399- struct FetchReq {
400- url : String ,
401- query : Box < dyn Fn ( ) -> Option < Result < String , String > > > ,
402- tries : u32 ,
403- }
404-
405- pub fn try_fetch_sync ( url : & str ) -> Option < Result < String , String > > {
406- REQ . with ( |req| {
407- let mut req = req. borrow_mut ( ) ;
408- let url = url. to_string ( ) ;
409- if let Some ( req) = req. as_mut ( ) . filter ( |req| req. url == url) {
410- req. tries += 1 ;
411- if req. tries > 20 {
412- return Some ( Err ( "Failed to fetch" . into ( ) ) ) ;
413- }
414- ( req. query ) ( )
415- } else {
416- * req = Some ( FetchReq {
417- url : url. clone ( ) ,
418- query : {
419- let query_res = create_query ( fetch_string, QueryOptions :: default ( ) )
420- . use_query ( move || url. clone ( ) ) ;
421- Box :: new ( move || query_res. data . try_get ( ) . flatten ( ) )
422- } ,
423- tries : 0 ,
424- } ) ;
425- None
426- }
427- } )
428- }
429-
430- async fn fetch_string ( s : String ) -> Result < String , String > {
431- fetch ( & s) . await
432- }
433-
434401pub async fn fetch ( url : & str ) -> Result < String , String > {
435402 let mut opts = RequestInit :: new ( ) ;
436403 opts. method ( "GET" ) ;
0 commit comments