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

@ -221,16 +221,26 @@ impl Page {
}
}
}
Request::Search(ref query) => {
// try autocomplete scheme prefix @TODO optional resolve timeout
if self.navigation.request.to_gemini(500).is_none() {
// make search request to default provider @TODO optional
self.navigation.request.widget.entry.set_text(&format!(
"gemini://tlgs.one/search?{}",
Uri::escape_string(query, None, false)
));
}
self.load(true)
Request::Search(query) => {
// try autocomplete scheme and request it on successful resolve
// otherwise make search request @TODO optional search provider
self.navigation
.request
.to_gemini_async(500, Some(&self.client.cancellable()), {
let tab_action = self.tab_action.clone();
move |result| {
tab_action.load.activate(
Some(&match result {
Some(url) => url,
None => gformat!(
"gemini://tlgs.one/search?{}",
Uri::escape_string(&query, None, false)
),
}),
true,
)
}
});
}
};
}

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
}