Further, simplify schedule api

This commit is contained in:
Maximilian Ammann 2022-04-05 15:21:32 +02:00
parent 197a3a3662
commit 315fd811d9
3 changed files with 35 additions and 23 deletions

View File

@ -45,27 +45,33 @@ impl Default for ScheduleMethod {
}
impl ScheduleMethod {
pub fn schedule_fn<T>(
#[cfg(target_arch = "wasm32")]
pub fn schedule<T>(
&self,
future_factory: impl (FnOnce() -> T) + Send + 'static,
scheduler: &IOScheduler,
future_factory: impl (FnOnce(ThreadLocalTessellatorState) -> T) + Send + 'static,
) -> Result<(), Error>
where
T: Future<Output = ()> + 'static,
{
match self {
#[cfg(target_arch = "wasm32")]
ScheduleMethod::WebWorkerPool(method) => Ok(method.schedule(future_factory)),
ScheduleMethod::WebWorkerPool(method) => Ok(method.schedule(scheduler, future_factory)),
_ => Err(Error::Schedule),
}
}
pub fn schedule<T>(&self, future: T) -> Result<(), Error>
#[cfg(not(target_arch = "wasm32"))]
pub fn schedule<T>(
&self,
scheduler: &IOScheduler,
future_factory: impl (FnOnce(ThreadLocalTessellatorState) -> T) + Send + 'static,
) -> Result<(), Error>
where
T: Future<Output = ()> + Send + 'static,
T: std::future::Future + Send + 'static,
T::Output: Send + 'static,
{
match self {
#[cfg(not(target_arch = "wasm32"))]
ScheduleMethod::Tokio(method) => Ok(method.schedule(future)),
ScheduleMethod::Tokio(method) => Ok(method.schedule(scheduler, future_factory)),
_ => Err(Error::Schedule),
}
}
@ -313,24 +319,23 @@ impl IOScheduler {
}*/
{
let state = self.new_tessellator_state();
let client = SourceClient::Http(HttpSourceClient::new());
let copied_coords = *coords;
let future_fn = move || async move {
let future_fn = move |thread_local_state: ThreadLocalTessellatorState| async move {
if let Ok(data) = client.fetch(&copied_coords).await {
state
thread_local_state
.process_tile(request_id, data.into_boxed_slice())
.unwrap();
} else {
state.tile_unavailable(request_id).unwrap();
thread_local_state.tile_unavailable(request_id).unwrap();
}
};
#[cfg(target_arch = "wasm32")]
self.schedule_method.schedule_fn(future_fn);
self.schedule_method.schedule(self, future_fn);
#[cfg(not(target_arch = "wasm32"))]
self.schedule_method.schedule(future_fn());
self.schedule_method.schedule(self, future_fn);
}
}
}

View File

@ -1,4 +1,3 @@
use reqwest::{Client, StatusCode};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_middleware_cache::managers::CACacheManager;
@ -7,7 +6,7 @@ use std::future::Future;
use crate::coords::TileCoords;
use crate::error::Error;
use crate::io::scheduler::IOScheduler;
use crate::io::scheduler::{IOScheduler, ThreadLocalTessellatorState};
use crate::io::TileRequestID;
pub struct TokioScheduleMethod;
@ -17,10 +16,14 @@ impl TokioScheduleMethod {
Self {}
}
pub fn schedule<T>(&self, future: T)
where
T: Future<Output = ()> + Send + 'static,
pub fn schedule<T>(
&self,
scheduler: &IOScheduler,
future_factory: impl (FnOnce(ThreadLocalTessellatorState) -> T) + Send + 'static,
) where
T: std::future::Future + Send + 'static,
T::Output: Send + 'static,
{
tokio::task::spawn(future);
tokio::task::spawn(future_factory(scheduler.new_tessellator_state()));
}
}

View File

@ -38,15 +38,19 @@ impl WebWorkerPoolScheduleMethod {
}
}
pub fn schedule<T>(&self, future_factory: impl (FnOnce() -> T) + Send + 'static)
where
pub fn schedule<T>(
&self,
scheduler: &IOScheduler,
future_factory: impl (FnOnce(ThreadLocalTessellatorState) -> T) + Send + 'static,
) where
T: std::future::Future + 'static,
T::Output: Send + 'static,
{
let state = scheduler.new_tessellator_state();
self.pool
.run(move || {
wasm_bindgen_futures::future_to_promise(async move {
future_factory().await;
future_factory(state).await;
Ok(JsValue::undefined())
})
})