From 04d194924e471eed785e13446fb9d372b0643817 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 28 Sep 2022 19:50:13 +0200 Subject: [PATCH] WIP: Start on `ActiveCall` Co-Authored-By: Nathan Sobo --- crates/room/src/active_call.rs | 55 ++++++++++++++++++++++++++++++++++ crates/room/src/room.rs | 1 + 2 files changed, 56 insertions(+) create mode 100644 crates/room/src/active_call.rs diff --git a/crates/room/src/active_call.rs b/crates/room/src/active_call.rs new file mode 100644 index 0000000000..63ca9583c2 --- /dev/null +++ b/crates/room/src/active_call.rs @@ -0,0 +1,55 @@ +use crate::Room; +use gpui::{Entity, ModelHandle, MutableAppContext}; + +#[derive(Default)] +pub struct ActiveCall { + room: Option>, +} + +impl Entity for ActiveCall { + type Event = (); +} + +impl ActiveCall { + pub fn global(cx: &mut MutableAppContext) -> ModelHandle { + if cx.has_global::>() { + let active_call = cx.add_model(|_| ActiveCall::default()); + cx.set_global(active_call.clone()); + active_call + } else { + cx.global::>().clone() + } + } + + pub fn observe(cx: &mut MutableAppContext, mut callback: F) -> gpui::Subscription + where + F: 'static + FnMut(Option>, &mut MutableAppContext), + { + cx.observe_default_global::>, _>(move |cx| { + let room = cx.global::>>().clone(); + callback(room, cx); + }) + } + + pub fn get_or_create( + client: &Arc, + user_store: &ModelHandle, + cx: &mut MutableAppContext, + ) -> Task>> { + if let Some(room) = cx.global::>>() { + Task::ready(Ok(room.clone())) + } else { + let client = client.clone(); + let user_store = user_store.clone(); + cx.spawn(|mut cx| async move { + let room = cx.update(|cx| Room::create(client, user_store, cx)).await?; + cx.update(|cx| cx.set_global(Some(room.clone()))); + Ok(room) + }) + } + } + + pub fn clear(cx: &mut MutableAppContext) { + cx.set_global::>>(None); + } +} diff --git a/crates/room/src/room.rs b/crates/room/src/room.rs index f7d5a58fa6..ba0a37e980 100644 --- a/crates/room/src/room.rs +++ b/crates/room/src/room.rs @@ -1,3 +1,4 @@ +mod active_call; mod participant; use anyhow::{anyhow, Result};