2023-08-21 23:30:57 +00:00
|
|
|
use crate::ChannelId;
|
|
|
|
use anyhow::Result;
|
|
|
|
use client::Client;
|
2023-08-22 00:53:37 +00:00
|
|
|
use gpui::{AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, Task};
|
|
|
|
use rpc::{proto, TypedEnvelope};
|
2023-08-21 23:30:57 +00:00
|
|
|
use std::sync::Arc;
|
2023-08-22 00:53:37 +00:00
|
|
|
use util::ResultExt;
|
2023-08-21 23:30:57 +00:00
|
|
|
|
|
|
|
// Open the channel document
|
|
|
|
// ChannelDocumentView { ChannelDocument, Editor } -> On clone, clones internal ChannelDocument handle, instantiates new editor
|
|
|
|
// Produces a view which is: (ChannelDocument, Editor), ChannelDocument manages subscriptions
|
|
|
|
// ChannelDocuments -> Buffers -> Editor with that buffer
|
|
|
|
|
|
|
|
// ChannelDocuments {
|
|
|
|
// ChannleBuffers: HashMap<bufferId, ModelHandle<language::Buffer>>
|
|
|
|
// }
|
|
|
|
|
2023-08-22 00:53:37 +00:00
|
|
|
type BufferId = u64;
|
|
|
|
|
2023-08-21 23:30:57 +00:00
|
|
|
pub struct ChannelBuffer {
|
|
|
|
channel_id: ChannelId,
|
2023-08-22 00:53:37 +00:00
|
|
|
buffer_id: BufferId,
|
|
|
|
buffer: ModelHandle<language::Buffer>,
|
2023-08-21 23:30:57 +00:00
|
|
|
client: Arc<Client>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Entity for ChannelBuffer {
|
|
|
|
type Event = ();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChannelBuffer {
|
|
|
|
pub fn for_channel(
|
|
|
|
channel_id: ChannelId,
|
|
|
|
client: Arc<Client>,
|
2023-08-22 00:53:37 +00:00
|
|
|
cx: &mut AppContext,
|
|
|
|
) -> Task<Result<ModelHandle<Self>>> {
|
|
|
|
cx.spawn(|mut cx| async move {
|
|
|
|
let response = client
|
|
|
|
.request(proto::OpenChannelBuffer { channel_id })
|
|
|
|
.await?;
|
2023-08-21 23:30:57 +00:00
|
|
|
|
2023-08-22 00:53:37 +00:00
|
|
|
let base_text = response.base_text;
|
|
|
|
let operations = response
|
|
|
|
.operations
|
|
|
|
.into_iter()
|
|
|
|
.map(language::proto::deserialize_operation)
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
let buffer_id = response.buffer_id;
|
2023-08-21 23:30:57 +00:00
|
|
|
|
2023-08-22 00:53:37 +00:00
|
|
|
let buffer = cx.add_model(|cx| language::Buffer::new(0, base_text, cx));
|
|
|
|
buffer.update(&mut cx, |buffer, cx| buffer.apply_ops(operations, cx))?;
|
2023-08-21 23:30:57 +00:00
|
|
|
|
2023-08-22 00:53:37 +00:00
|
|
|
anyhow::Ok(cx.add_model(|cx| {
|
|
|
|
cx.subscribe(&buffer, Self::on_buffer_update).detach();
|
|
|
|
client.add_model_message_handler(Self::handle_update_channel_buffer);
|
|
|
|
Self {
|
|
|
|
buffer_id,
|
|
|
|
buffer,
|
|
|
|
client,
|
|
|
|
channel_id,
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handle_update_channel_buffer(
|
|
|
|
this: ModelHandle<Self>,
|
|
|
|
update_channel_buffer: TypedEnvelope<proto::UpdateChannelBuffer>,
|
|
|
|
_: Arc<Client>,
|
|
|
|
mut cx: AsyncAppContext,
|
|
|
|
) -> Result<()> {
|
|
|
|
let ops = update_channel_buffer
|
|
|
|
.payload
|
|
|
|
.operations
|
|
|
|
.into_iter()
|
|
|
|
.map(language::proto::deserialize_operation)
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
2023-08-21 23:30:57 +00:00
|
|
|
|
2023-08-22 00:53:37 +00:00
|
|
|
this.update(&mut cx, |this, cx| {
|
|
|
|
this.buffer
|
|
|
|
.update(cx, |buffer, cx| buffer.apply_ops(ops, cx))
|
|
|
|
})?;
|
2023-08-21 23:30:57 +00:00
|
|
|
|
2023-08-22 00:53:37 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-08-21 23:30:57 +00:00
|
|
|
|
2023-08-22 00:53:37 +00:00
|
|
|
fn on_buffer_update(
|
|
|
|
&mut self,
|
|
|
|
_: ModelHandle<language::Buffer>,
|
|
|
|
event: &language::Event,
|
|
|
|
_: &mut ModelContext<Self>,
|
|
|
|
) {
|
|
|
|
if let language::Event::Operation(operation) = event {
|
|
|
|
let operation = language::proto::serialize_operation(operation);
|
|
|
|
self.client
|
|
|
|
.send(proto::UpdateChannelBuffer {
|
|
|
|
buffer_id: self.buffer_id,
|
|
|
|
operations: vec![operation],
|
2023-08-21 23:30:57 +00:00
|
|
|
})
|
2023-08-22 00:53:37 +00:00
|
|
|
.log_err();
|
2023-08-21 23:30:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-22 00:53:37 +00:00
|
|
|
|
|
|
|
pub fn buffer(&self) -> ModelHandle<language::Buffer> {
|
|
|
|
self.buffer.clone()
|
|
|
|
}
|
2023-08-21 23:30:57 +00:00
|
|
|
}
|