Detach Background Tasks Directly

This commit is contained in:
2026-09-10 07:26:00 +07:00
parent 5f37f2ff2a
commit 3cfdffdf82
7 changed files with 144 additions and 192 deletions
+12 -20
View File
@@ -75,7 +75,6 @@ pub struct ProfileStore {
seen: RefCell<HashSet<PublicKey>>,
/// Sender for queuing fetch requests, batched by a background task.
sender: Sender<PublicKey>,
tasks: Vec<Task<Result<(), Error>>>,
_subscription: Subscription,
}
@@ -114,17 +113,15 @@ impl ProfileStore {
let (sender, receiver) = flume::unbounded::<PublicKey>();
let entity = cx.entity().downgrade();
let mut tasks = Vec::new();
tasks.push(cx.spawn(async move |_this, cx| {
cx.spawn(async move |_this, cx| {
Self::handle_requests(entity, &client, &receiver, cx).await
}));
})
.detach();
let mut store = Self {
profiles: HashMap::new(),
seen: RefCell::new(HashSet::new()),
sender,
tasks,
_subscription: subscription,
};
@@ -132,14 +129,6 @@ impl ProfileStore {
store
}
/// Track a spawned task, pruning finished tasks first.
///
/// Keeps the store's task list bounded by the number of in-flight tasks.
fn push_task(&mut self, task: Task<Result<(), Error>>) {
self.tasks.retain(|task| !task.is_ready());
self.tasks.push(task);
}
/// Get a profile.
///
/// Returns a placeholder with default metadata. Queues a fetch when the profile is not cached yet.
@@ -181,7 +170,7 @@ impl ProfileStore {
Ok::<_, Error>(profiles)
});
self.push_task(cx.spawn(async move |this, cx| {
let task: Task<Result<(), Error>> = cx.spawn(async move |this, cx| {
let profiles = work.await?;
this.update(cx, |this, cx| {
@@ -192,7 +181,8 @@ impl ProfileStore {
})?;
Ok(())
}));
});
task.detach();
}
/// Re-read the latest metadata of an author from the local database.
@@ -217,7 +207,7 @@ impl ProfileStore {
Ok::<_, Error>(profile)
});
self.push_task(cx.spawn(async move |this, cx| {
let task: Task<Result<(), Error>> = cx.spawn(async move |this, cx| {
let profile = work.await?;
this.update(cx, |this, cx| {
@@ -228,7 +218,8 @@ impl ProfileStore {
})?;
Ok(())
}));
});
task.detach();
}
/// Re-read the latest metadata of every requested author from the local database.
@@ -273,7 +264,7 @@ impl ProfileStore {
Ok::<_, Error>(profiles)
});
self.push_task(cx.spawn(async move |this, cx| {
let task: Task<Result<(), Error>> = cx.spawn(async move |this, cx| {
let profiles = work.await?;
this.update(cx, |this, cx| {
@@ -284,7 +275,8 @@ impl ProfileStore {
})?;
Ok(())
}));
});
task.detach();
}
/// Sync metadata for requested authors in batches, debounced to collect requests.