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) => { Request::Search(query) => {
// try autocomplete scheme prefix @TODO optional resolve timeout // try autocomplete scheme and request it on successful resolve
if self.navigation.request.to_gemini(500).is_none() { // otherwise make search request @TODO optional search provider
// make search request to default provider @TODO optional self.navigation
self.navigation.request.widget.entry.set_text(&format!( .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?{}", "gemini://tlgs.one/search?{}",
Uri::escape_string(query, None, false) Uri::escape_string(&query, None, false)
)); ),
}),
true,
)
} }
self.load(true) });
} }
}; };
} }

View File

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