From 39ad7f6a60fecddaead0da5bc9d976843fe5346f Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 24 Aug 2021 08:37:32 -0600 Subject: [PATCH] Make RPC client's current user_id observable Co-Authored-By: Antonio Scandurra --- zed/src/channel.rs | 9 +++++++-- zed/src/rpc.rs | 21 ++++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/zed/src/channel.rs b/zed/src/channel.rs index 357546298c586e008aa4d8f412c92f7de0207e6b..f65320ae7fae3b501ccf801f177451fa425e4995 100644 --- a/zed/src/channel.rs +++ b/zed/src/channel.rs @@ -206,7 +206,8 @@ impl Channel { } }); Ok(()) - }.log_err() + } + .log_err() }) .detach(); cx.notify(); @@ -222,7 +223,11 @@ impl Channel { } fn current_user_id(&self) -> Result { - self.rpc.user_id().ok_or_else(|| anyhow!("not logged in")) + self + .rpc + .user_id() + .borrow() + .ok_or_else(|| anyhow!("not logged in")) } fn handle_message_sent( diff --git a/zed/src/rpc.rs b/zed/src/rpc.rs index b9992de6633604b2c066f625d57a1d393cd1ca43..4b1ab0e66e917a2db3910ffaa69f2a4d8eb396e2 100644 --- a/zed/src/rpc.rs +++ b/zed/src/rpc.rs @@ -5,6 +5,7 @@ use gpui::{AsyncAppContext, Entity, ModelContext, Task}; use lazy_static::lazy_static; use parking_lot::RwLock; use postage::prelude::Stream; +use postage::watch; use std::any::TypeId; use std::collections::HashMap; use std::sync::Weak; @@ -28,10 +29,9 @@ pub struct Client { state: RwLock, } -#[derive(Default)] struct ClientState { connection_id: Option, - user_id: Option, + user_id: (watch::Sender>, watch::Receiver>), entity_id_extractors: HashMap u64>>, model_handlers: HashMap< (TypeId, u64), @@ -39,6 +39,17 @@ struct ClientState { >, } +impl Default for ClientState { + fn default() -> Self { + Self { + connection_id: Default::default(), + user_id: watch::channel(), + entity_id_extractors: Default::default(), + model_handlers: Default::default(), + } + } +} + pub struct Subscription { client: Weak, id: (TypeId, u64), @@ -67,8 +78,8 @@ impl Client { }) } - pub fn user_id(&self) -> Option { - self.state.read().user_id + pub fn user_id(&self) -> watch::Receiver> { + self.state.read().user_id.1.clone() } pub fn subscribe_from_model( @@ -214,7 +225,7 @@ impl Client { .detach(); let mut state = self.state.write(); state.connection_id = Some(connection_id); - state.user_id = Some(user_id); + state.user_id = watch::channel_with(Some(user_id)); Ok(()) }