Skip to content

Commit 1540df2

Browse files
committed
feat: add start_request function
1 parent 4a0baec commit 1540df2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/data_state.rs

+31
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,37 @@ pub enum DataState<T, E: ErrorBounds = anyhow::Error> {
5454
}
5555

5656
impl<T, E: ErrorBounds> DataState<T, E> {
57+
#[cfg(feature = "egui")]
58+
/// Calls [Self::start_request] and adds a spinner if progress can be made
59+
#[must_use]
60+
pub fn egui_start_request<F>(&mut self, ui: &mut egui::Ui, fetch_fn: F) -> CanMakeProgress
61+
where
62+
F: FnOnce() -> Awaiting<T, E>,
63+
{
64+
let result = self.start_request(fetch_fn);
65+
if result.is_able_to_make_progress() {
66+
ui.spinner();
67+
}
68+
result
69+
}
70+
71+
/// Starts a new request. Only intended to be on [Self::None] and if state
72+
/// is any other value it returns
73+
/// [CanMakeProgress::UnableToMakeProgress]
74+
#[must_use]
75+
pub fn start_request<F>(&mut self, fetch_fn: F) -> CanMakeProgress
76+
where
77+
F: FnOnce() -> Awaiting<T, E>,
78+
{
79+
if self.is_none() {
80+
let result = self.get(fetch_fn);
81+
assert!(result.is_able_to_make_progress());
82+
result
83+
} else {
84+
CanMakeProgress::UnableToMakeProgress
85+
}
86+
}
87+
5788
#[cfg(feature = "egui")]
5889
/// Attempts to load the data and displays appropriate UI if applicable.
5990
/// Some branches lead to no UI being displayed, in particular when the data

0 commit comments

Comments
 (0)