Process remote worktree updates in the background
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
This commit is contained in:
co-authored by
Max Brunsfeld
parent
036dcd50fd
commit
73db910a95
+47
-19
@@ -154,7 +154,7 @@ impl Worktree {
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
let worktree = cx.update(|cx| {
|
let worktree = cx.update(|cx| {
|
||||||
cx.add_model(|cx| {
|
cx.add_model(|cx: &mut ModelContext<Worktree>| {
|
||||||
let snapshot = Snapshot {
|
let snapshot = Snapshot {
|
||||||
id: cx.model_id(),
|
id: cx.model_id(),
|
||||||
scan_id: 0,
|
scan_id: 0,
|
||||||
@@ -168,10 +168,39 @@ impl Worktree {
|
|||||||
next_entry_id: Default::default(),
|
next_entry_id: Default::default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let (updates_tx, mut updates_rx) = postage::mpsc::channel(64);
|
||||||
|
let (mut snapshot_tx, mut snapshot_rx) =
|
||||||
|
postage::watch::channel_with(snapshot.clone());
|
||||||
|
|
||||||
|
cx.background()
|
||||||
|
.spawn(async move {
|
||||||
|
while let Some(update) = updates_rx.recv().await {
|
||||||
|
let mut snapshot = snapshot_tx.borrow().clone();
|
||||||
|
if let Err(error) = snapshot.apply_update(update) {
|
||||||
|
log::error!("error applying worktree update: {}", error);
|
||||||
|
}
|
||||||
|
*snapshot_tx.borrow_mut() = snapshot;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
|
cx.spawn(|this, mut cx| async move {
|
||||||
|
while let Some(snapshot) = snapshot_rx.recv().await {
|
||||||
|
this.update(&mut cx, |this, cx| {
|
||||||
|
let this = this.as_remote_mut().unwrap();
|
||||||
|
this.snapshot = snapshot;
|
||||||
|
cx.notify();
|
||||||
|
this.update_open_buffers(cx);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
|
||||||
Worktree::Remote(RemoteWorktree {
|
Worktree::Remote(RemoteWorktree {
|
||||||
remote_id: id,
|
remote_id: id,
|
||||||
replica_id,
|
replica_id,
|
||||||
snapshot,
|
snapshot,
|
||||||
|
updates_tx,
|
||||||
rpc: rpc.clone(),
|
rpc: rpc.clone(),
|
||||||
open_buffers: Default::default(),
|
open_buffers: Default::default(),
|
||||||
peers: peers
|
peers: peers
|
||||||
@@ -218,7 +247,7 @@ impl Worktree {
|
|||||||
pub fn snapshot(&self) -> Snapshot {
|
pub fn snapshot(&self) -> Snapshot {
|
||||||
match self {
|
match self {
|
||||||
Worktree::Local(worktree) => worktree.snapshot(),
|
Worktree::Local(worktree) => worktree.snapshot(),
|
||||||
Worktree::Remote(worktree) => worktree.snapshot.clone(),
|
Worktree::Remote(worktree) => worktree.snapshot(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -861,6 +890,7 @@ pub struct RemoteWorktree {
|
|||||||
remote_id: u64,
|
remote_id: u64,
|
||||||
snapshot: Snapshot,
|
snapshot: Snapshot,
|
||||||
rpc: rpc::Client,
|
rpc: rpc::Client,
|
||||||
|
updates_tx: postage::mpsc::Sender<proto::UpdateWorktree>,
|
||||||
replica_id: ReplicaId,
|
replica_id: ReplicaId,
|
||||||
open_buffers: HashMap<usize, WeakModelHandle<Buffer>>,
|
open_buffers: HashMap<usize, WeakModelHandle<Buffer>>,
|
||||||
peers: HashMap<PeerId, ReplicaId>,
|
peers: HashMap<PeerId, ReplicaId>,
|
||||||
@@ -923,6 +953,10 @@ impl RemoteWorktree {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn snapshot(&self) -> Snapshot {
|
||||||
|
self.snapshot.clone()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn add_peer(
|
pub fn add_peer(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::AddPeer>,
|
envelope: TypedEnvelope<proto::AddPeer>,
|
||||||
@@ -954,17 +988,6 @@ impl RemoteWorktree {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(
|
|
||||||
&mut self,
|
|
||||||
envelope: TypedEnvelope<proto::UpdateWorktree>,
|
|
||||||
cx: &mut ModelContext<Worktree>,
|
|
||||||
) -> Result<()> {
|
|
||||||
self.snapshot.apply_update(envelope.payload)?;
|
|
||||||
self.update_open_buffers(cx);
|
|
||||||
cx.notify();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update_open_buffers(&mut self, cx: &mut ModelContext<Worktree>) {
|
fn update_open_buffers(&mut self, cx: &mut ModelContext<Worktree>) {
|
||||||
let mut buffers_to_delete = Vec::new();
|
let mut buffers_to_delete = Vec::new();
|
||||||
for (buffer_id, buffer) in &self.open_buffers {
|
for (buffer_id, buffer) in &self.open_buffers {
|
||||||
@@ -2305,17 +2328,22 @@ mod remote {
|
|||||||
.read()
|
.read()
|
||||||
.await
|
.await
|
||||||
.shared_worktree(envelope.payload.worktree_id, cx)?
|
.shared_worktree(envelope.payload.worktree_id, cx)?
|
||||||
.update(cx, |worktree, cx| {
|
.update(cx, |worktree, _| {
|
||||||
if let Some(worktree) = worktree.as_remote_mut() {
|
if let Some(worktree) = worktree.as_remote_mut() {
|
||||||
worktree.update(envelope, cx)?;
|
let mut tx = worktree.updates_tx.clone();
|
||||||
|
Ok(async move {
|
||||||
|
tx.send(envelope.payload)
|
||||||
|
.await
|
||||||
|
.expect("receiver runs to completion");
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
log::error!(
|
Err(anyhow!(
|
||||||
"invalid update message for local worktree {}",
|
"invalid update message for local worktree {}",
|
||||||
envelope.payload.worktree_id
|
envelope.payload.worktree_id
|
||||||
);
|
))
|
||||||
}
|
}
|
||||||
Result::<_, anyhow::Error>::Ok(())
|
})?
|
||||||
})?;
|
.await;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user