Avoid circular model update in save_buffer handler

This commit is contained in:
Max Brunsfeld 2021-07-02 15:53:45 -07:00
parent 86b9ab4505
commit a7f812e059

View file

@ -59,6 +59,7 @@ pub fn init(cx: &mut MutableAppContext, rpc: rpc::Client) {
rpc.on_message(remote::close_buffer, cx); rpc.on_message(remote::close_buffer, cx);
rpc.on_message(remote::update_buffer, cx); rpc.on_message(remote::update_buffer, cx);
rpc.on_message(remote::buffer_saved, cx); rpc.on_message(remote::buffer_saved, cx);
rpc.on_message(remote::save_buffer, cx);
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -712,20 +713,6 @@ impl LocalWorktree {
}) })
} }
pub fn save_remote_buffer(
&self,
envelope: &TypedEnvelope<proto::SaveBuffer>,
cx: &mut ModelContext<Worktree>,
) -> Result<Task<Result<time::Global>>> {
let sender_id = envelope.original_sender_id()?;
let buffer = self
.shared_buffers
.get_mut(&sender_id)
.and_then(|shared_buffers| shared_buffers.get(&envelope.payload.buffer_id))
.ok_or_else(|| anyhow!("unknown buffer id {}", envelope.payload.buffer_id))?;
buffer.update(cx, |buffer, cx| buffer.save(cx))
}
pub fn save_buffer_as( pub fn save_buffer_as(
&self, &self,
buffer: ModelHandle<Buffer>, buffer: ModelHandle<Buffer>,
@ -2187,15 +2174,18 @@ mod remote {
rpc: &rpc::Client, rpc: &rpc::Client,
cx: &mut AsyncAppContext, cx: &mut AsyncAppContext,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mut state = rpc.state.lock().await; let state = rpc.state.read().await;
let worktree = state.shared_worktree(envelope.payload.worktree_id, cx)?; let worktree = state.shared_worktree(envelope.payload.worktree_id, cx)?;
let version = worktree let sender_id = envelope.original_sender_id()?;
.update(cx, |tree, cx| { let buffer = worktree.read_with(cx, |tree, _| {
tree.as_local_mut() tree.as_local()
.unwrap() .unwrap()
.save_remote_buffer(&envelope, cx) .shared_buffers
})? .get(&sender_id)
.await?; .and_then(|shared_buffers| shared_buffers.get(&envelope.payload.buffer_id).cloned())
.ok_or_else(|| anyhow!("unknown buffer id {}", envelope.payload.buffer_id))
})?;
let version = buffer.update(cx, |buffer, cx| buffer.save(cx))?.await?;
rpc.respond( rpc.respond(
envelope.receipt(), envelope.receipt(),
proto::BufferSaved { proto::BufferSaved {