mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-08 07:35:01 +03:00
Add RpcClient::subscribe
Co-Authored-By: Max Brunsfeld <max@zed.dev>
This commit is contained in:
parent
cd1a4c49cf
commit
e551894189
@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
|
|||||||
use gpui::executor::Background;
|
use gpui::executor::Background;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use postage::{
|
use postage::{
|
||||||
oneshot,
|
mpsc, oneshot,
|
||||||
prelude::{Sink, Stream},
|
prelude::{Sink, Stream},
|
||||||
};
|
};
|
||||||
use smol::{
|
use smol::{
|
||||||
@ -11,11 +11,13 @@ use smol::{
|
|||||||
prelude::{AsyncRead, AsyncWrite},
|
prelude::{AsyncRead, AsyncWrite},
|
||||||
};
|
};
|
||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::HashMap, sync::Arc};
|
||||||
use zed_rpc::proto::{self, MessageStream, RequestMessage, SendMessage, ServerMessage};
|
use zed_rpc::proto::{
|
||||||
|
self, MessageStream, RequestMessage, SendMessage, ServerMessage, SubscribeMessage,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct RpcClient<Conn> {
|
pub struct RpcClient<Conn> {
|
||||||
stream: MessageStream<WriteHalf<Conn>>,
|
stream: MessageStream<WriteHalf<Conn>>,
|
||||||
response_channels: Arc<Mutex<HashMap<i32, oneshot::Sender<proto::from_server::Variant>>>>,
|
response_channels: Arc<Mutex<HashMap<i32, (mpsc::Sender<proto::from_server::Variant>, bool)>>>,
|
||||||
next_message_id: i32,
|
next_message_id: i32,
|
||||||
_drop_tx: oneshot::Sender<()>,
|
_drop_tx: oneshot::Sender<()>,
|
||||||
}
|
}
|
||||||
@ -59,9 +61,15 @@ where
|
|||||||
Message::Message(message) => {
|
Message::Message(message) => {
|
||||||
if let Some(variant) = message.variant {
|
if let Some(variant) = message.variant {
|
||||||
if let Some(request_id) = message.request_id {
|
if let Some(request_id) = message.request_id {
|
||||||
let tx = response_channels.lock().remove(&request_id);
|
let channel = response_channels.lock().remove(&request_id);
|
||||||
if let Some(mut tx) = tx {
|
if let Some((mut tx, oneshot)) = channel {
|
||||||
tx.send(variant).await?;
|
if tx.send(variant).await.is_ok() {
|
||||||
|
if !oneshot {
|
||||||
|
response_channels
|
||||||
|
.lock()
|
||||||
|
.insert(request_id, (tx, false));
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
log::warn!(
|
log::warn!(
|
||||||
"received RPC response to unknown request id {}",
|
"received RPC response to unknown request id {}",
|
||||||
@ -85,10 +93,8 @@ where
|
|||||||
pub async fn request<T: RequestMessage>(&mut self, req: T) -> Result<T::Response> {
|
pub async fn request<T: RequestMessage>(&mut self, req: T) -> Result<T::Response> {
|
||||||
let message_id = self.next_message_id;
|
let message_id = self.next_message_id;
|
||||||
self.next_message_id += 1;
|
self.next_message_id += 1;
|
||||||
|
let (tx, mut rx) = mpsc::channel(1);
|
||||||
let (tx, mut rx) = oneshot::channel();
|
self.response_channels.lock().insert(message_id, (tx, true));
|
||||||
self.response_channels.lock().insert(message_id, tx);
|
|
||||||
|
|
||||||
self.stream
|
self.stream
|
||||||
.write_message(&proto::FromClient {
|
.write_message(&proto::FromClient {
|
||||||
id: message_id,
|
id: message_id,
|
||||||
@ -114,6 +120,28 @@ where
|
|||||||
.await?;
|
.await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn subscribe<T: SubscribeMessage>(
|
||||||
|
&mut self,
|
||||||
|
subscription: T,
|
||||||
|
) -> Result<impl Stream<Item = Result<T::Event>>> {
|
||||||
|
let message_id = self.next_message_id;
|
||||||
|
self.next_message_id += 1;
|
||||||
|
let (tx, rx) = mpsc::channel(256);
|
||||||
|
self.response_channels
|
||||||
|
.lock()
|
||||||
|
.insert(message_id, (tx, false));
|
||||||
|
self.stream
|
||||||
|
.write_message(&proto::FromClient {
|
||||||
|
id: message_id,
|
||||||
|
variant: Some(subscription.to_variant()),
|
||||||
|
})
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(rx.map(|event| {
|
||||||
|
T::Event::from_variant(event).ok_or_else(|| anyhow!("invalid event {:?}"))
|
||||||
|
}))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
Loading…
Reference in New Issue
Block a user