Skip to content

Commit

Permalink
attempt to fix web backend git fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jun 13, 2024
1 parent 4204e7f commit b7402a2
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 85 deletions.
37 changes: 0 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion site/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ instant = {version = "0.1.12", features = ['wasm-bindgen']}
js-sys = "0.3"
leptos = "0.6.11"
leptos_meta = {version = "0.6.11", features = ["csr"]}
leptos_query = {version = "0.5.3", features = ["csr"]}
leptos_router = {version = "0.6.11", features = ["csr"]}
pathdiff = "0.2.1"
serde = {version = "1", features = ["derive"]}
Expand Down
57 changes: 12 additions & 45 deletions site/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use std::{

use crate::{editor::get_ast_time, weewuh};
use leptos::*;
use leptos_query::{create_query, QueryOptions};
use uiua::{Handle, Report, SysBackend, EXAMPLE_TXT, EXAMPLE_UA};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::JsFuture;
Expand All @@ -32,7 +31,6 @@ struct VirtualStream {

thread_local! {
static GLOBAL_FILES: RefCell<HashMap<PathBuf, Vec<u8>>> = Default::default();
static REQ: RefCell<Option<FetchReq>> = Default::default();
}

pub fn drop_file(path: PathBuf, contents: Vec<u8>) {
Expand Down Expand Up @@ -370,20 +368,24 @@ impl SysBackend for WebBackend {
url = format!("{url}/main/lib.ua");
}
thread_local! {
static CACHE: RefCell<HashMap<String, Result<String, String>>> = Default::default();
static CACHE: RefCell<HashMap<String, Result<String, String>>> = Default::default();
}
let res = CACHE.with(|cache| {
let mut cache = cache.borrow_mut();
if let Some(res) = cache.get(&url) {
if let Some(res) = cache.borrow().get(&url) {
logging::log!("Using cached module for {url:?}");
Some(res.clone())
} else {
logging::log!("url: {url}");
let res = try_fetch_sync(&url)?;
logging::log!("res: {res:?}");
cache.insert(url.clone(), res.clone());
Some(res)
logging::log!("Fetching url: {url}");
spawn_local(async move {
let res = fetch(&url).await;
CACHE.with(|cache| {
cache.borrow_mut().insert(url.clone(), res.clone());
});
});
None
}
});

match res {
Some(Ok(text)) => {
let contents = text.as_bytes().to_vec();
Expand All @@ -396,41 +398,6 @@ impl SysBackend for WebBackend {
}
}

struct FetchReq {
url: String,
query: Box<dyn Fn() -> Option<Result<String, String>>>,
tries: u32,
}

pub fn try_fetch_sync(url: &str) -> Option<Result<String, String>> {
REQ.with(|req| {
let mut req = req.borrow_mut();
let url = url.to_string();
if let Some(req) = req.as_mut().filter(|req| req.url == url) {
req.tries += 1;
if req.tries > 20 {
return Some(Err("Failed to fetch".into()));
}
(req.query)()
} else {
*req = Some(FetchReq {
url: url.clone(),
query: {
let query_res = create_query(fetch_string, QueryOptions::default())
.use_query(move || url.clone());
Box::new(move || query_res.data.try_get().flatten())
},
tries: 0,
});
None
}
})
}

async fn fetch_string(s: String) -> Result<String, String> {
fetch(&s).await
}

pub async fn fetch(url: &str) -> Result<String, String> {
let mut opts = RequestInit::new();
opts.method("GET");
Expand Down
2 changes: 0 additions & 2 deletions site/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use instant::Duration;
use js_sys::Date;
use leptos::*;
use leptos_meta::*;
use leptos_query::provide_query_client;
use leptos_router::*;
use uiua::{
lsp::{BindingDocs, BindingDocsKind},
Expand All @@ -45,7 +44,6 @@ pub fn main() {
pub fn Site() -> impl IntoView {
use Primitive::*;
provide_meta_context();
provide_query_client();

// Choose a subtitle
let subtitles_common = [
Expand Down

0 comments on commit b7402a2

Please sign in to comment.