@@ -9,7 +9,6 @@ use std::{
9
9
10
10
use crate :: { editor:: get_ast_time, weewuh} ;
11
11
use leptos:: * ;
12
- use leptos_query:: { create_query, QueryOptions } ;
13
12
use uiua:: { Handle , Report , SysBackend , EXAMPLE_TXT , EXAMPLE_UA } ;
14
13
use wasm_bindgen:: prelude:: * ;
15
14
use wasm_bindgen_futures:: JsFuture ;
@@ -32,7 +31,6 @@ struct VirtualStream {
32
31
33
32
thread_local ! {
34
33
static GLOBAL_FILES : RefCell <HashMap <PathBuf , Vec <u8 >>> = Default :: default ( ) ;
35
- static REQ : RefCell <Option <FetchReq >> = Default :: default ( ) ;
36
34
}
37
35
38
36
pub fn drop_file ( path : PathBuf , contents : Vec < u8 > ) {
@@ -370,20 +368,24 @@ impl SysBackend for WebBackend {
370
368
url = format ! ( "{url}/main/lib.ua" ) ;
371
369
}
372
370
thread_local ! {
373
- static CACHE : RefCell <HashMap <String , Result <String , String >>> = Default :: default ( ) ;
371
+ static CACHE : RefCell <HashMap <String , Result <String , String >>> = Default :: default ( ) ;
374
372
}
375
373
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:?}" ) ;
378
376
Some ( res. clone ( ) )
379
377
} 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
385
386
}
386
387
} ) ;
388
+
387
389
match res {
388
390
Some ( Ok ( text) ) => {
389
391
let contents = text. as_bytes ( ) . to_vec ( ) ;
@@ -396,41 +398,6 @@ impl SysBackend for WebBackend {
396
398
}
397
399
}
398
400
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
-
434
401
pub async fn fetch ( url : & str ) -> Result < String , String > {
435
402
let mut opts = RequestInit :: new ( ) ;
436
403
opts. method ( "GET" ) ;
0 commit comments