make resolve request async

This commit is contained in:
yggverse 2024-12-12 20:51:00 +02:00
parent 04ca502bf2
commit 43064aa1b6
2 changed files with 55 additions and 23 deletions

View file

@ -99,11 +99,26 @@ impl Request {
self.widget.entry.set_text(&self.source());
}
pub fn to_gemini(&self, resolver_timeout: u32) -> Option<GString> {
self.gemini(resolver_timeout).and_then(|url| {
self.widget.entry.set_text(&url);
Some(url)
})
/// Asynchronously try replace `Self` entry value with valid, resolvable Gemini request
/// * callback with `None` if current value does not compatible with Gemini scheme
pub fn to_gemini_async(
&self,
resolver_timeout: u32,
cancellable: Option<&Cancellable>,
callback: impl FnOnce(Option<GString>) + 'static,
) {
self.gemini_async(resolver_timeout, cancellable, {
let entry = self.widget.entry.clone();
move |result| {
callback(match result {
Some(url) => {
entry.set_text(&url);
Some(url)
}
None => None,
})
}
});
}
// Getters
@ -145,7 +160,14 @@ impl Request {
gformat!("source:{}", self.strip_prefix())
}
pub fn gemini(&self, resolver_timeout: u32) -> Option<GString> {
/// Asynchronously get valid, resolvable Gemini request for current `Self` entry value
/// * callback with `None` if current value does not compatible with Gemini scheme
pub fn gemini_async(
&self,
resolver_timeout: u32,
cancellable: Option<&Cancellable>,
callback: impl FnOnce(Option<GString>) + 'static,
) -> Option<GString> {
// suggest scheme
let url = gformat!("gemini://{}", self.strip_prefix().trim());
@ -156,13 +178,13 @@ impl Request {
// is connectable
if let Ok(connectable) = NetworkAddress::parse_uri(&url, 1965) {
// is resolvable @TODO async
if resolver
.lookup_by_name(&connectable.hostname(), Cancellable::NONE)
.is_ok()
{
return Some(url);
}
// is resolvable
resolver.lookup_by_name_async(&connectable.hostname(), cancellable, move |result| {
callback(match result {
Ok(_) => Some(url),
Err(_) => None,
})
});
}
None
}