diff --git a/Cargo.toml b/Cargo.toml index 33740bfd..6d2a4a12 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,6 @@ console_error_panic_hook = "0.1" winit = { version = "0.26", default-features = false } web-sys = { version = "0.3", features = [ "Window", - "Worker", "WorkerOptions", "WorkerType", "MessageEvent", "Headers", "WorkerGlobalScope", "Request", "RequestInit", "RequestMode", "Response", ] } diff --git a/src/io/mod.rs b/src/io/mod.rs index 2a6b4682..1e8b154f 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -8,10 +8,15 @@ pub mod static_tile_fetcher; pub mod web_tile_fetcher; pub mod worker_loop; +pub struct HttpFetcherConfig { + /// Under which path should we cache requests. + pub cache_path: String, +} + #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(not(target_arch = "wasm32"), async_trait)] pub trait HttpFetcher { - fn new() -> Self; + fn new(config: HttpFetcherConfig) -> Self; async fn fetch(&self, url: &str) -> Result, Error>; } @@ -19,7 +24,7 @@ pub trait HttpFetcher { #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(not(target_arch = "wasm32"), async_trait)] pub trait TileFetcher { - fn new() -> Self; + fn new(config: HttpFetcherConfig) -> Self; async fn fetch_tile(&self, coords: &TileCoords) -> Result, Error>; fn sync_fetch_tile(&self, coords: &TileCoords) -> Result, Error>; diff --git a/src/io/static_tile_fetcher.rs b/src/io/static_tile_fetcher.rs index 38eb3725..d7803e9d 100644 --- a/src/io/static_tile_fetcher.rs +++ b/src/io/static_tile_fetcher.rs @@ -7,6 +7,7 @@ use log::error; use crate::coords::TileCoords; use crate::error::Error; +use crate::io::HttpFetcherConfig; use super::TileFetcher; @@ -23,7 +24,7 @@ impl StaticTileFetcher { #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(not(target_arch = "wasm32"), async_trait)] impl TileFetcher for StaticTileFetcher { - fn new() -> Self { + fn new(_config: HttpFetcherConfig) -> Self { Self {} } diff --git a/src/io/web_tile_fetcher.rs b/src/io/web_tile_fetcher.rs index bbb1197b..c39ee02b 100644 --- a/src/io/web_tile_fetcher.rs +++ b/src/io/web_tile_fetcher.rs @@ -1,6 +1,6 @@ use crate::coords::TileCoords; use crate::error::Error; -use crate::io::{HttpFetcher, TileFetcher}; +use crate::io::{HttpFetcher, HttpFetcherConfig, TileFetcher}; use crate::platform::PlatformHttpFetcher; use async_trait::async_trait; @@ -11,9 +11,9 @@ pub struct WebTileFetcher { #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(not(target_arch = "wasm32"), async_trait)] impl TileFetcher for WebTileFetcher { - fn new() -> Self { + fn new(config: HttpFetcherConfig) -> Self { Self { - http_fetcher: PlatformHttpFetcher::new(), + http_fetcher: PlatformHttpFetcher::new(config), } } diff --git a/src/io/worker_loop.rs b/src/io/worker_loop.rs index 52ad7ffe..9551ba18 100644 --- a/src/io/worker_loop.rs +++ b/src/io/worker_loop.rs @@ -8,7 +8,7 @@ use crate::coords::TileCoords; use vector_tile::parse_tile_bytes; use crate::io::web_tile_fetcher::WebTileFetcher; -use crate::io::TileFetcher; +use crate::io::{HttpFetcherConfig, TileFetcher}; use crate::render::ShaderVertex; use crate::tesselation::{IndexDataType, OverAlignedVertexBuffer, Tesselated}; @@ -49,7 +49,9 @@ impl WorkerLoop { } pub async fn run_loop(&mut self) { - let fetcher = WebTileFetcher::new(); + let fetcher = WebTileFetcher::new(HttpFetcherConfig { + cache_path: "/tmp/mapr-cache".to_string(), + }); // let fetcher = StaticTileFetcher::new(); let mut current_id = 0; diff --git a/src/platform/noweb/mod.rs b/src/platform/noweb/mod.rs index ad59a903..45ab9abc 100644 --- a/src/platform/noweb/mod.rs +++ b/src/platform/noweb/mod.rs @@ -7,7 +7,7 @@ use reqwest_middleware_cache::managers::CACacheManager; use reqwest_middleware_cache::{Cache, CacheMode}; use crate::error::Error; -use crate::io::HttpFetcher; +use crate::io::{HttpFetcher, HttpFetcherConfig}; impl From for Error { fn from(err: reqwest::Error) -> Self { @@ -28,14 +28,16 @@ pub struct PlatformHttpFetcher { #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(not(target_arch = "wasm32"), async_trait)] impl HttpFetcher for PlatformHttpFetcher { - fn new() -> Self { + fn new(config: HttpFetcherConfig) -> Self { let mut builder = ClientBuilder::new(Client::new()); // FIXME: Cache only works on desktop so far if cfg!(not(any(target_os = "android", target_arch = "aarch64"))) { builder = builder.with(Cache { mode: CacheMode::Default, - cache_manager: CACacheManager::default(), + cache_manager: CACacheManager { + path: config.cache_path, + }, }); } diff --git a/src/platform/web/http_fetcher.rs b/src/platform/web/http_fetcher.rs index 010465b6..074f490a 100644 --- a/src/platform/web/http_fetcher.rs +++ b/src/platform/web/http_fetcher.rs @@ -1,5 +1,5 @@ use crate::error::Error; -use crate::io::HttpFetcher; +use crate::io::{HttpFetcher, HttpFetcherConfig}; use async_trait::async_trait; use js_sys::ArrayBuffer; use js_sys::Uint8Array; @@ -19,7 +19,7 @@ pub struct PlatformHttpFetcher; #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(not(target_arch = "wasm32"), async_trait)] impl HttpFetcher for PlatformHttpFetcher { - fn new() -> Self { + fn new(_config: HttpFetcherConfig) -> Self { Self {} }