WIP
This commit is contained in:
+39
-9
@@ -1,11 +1,14 @@
|
||||
use crate::rpc::{self, Client};
|
||||
use anyhow::Result;
|
||||
use gpui::{Entity, ModelContext, WeakModelHandle};
|
||||
use anyhow::{Context, Result};
|
||||
use gpui::{AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle, WeakModelHandle};
|
||||
use std::{
|
||||
collections::{HashMap, VecDeque},
|
||||
sync::Arc,
|
||||
};
|
||||
use zrpc::{proto::ChannelMessageSent, TypedEnvelope};
|
||||
use zrpc::{
|
||||
proto::{self, ChannelMessageSent},
|
||||
TypedEnvelope,
|
||||
};
|
||||
|
||||
pub struct ChannelList {
|
||||
available_channels: Vec<ChannelDetails>,
|
||||
@@ -13,10 +16,12 @@ pub struct ChannelList {
|
||||
rpc: Arc<Client>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct ChannelDetails {
|
||||
id: u64,
|
||||
name: String,
|
||||
pub id: u64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
pub struct Channel {
|
||||
details: ChannelDetails,
|
||||
first_message_id: Option<u64>,
|
||||
@@ -35,12 +40,28 @@ impl Entity for ChannelList {
|
||||
}
|
||||
|
||||
impl ChannelList {
|
||||
fn new(rpc: Arc<rpc::Client>) -> Self {
|
||||
Self {
|
||||
available_channels: Default::default(),
|
||||
pub async fn new(rpc: Arc<rpc::Client>, cx: &mut AsyncAppContext) -> Result<ModelHandle<Self>> {
|
||||
let response = rpc
|
||||
.request(proto::GetChannels {})
|
||||
.await
|
||||
.context("failed to fetch available channels")?;
|
||||
|
||||
Ok(cx.add_model(|_| Self {
|
||||
available_channels: response.channels.into_iter().map(Into::into).collect(),
|
||||
channels: Default::default(),
|
||||
rpc,
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn available_channels(&self) -> &[ChannelDetails] {
|
||||
&self.available_channels
|
||||
}
|
||||
|
||||
pub fn get_channel(&self, id: u64, cx: &AppContext) -> Option<ModelHandle<Channel>> {
|
||||
self.channels
|
||||
.get(&id)
|
||||
.cloned()
|
||||
.and_then(|handle| handle.upgrade(cx))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,3 +91,12 @@ impl Channel {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<proto::Channel> for ChannelDetails {
|
||||
fn from(message: proto::Channel) -> Self {
|
||||
Self {
|
||||
id: message.id,
|
||||
name: message.name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
-6
@@ -46,12 +46,14 @@ pub struct Subscription {
|
||||
impl Drop for Subscription {
|
||||
fn drop(&mut self) {
|
||||
if let Some(client) = self.client.upgrade() {
|
||||
client
|
||||
.state
|
||||
.write()
|
||||
.model_handlers
|
||||
.remove(&self.id)
|
||||
.unwrap();
|
||||
drop(
|
||||
client
|
||||
.state
|
||||
.write()
|
||||
.model_handlers
|
||||
.remove(&self.id)
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user