Move Arc outside of rpc::Client
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
3631fbd874
commit
86c819757d
6 changed files with 45 additions and 33 deletions
|
@ -549,7 +549,11 @@ impl TestServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn create_client(&mut self, cx: &mut TestAppContext, name: &str) -> (UserId, Client) {
|
async fn create_client(
|
||||||
|
&mut self,
|
||||||
|
cx: &mut TestAppContext,
|
||||||
|
name: &str,
|
||||||
|
) -> (UserId, Arc<Client>) {
|
||||||
let user_id = self.app_state.db.create_user(name, false).await.unwrap();
|
let user_id = self.app_state.db.create_user(name, false).await.unwrap();
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
let (client_conn, server_conn) = Channel::bidirectional();
|
let (client_conn, server_conn) = Channel::bidirectional();
|
||||||
|
|
|
@ -64,7 +64,7 @@ impl Channel {
|
||||||
fn handle_message_sent(
|
fn handle_message_sent(
|
||||||
&mut self,
|
&mut self,
|
||||||
message: TypedEnvelope<ChannelMessageSent>,
|
message: TypedEnvelope<ChannelMessageSent>,
|
||||||
rpc: rpc::Client,
|
rpc: Arc<rpc::Client>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -30,7 +30,7 @@ pub struct AppState {
|
||||||
pub settings: watch::Receiver<Settings>,
|
pub settings: watch::Receiver<Settings>,
|
||||||
pub languages: Arc<language::LanguageRegistry>,
|
pub languages: Arc<language::LanguageRegistry>,
|
||||||
pub themes: Arc<settings::ThemeRegistry>,
|
pub themes: Arc<settings::ThemeRegistry>,
|
||||||
pub rpc: rpc::Client,
|
pub rpc: Arc<rpc::Client>,
|
||||||
pub fs: Arc<dyn fs::Fs>,
|
pub fs: Arc<dyn fs::Fs>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,14 +23,13 @@ lazy_static! {
|
||||||
std::env::var("ZED_SERVER_URL").unwrap_or("https://zed.dev:443".to_string());
|
std::env::var("ZED_SERVER_URL").unwrap_or("https://zed.dev:443".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
peer: Arc<Peer>,
|
peer: Arc<Peer>,
|
||||||
state: Arc<RwLock<ClientState>>,
|
state: RwLock<ClientState>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct ClientState {
|
struct ClientState {
|
||||||
connection_id: Option<ConnectionId>,
|
connection_id: Option<ConnectionId>,
|
||||||
entity_id_extractors: HashMap<TypeId, Box<dyn Send + Sync + Fn(&dyn AnyTypedEnvelope) -> u64>>,
|
entity_id_extractors: HashMap<TypeId, Box<dyn Send + Sync + Fn(&dyn AnyTypedEnvelope) -> u64>>,
|
||||||
model_handlers: HashMap<
|
model_handlers: HashMap<
|
||||||
|
@ -40,28 +39,33 @@ pub struct ClientState {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Subscription {
|
pub struct Subscription {
|
||||||
state: Weak<RwLock<ClientState>>,
|
client: Weak<Client>,
|
||||||
id: (TypeId, u64),
|
id: (TypeId, u64),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Subscription {
|
impl Drop for Subscription {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some(state) = self.state.upgrade() {
|
if let Some(client) = self.client.upgrade() {
|
||||||
let _ = state.write().model_handlers.remove(&self.id).unwrap();
|
client
|
||||||
|
.state
|
||||||
|
.write()
|
||||||
|
.model_handlers
|
||||||
|
.remove(&self.id)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Arc<Self> {
|
||||||
Self {
|
Arc::new(Self {
|
||||||
peer: Peer::new(),
|
peer: Peer::new(),
|
||||||
state: Default::default(),
|
state: Default::default(),
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe_from_model<T, M, F>(
|
pub fn subscribe_from_model<T, M, F>(
|
||||||
&self,
|
self: &Arc<Self>,
|
||||||
remote_id: u64,
|
remote_id: u64,
|
||||||
cx: &mut ModelContext<M>,
|
cx: &mut ModelContext<M>,
|
||||||
mut handler: F,
|
mut handler: F,
|
||||||
|
@ -72,7 +76,7 @@ impl Client {
|
||||||
F: 'static
|
F: 'static
|
||||||
+ Send
|
+ Send
|
||||||
+ Sync
|
+ Sync
|
||||||
+ FnMut(&mut M, TypedEnvelope<T>, Client, &mut ModelContext<M>) -> Result<()>,
|
+ FnMut(&mut M, TypedEnvelope<T>, Arc<Self>, &mut ModelContext<M>) -> Result<()>,
|
||||||
{
|
{
|
||||||
let subscription_id = (TypeId::of::<T>(), remote_id);
|
let subscription_id = (TypeId::of::<T>(), remote_id);
|
||||||
let client = self.clone();
|
let client = self.clone();
|
||||||
|
@ -108,12 +112,12 @@ impl Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
Subscription {
|
Subscription {
|
||||||
state: Arc::downgrade(&self.state),
|
client: Arc::downgrade(self),
|
||||||
id: subscription_id,
|
id: subscription_id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn log_in_and_connect(&self, cx: AsyncAppContext) -> surf::Result<()> {
|
pub async fn log_in_and_connect(self: &Arc<Self>, cx: AsyncAppContext) -> surf::Result<()> {
|
||||||
if self.state.read().connection_id.is_some() {
|
if self.state.read().connection_id.is_some() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -144,7 +148,11 @@ impl Client {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn add_connection<Conn>(&self, conn: Conn, cx: AsyncAppContext) -> surf::Result<()>
|
pub async fn add_connection<Conn>(
|
||||||
|
self: &Arc<Self>,
|
||||||
|
conn: Conn,
|
||||||
|
cx: AsyncAppContext,
|
||||||
|
) -> surf::Result<()>
|
||||||
where
|
where
|
||||||
Conn: 'static
|
Conn: 'static
|
||||||
+ futures::Sink<WebSocketMessage, Error = WebSocketError>
|
+ futures::Sink<WebSocketMessage, Error = WebSocketError>
|
||||||
|
@ -155,11 +163,11 @@ impl Client {
|
||||||
let (connection_id, handle_io, mut incoming) = self.peer.add_connection(conn).await;
|
let (connection_id, handle_io, mut incoming) = self.peer.add_connection(conn).await;
|
||||||
{
|
{
|
||||||
let mut cx = cx.clone();
|
let mut cx = cx.clone();
|
||||||
let state = self.state.clone();
|
let this = self.clone();
|
||||||
cx.foreground()
|
cx.foreground()
|
||||||
.spawn(async move {
|
.spawn(async move {
|
||||||
while let Some(message) = incoming.recv().await {
|
while let Some(message) = incoming.recv().await {
|
||||||
let mut state = state.write();
|
let mut state = this.state.write();
|
||||||
if let Some(extract_entity_id) =
|
if let Some(extract_entity_id) =
|
||||||
state.entity_id_extractors.get(&message.payload_type_id())
|
state.entity_id_extractors.get(&message.payload_type_id())
|
||||||
{
|
{
|
||||||
|
|
|
@ -312,7 +312,7 @@ pub struct State {
|
||||||
pub struct Workspace {
|
pub struct Workspace {
|
||||||
pub settings: watch::Receiver<Settings>,
|
pub settings: watch::Receiver<Settings>,
|
||||||
languages: Arc<LanguageRegistry>,
|
languages: Arc<LanguageRegistry>,
|
||||||
rpc: rpc::Client,
|
rpc: Arc<rpc::Client>,
|
||||||
fs: Arc<dyn Fs>,
|
fs: Arc<dyn Fs>,
|
||||||
modal: Option<AnyViewHandle>,
|
modal: Option<AnyViewHandle>,
|
||||||
center: PaneGroup,
|
center: PaneGroup,
|
||||||
|
|
|
@ -107,7 +107,7 @@ impl Worktree {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn open_remote(
|
pub async fn open_remote(
|
||||||
rpc: rpc::Client,
|
rpc: Arc<rpc::Client>,
|
||||||
id: u64,
|
id: u64,
|
||||||
access_token: String,
|
access_token: String,
|
||||||
languages: Arc<LanguageRegistry>,
|
languages: Arc<LanguageRegistry>,
|
||||||
|
@ -125,7 +125,7 @@ impl Worktree {
|
||||||
|
|
||||||
async fn remote(
|
async fn remote(
|
||||||
open_response: proto::OpenWorktreeResponse,
|
open_response: proto::OpenWorktreeResponse,
|
||||||
rpc: rpc::Client,
|
rpc: Arc<rpc::Client>,
|
||||||
languages: Arc<LanguageRegistry>,
|
languages: Arc<LanguageRegistry>,
|
||||||
cx: &mut AsyncAppContext,
|
cx: &mut AsyncAppContext,
|
||||||
) -> Result<ModelHandle<Self>> {
|
) -> Result<ModelHandle<Self>> {
|
||||||
|
@ -283,7 +283,7 @@ impl Worktree {
|
||||||
pub fn handle_add_peer(
|
pub fn handle_add_peer(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::AddPeer>,
|
envelope: TypedEnvelope<proto::AddPeer>,
|
||||||
_: rpc::Client,
|
_: Arc<rpc::Client>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
match self {
|
match self {
|
||||||
|
@ -295,7 +295,7 @@ impl Worktree {
|
||||||
pub fn handle_remove_peer(
|
pub fn handle_remove_peer(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::RemovePeer>,
|
envelope: TypedEnvelope<proto::RemovePeer>,
|
||||||
_: rpc::Client,
|
_: Arc<rpc::Client>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
match self {
|
match self {
|
||||||
|
@ -307,7 +307,7 @@ impl Worktree {
|
||||||
pub fn handle_update(
|
pub fn handle_update(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::UpdateWorktree>,
|
envelope: TypedEnvelope<proto::UpdateWorktree>,
|
||||||
_: rpc::Client,
|
_: Arc<rpc::Client>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
self.as_remote_mut()
|
self.as_remote_mut()
|
||||||
|
@ -318,7 +318,7 @@ impl Worktree {
|
||||||
pub fn handle_open_buffer(
|
pub fn handle_open_buffer(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::OpenBuffer>,
|
envelope: TypedEnvelope<proto::OpenBuffer>,
|
||||||
rpc: rpc::Client,
|
rpc: Arc<rpc::Client>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
let receipt = envelope.receipt();
|
let receipt = envelope.receipt();
|
||||||
|
@ -341,7 +341,7 @@ impl Worktree {
|
||||||
pub fn handle_close_buffer(
|
pub fn handle_close_buffer(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::CloseBuffer>,
|
envelope: TypedEnvelope<proto::CloseBuffer>,
|
||||||
_: rpc::Client,
|
_: Arc<rpc::Client>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
self.as_local_mut()
|
self.as_local_mut()
|
||||||
|
@ -397,7 +397,7 @@ impl Worktree {
|
||||||
pub fn handle_update_buffer(
|
pub fn handle_update_buffer(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::UpdateBuffer>,
|
envelope: TypedEnvelope<proto::UpdateBuffer>,
|
||||||
_: rpc::Client,
|
_: Arc<rpc::Client>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let payload = envelope.payload.clone();
|
let payload = envelope.payload.clone();
|
||||||
|
@ -444,7 +444,7 @@ impl Worktree {
|
||||||
pub fn handle_save_buffer(
|
pub fn handle_save_buffer(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::SaveBuffer>,
|
envelope: TypedEnvelope<proto::SaveBuffer>,
|
||||||
rpc: rpc::Client,
|
rpc: Arc<rpc::Client>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let sender_id = envelope.original_sender_id()?;
|
let sender_id = envelope.original_sender_id()?;
|
||||||
|
@ -488,7 +488,7 @@ impl Worktree {
|
||||||
pub fn handle_buffer_saved(
|
pub fn handle_buffer_saved(
|
||||||
&mut self,
|
&mut self,
|
||||||
envelope: TypedEnvelope<proto::BufferSaved>,
|
envelope: TypedEnvelope<proto::BufferSaved>,
|
||||||
_: rpc::Client,
|
_: Arc<rpc::Client>,
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let payload = envelope.payload.clone();
|
let payload = envelope.payload.clone();
|
||||||
|
@ -966,7 +966,7 @@ impl LocalWorktree {
|
||||||
|
|
||||||
pub fn share(
|
pub fn share(
|
||||||
&mut self,
|
&mut self,
|
||||||
rpc: rpc::Client,
|
rpc: Arc<rpc::Client>,
|
||||||
cx: &mut ModelContext<Worktree>,
|
cx: &mut ModelContext<Worktree>,
|
||||||
) -> Task<anyhow::Result<(u64, String)>> {
|
) -> Task<anyhow::Result<(u64, String)>> {
|
||||||
let snapshot = self.snapshot();
|
let snapshot = self.snapshot();
|
||||||
|
@ -1068,7 +1068,7 @@ impl fmt::Debug for LocalWorktree {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ShareState {
|
struct ShareState {
|
||||||
rpc: rpc::Client,
|
rpc: Arc<rpc::Client>,
|
||||||
remote_id: u64,
|
remote_id: u64,
|
||||||
snapshots_tx: Sender<Snapshot>,
|
snapshots_tx: Sender<Snapshot>,
|
||||||
_subscriptions: Vec<rpc::Subscription>,
|
_subscriptions: Vec<rpc::Subscription>,
|
||||||
|
@ -1078,7 +1078,7 @@ pub struct RemoteWorktree {
|
||||||
remote_id: u64,
|
remote_id: u64,
|
||||||
snapshot: Snapshot,
|
snapshot: Snapshot,
|
||||||
snapshot_rx: watch::Receiver<Snapshot>,
|
snapshot_rx: watch::Receiver<Snapshot>,
|
||||||
rpc: rpc::Client,
|
rpc: Arc<rpc::Client>,
|
||||||
updates_tx: postage::mpsc::Sender<proto::UpdateWorktree>,
|
updates_tx: postage::mpsc::Sender<proto::UpdateWorktree>,
|
||||||
replica_id: ReplicaId,
|
replica_id: ReplicaId,
|
||||||
open_buffers: HashMap<usize, RemoteBuffer>,
|
open_buffers: HashMap<usize, RemoteBuffer>,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue