mirror of
https://github.com/aljazceru/notedeck.git
synced 2025-12-18 17:14:21 +01:00
images: make promise payload optional to take easily
Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
@@ -17,7 +17,7 @@ use std::path::{self};
|
|||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use tracing::warn;
|
use tracing::warn;
|
||||||
|
|
||||||
pub type MediaCacheValue = Promise<Result<TexturedImage>>;
|
pub type MediaCacheValue = Promise<Option<Result<TexturedImage>>>;
|
||||||
pub type MediaCacheMap = HashMap<String, MediaCacheValue>;
|
pub type MediaCacheMap = HashMap<String, MediaCacheValue>;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
|||||||
@@ -178,14 +178,14 @@ fn fetch_img_from_disk(
|
|||||||
url: &str,
|
url: &str,
|
||||||
path: &path::Path,
|
path: &path::Path,
|
||||||
cache_type: MediaCacheType,
|
cache_type: MediaCacheType,
|
||||||
) -> Promise<Result<TexturedImage, notedeck::Error>> {
|
) -> Promise<Option<Result<TexturedImage, notedeck::Error>>> {
|
||||||
let ctx = ctx.clone();
|
let ctx = ctx.clone();
|
||||||
let url = url.to_owned();
|
let url = url.to_owned();
|
||||||
let path = path.to_owned();
|
let path = path.to_owned();
|
||||||
|
|
||||||
Promise::spawn_async(
|
Promise::spawn_async(async move {
|
||||||
async move { async_fetch_img_from_disk(ctx, url, &path, cache_type).await },
|
Some(async_fetch_img_from_disk(ctx, url, &path, cache_type).await)
|
||||||
)
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn async_fetch_img_from_disk(
|
async fn async_fetch_img_from_disk(
|
||||||
@@ -361,7 +361,7 @@ pub fn fetch_img(
|
|||||||
url: &str,
|
url: &str,
|
||||||
imgtyp: ImageType,
|
imgtyp: ImageType,
|
||||||
cache_type: MediaCacheType,
|
cache_type: MediaCacheType,
|
||||||
) -> Promise<Result<TexturedImage, notedeck::Error>> {
|
) -> Promise<Option<Result<TexturedImage, notedeck::Error>>> {
|
||||||
let key = MediaCache::key(url);
|
let key = MediaCache::key(url);
|
||||||
let path = img_cache_path.join(key);
|
let path = img_cache_path.join(key);
|
||||||
|
|
||||||
@@ -380,7 +380,7 @@ fn fetch_img_from_net(
|
|||||||
url: &str,
|
url: &str,
|
||||||
imgtyp: ImageType,
|
imgtyp: ImageType,
|
||||||
cache_type: MediaCacheType,
|
cache_type: MediaCacheType,
|
||||||
) -> Promise<Result<TexturedImage, notedeck::Error>> {
|
) -> Promise<Option<Result<TexturedImage, notedeck::Error>>> {
|
||||||
let (sender, promise) = Promise::new();
|
let (sender, promise) = Promise::new();
|
||||||
let request = ehttp::Request::get(url);
|
let request = ehttp::Request::get(url);
|
||||||
let ctx = ctx.clone();
|
let ctx = ctx.clone();
|
||||||
@@ -417,7 +417,7 @@ fn fetch_img_from_net(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
sender.send(handle); // send the results back to the UI thread.
|
sender.send(Some(handle)); // send the results back to the UI thread.
|
||||||
ctx.request_repaint();
|
ctx.request_repaint();
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -476,7 +476,7 @@ fn render_media_cache(
|
|||||||
.show(ui, |ui| {
|
.show(ui, |ui| {
|
||||||
match cache.map_mut().get_mut(url).and_then(|p| p.ready_mut()) {
|
match cache.map_mut().get_mut(url).and_then(|p| p.ready_mut()) {
|
||||||
None => show_waiting(ui),
|
None => show_waiting(ui),
|
||||||
Some(Err(err)) => {
|
Some(Some(Err(err))) => {
|
||||||
let err = err.to_string();
|
let err = err.to_string();
|
||||||
let no_pfp = crate::images::fetch_img(
|
let no_pfp = crate::images::fetch_img(
|
||||||
&cache.cache_dir,
|
&cache.cache_dir,
|
||||||
@@ -488,7 +488,12 @@ fn render_media_cache(
|
|||||||
cache.map_mut().insert(url.to_owned(), no_pfp);
|
cache.map_mut().insert(url.to_owned(), no_pfp);
|
||||||
show_error(ui, err)
|
show_error(ui, err)
|
||||||
}
|
}
|
||||||
Some(Ok(renderable_media)) => show_success(ui, url, renderable_media, gif_states),
|
Some(Some(Ok(renderable_media))) => {
|
||||||
|
show_success(ui, url, renderable_media, gif_states)
|
||||||
|
}
|
||||||
|
Some(None) => {
|
||||||
|
tracing::error!("Promise already taken");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.response
|
.response
|
||||||
|
|||||||
Reference in New Issue
Block a user