Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions crates/ui/src/repository/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ impl<T> LoadState<T> {
matches!(self, Self::Loading)
}

pub fn start_reload(&mut self) {
if self.ready().is_none() {
*self = Self::Loading;
}
}

pub fn error(&self) -> Option<&str> {
match self {
Self::Failed(message) => Some(message),
Expand Down Expand Up @@ -297,6 +303,34 @@ mod tests {
assert_eq!(index.tags.len(), 1);
}

#[test]
fn a_reload_keeps_a_value_that_is_already_loaded() {
let mut state: LoadState<u8> = LoadState::Ready(1);
state.start_reload();
assert_eq!(state, LoadState::Ready(1));
}

#[test]
fn a_first_load_has_nothing_to_keep() {
let mut state: LoadState<u8> = LoadState::Idle;
state.start_reload();
assert_eq!(state, LoadState::Loading);
}

#[test]
fn a_reload_after_a_failure_has_nothing_to_keep() {
let mut state: LoadState<u8> = LoadState::Failed("boom".into());
state.start_reload();
assert_eq!(state, LoadState::Loading);
}

#[test]
fn a_reload_that_overtakes_another_stays_loading() {
let mut state: LoadState<u8> = LoadState::Loading;
state.start_reload();
assert_eq!(state, LoadState::Loading);
}

#[test]
fn load_state_exposes_only_the_matching_accessor() {
let ready: LoadState<u8> = LoadState::Ready(1);
Expand Down
6 changes: 3 additions & 3 deletions crates/ui/src/repository/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl RepositoryState {
/// immediately (`gpui::Task` semantics) — a later call always wins over an earlier one
/// that has not yet applied its result.
fn reload_head(&mut self, cx: &mut Context<Self>) {
self.head = LoadState::Loading;
self.head.start_reload();
cx.notify();
let path = self.path.clone();
self.head_task = Some(cx.spawn(async move |this, cx| {
Expand All @@ -172,7 +172,7 @@ impl RepositoryState {
}

fn reload_references(&mut self, cx: &mut Context<Self>) {
self.references = LoadState::Loading;
self.references.start_reload();
cx.notify();
let path = self.path.clone();
self.references_task = Some(cx.spawn(async move |this, cx| {
Expand All @@ -187,7 +187,7 @@ impl RepositoryState {
/// The graph layout is computed in the same background closure as the walk that
/// produces the commits it lays out, never after the result reaches the foreground.
fn reload_history(&mut self, cx: &mut Context<Self>) {
self.history = LoadState::Loading;
self.history.start_reload();
cx.notify();
let path = self.path.clone();
let scope = self.filter.scope.clone();
Expand Down
Loading