call2.rs

  1pub mod call_settings;
  2pub mod participant;
  3pub mod room;
  4mod shared_screen;
  5
  6use anyhow::{anyhow, Result};
  7use async_trait::async_trait;
  8use audio::Audio;
  9use call_settings::CallSettings;
 10use client::{
 11    proto::{self, PeerId},
 12    Client, TelemetrySettings, TypedEnvelope, User, UserStore, ZED_ALWAYS_ACTIVE,
 13};
 14use collections::HashSet;
 15use futures::{channel::oneshot, future::Shared, Future, FutureExt};
 16use gpui::{
 17    AppContext, AsyncAppContext, Context, EventEmitter, Model, ModelContext, PromptLevel,
 18    Subscription, Task, View, ViewContext, VisualContext, WeakModel, WindowHandle,
 19};
 20pub use participant::ParticipantLocation;
 21use postage::watch;
 22use project::Project;
 23use room::Event;
 24pub use room::Room;
 25use settings::Settings;
 26use shared_screen::SharedScreen;
 27use std::sync::Arc;
 28use util::ResultExt;
 29use workspace::{item::ItemHandle, CallHandler, Pane, Workspace};
 30
 31pub fn init(client: Arc<Client>, user_store: Model<UserStore>, cx: &mut AppContext) {
 32    CallSettings::register(cx);
 33
 34    let active_call = cx.build_model(|cx| ActiveCall::new(client, user_store, cx));
 35    cx.set_global(active_call);
 36}
 37
 38pub struct OneAtATime {
 39    cancel: Option<oneshot::Sender<()>>,
 40}
 41
 42impl OneAtATime {
 43    /// spawn a task in the given context.
 44    /// if another task is spawned before that resolves, or if the OneAtATime itself is dropped, the first task will be cancelled and return Ok(None)
 45    /// otherwise you'll see the result of the task.
 46    fn spawn<F, Fut, R>(&mut self, cx: &mut AppContext, f: F) -> Task<Result<Option<R>>>
 47    where
 48        F: 'static + FnOnce(AsyncAppContext) -> Fut,
 49        Fut: Future<Output = Result<R>>,
 50        R: 'static,
 51    {
 52        let (tx, rx) = oneshot::channel();
 53        self.cancel.replace(tx);
 54        cx.spawn(|cx| async move {
 55            futures::select_biased! {
 56                _ = rx.fuse() => Ok(None),
 57                result = f(cx).fuse() => result.map(Some),
 58            }
 59        })
 60    }
 61
 62    fn running(&self) -> bool {
 63        self.cancel
 64            .as_ref()
 65            .is_some_and(|cancel| !cancel.is_canceled())
 66    }
 67}
 68
 69#[derive(Clone)]
 70pub struct IncomingCall {
 71    pub room_id: u64,
 72    pub calling_user: Arc<User>,
 73    pub participants: Vec<Arc<User>>,
 74    pub initial_project: Option<proto::ParticipantProject>,
 75}
 76
 77/// Singleton global maintaining the user's participation in a room across workspaces.
 78pub struct ActiveCall {
 79    room: Option<(Model<Room>, Vec<Subscription>)>,
 80    pending_room_creation: Option<Shared<Task<Result<Model<Room>, Arc<anyhow::Error>>>>>,
 81    location: Option<WeakModel<Project>>,
 82    _join_debouncer: OneAtATime,
 83    pending_invites: HashSet<u64>,
 84    incoming_call: (
 85        watch::Sender<Option<IncomingCall>>,
 86        watch::Receiver<Option<IncomingCall>>,
 87    ),
 88    client: Arc<Client>,
 89    user_store: Model<UserStore>,
 90    _subscriptions: Vec<client::Subscription>,
 91}
 92
 93impl EventEmitter<Event> for ActiveCall {}
 94
 95impl ActiveCall {
 96    fn new(client: Arc<Client>, user_store: Model<UserStore>, cx: &mut ModelContext<Self>) -> Self {
 97        Self {
 98            room: None,
 99            pending_room_creation: None,
100            location: None,
101            pending_invites: Default::default(),
102            incoming_call: watch::channel(),
103            _join_debouncer: OneAtATime { cancel: None },
104            _subscriptions: vec![
105                client.add_request_handler(cx.weak_model(), Self::handle_incoming_call),
106                client.add_message_handler(cx.weak_model(), Self::handle_call_canceled),
107            ],
108            client,
109            user_store,
110        }
111    }
112
113    pub fn channel_id(&self, cx: &AppContext) -> Option<u64> {
114        self.room()?.read(cx).channel_id()
115    }
116
117    async fn handle_incoming_call(
118        this: Model<Self>,
119        envelope: TypedEnvelope<proto::IncomingCall>,
120        _: Arc<Client>,
121        mut cx: AsyncAppContext,
122    ) -> Result<proto::Ack> {
123        let user_store = this.update(&mut cx, |this, _| this.user_store.clone())?;
124        let call = IncomingCall {
125            room_id: envelope.payload.room_id,
126            participants: user_store
127                .update(&mut cx, |user_store, cx| {
128                    user_store.get_users(envelope.payload.participant_user_ids, cx)
129                })?
130                .await?,
131            calling_user: user_store
132                .update(&mut cx, |user_store, cx| {
133                    user_store.get_user(envelope.payload.calling_user_id, cx)
134                })?
135                .await?,
136            initial_project: envelope.payload.initial_project,
137        };
138        this.update(&mut cx, |this, _| {
139            *this.incoming_call.0.borrow_mut() = Some(call);
140        })?;
141
142        Ok(proto::Ack {})
143    }
144
145    async fn handle_call_canceled(
146        this: Model<Self>,
147        envelope: TypedEnvelope<proto::CallCanceled>,
148        _: Arc<Client>,
149        mut cx: AsyncAppContext,
150    ) -> Result<()> {
151        this.update(&mut cx, |this, _| {
152            let mut incoming_call = this.incoming_call.0.borrow_mut();
153            if incoming_call
154                .as_ref()
155                .map_or(false, |call| call.room_id == envelope.payload.room_id)
156            {
157                incoming_call.take();
158            }
159        })?;
160        Ok(())
161    }
162
163    pub fn global(cx: &AppContext) -> Model<Self> {
164        cx.global::<Model<Self>>().clone()
165    }
166
167    pub fn invite(
168        &mut self,
169        called_user_id: u64,
170        initial_project: Option<Model<Project>>,
171        cx: &mut ModelContext<Self>,
172    ) -> Task<Result<()>> {
173        if !self.pending_invites.insert(called_user_id) {
174            return Task::ready(Err(anyhow!("user was already invited")));
175        }
176        cx.notify();
177
178        if self._join_debouncer.running() {
179            return Task::ready(Ok(()));
180        }
181
182        let room = if let Some(room) = self.room().cloned() {
183            Some(Task::ready(Ok(room)).shared())
184        } else {
185            self.pending_room_creation.clone()
186        };
187
188        let invite = if let Some(room) = room {
189            cx.spawn(move |_, mut cx| async move {
190                let room = room.await.map_err(|err| anyhow!("{:?}", err))?;
191
192                let initial_project_id = if let Some(initial_project) = initial_project {
193                    Some(
194                        room.update(&mut cx, |room, cx| room.share_project(initial_project, cx))?
195                            .await?,
196                    )
197                } else {
198                    None
199                };
200
201                room.update(&mut cx, move |room, cx| {
202                    room.call(called_user_id, initial_project_id, cx)
203                })?
204                .await?;
205
206                anyhow::Ok(())
207            })
208        } else {
209            let client = self.client.clone();
210            let user_store = self.user_store.clone();
211            let room = cx
212                .spawn(move |this, mut cx| async move {
213                    let create_room = async {
214                        let room = cx
215                            .update(|cx| {
216                                Room::create(
217                                    called_user_id,
218                                    initial_project,
219                                    client,
220                                    user_store,
221                                    cx,
222                                )
223                            })?
224                            .await?;
225
226                        this.update(&mut cx, |this, cx| this.set_room(Some(room.clone()), cx))?
227                            .await?;
228
229                        anyhow::Ok(room)
230                    };
231
232                    let room = create_room.await;
233                    this.update(&mut cx, |this, _| this.pending_room_creation = None)?;
234                    room.map_err(Arc::new)
235                })
236                .shared();
237            self.pending_room_creation = Some(room.clone());
238            cx.background_executor().spawn(async move {
239                room.await.map_err(|err| anyhow!("{:?}", err))?;
240                anyhow::Ok(())
241            })
242        };
243
244        cx.spawn(move |this, mut cx| async move {
245            let result = invite.await;
246            if result.is_ok() {
247                this.update(&mut cx, |this, cx| this.report_call_event("invite", cx))?;
248            } else {
249                // TODO: Resport collaboration error
250            }
251
252            this.update(&mut cx, |this, cx| {
253                this.pending_invites.remove(&called_user_id);
254                cx.notify();
255            })?;
256            result
257        })
258    }
259
260    pub fn cancel_invite(
261        &mut self,
262        called_user_id: u64,
263        cx: &mut ModelContext<Self>,
264    ) -> Task<Result<()>> {
265        let room_id = if let Some(room) = self.room() {
266            room.read(cx).id()
267        } else {
268            return Task::ready(Err(anyhow!("no active call")));
269        };
270
271        let client = self.client.clone();
272        cx.background_executor().spawn(async move {
273            client
274                .request(proto::CancelCall {
275                    room_id,
276                    called_user_id,
277                })
278                .await?;
279            anyhow::Ok(())
280        })
281    }
282
283    pub fn incoming(&self) -> watch::Receiver<Option<IncomingCall>> {
284        self.incoming_call.1.clone()
285    }
286
287    pub fn accept_incoming(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
288        if self.room.is_some() {
289            return Task::ready(Err(anyhow!("cannot join while on another call")));
290        }
291
292        let call = if let Some(call) = self.incoming_call.1.borrow().clone() {
293            call
294        } else {
295            return Task::ready(Err(anyhow!("no incoming call")));
296        };
297
298        if self.pending_room_creation.is_some() {
299            return Task::ready(Ok(()));
300        }
301
302        let room_id = call.room_id.clone();
303        let client = self.client.clone();
304        let user_store = self.user_store.clone();
305        let join = self
306            ._join_debouncer
307            .spawn(cx, move |cx| Room::join(room_id, client, user_store, cx));
308
309        cx.spawn(|this, mut cx| async move {
310            let room = join.await?;
311            this.update(&mut cx, |this, cx| this.set_room(room.clone(), cx))?
312                .await?;
313            this.update(&mut cx, |this, cx| {
314                this.report_call_event("accept incoming", cx)
315            })?;
316            Ok(())
317        })
318    }
319
320    pub fn decline_incoming(&mut self, cx: &mut ModelContext<Self>) -> Result<()> {
321        let call = self
322            .incoming_call
323            .0
324            .borrow_mut()
325            .take()
326            .ok_or_else(|| anyhow!("no incoming call"))?;
327        report_call_event_for_room("decline incoming", call.room_id, None, &self.client, cx);
328        self.client.send(proto::DeclineCall {
329            room_id: call.room_id,
330        })?;
331        Ok(())
332    }
333
334    pub fn join_channel(
335        &mut self,
336        channel_id: u64,
337        requesting_window: Option<WindowHandle<Workspace>>,
338        cx: &mut ModelContext<Self>,
339    ) -> Task<Result<Option<Model<Room>>>> {
340        if let Some(room) = self.room().cloned() {
341            if room.read(cx).channel_id() == Some(channel_id) {
342                return cx.spawn(|_, _| async move {
343                    todo!();
344                    // let future = room.update(&mut cx, |room, cx| {
345                    //     room.most_active_project(cx).map(|(host, project)| {
346                    //         room.join_project(project, host, app_state.clone(), cx)
347                    //     })
348                    // })
349
350                    // if let Some(future) = future {
351                    //     future.await?;
352                    // }
353
354                    // Ok(Some(room))
355                });
356            }
357
358            let should_prompt = room.update(cx, |room, _| {
359                room.channel_id().is_some()
360                    && room.is_sharing_project()
361                    && room.remote_participants().len() > 0
362            });
363            if should_prompt && requesting_window.is_some() {
364                return cx.spawn(|this, mut cx| async move {
365                    let answer = requesting_window.unwrap().update(&mut cx, |_, cx| {
366                        cx.prompt(
367                            PromptLevel::Warning,
368                            "Leaving this call will unshare your current project.\nDo you want to switch channels?",
369                            &["Yes, Join Channel", "Cancel"],
370                        )
371                    })?;
372                    if answer.await? == 1 {
373                        return Ok(None);
374                    }
375
376                    room.update(&mut cx, |room, cx| room.clear_state(cx))?;
377
378                    this.update(&mut cx, |this, cx| {
379                        this.join_channel(channel_id, requesting_window, cx)
380                    })?
381                    .await
382                });
383            }
384
385            if room.read(cx).channel_id().is_some() {
386                room.update(cx, |room, cx| room.clear_state(cx));
387            }
388        }
389
390        if self.pending_room_creation.is_some() {
391            return Task::ready(Ok(None));
392        }
393
394        let client = self.client.clone();
395        let user_store = self.user_store.clone();
396        let join = self._join_debouncer.spawn(cx, move |cx| async move {
397            Room::join_channel(channel_id, client, user_store, cx).await
398        });
399
400        cx.spawn(|this, mut cx| async move {
401            let room = join.await?;
402            this.update(&mut cx, |this, cx| this.set_room(room.clone(), cx))?
403                .await?;
404            this.update(&mut cx, |this, cx| {
405                this.report_call_event("join channel", cx)
406            })?;
407            Ok(room)
408        })
409    }
410
411    pub fn hang_up(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
412        cx.notify();
413        self.report_call_event("hang up", cx);
414
415        Audio::end_call(cx);
416        if let Some((room, _)) = self.room.take() {
417            room.update(cx, |room, cx| room.leave(cx))
418        } else {
419            Task::ready(Ok(()))
420        }
421    }
422
423    pub fn share_project(
424        &mut self,
425        project: Model<Project>,
426        cx: &mut ModelContext<Self>,
427    ) -> Task<Result<u64>> {
428        if let Some((room, _)) = self.room.as_ref() {
429            self.report_call_event("share project", cx);
430            room.update(cx, |room, cx| room.share_project(project, cx))
431        } else {
432            Task::ready(Err(anyhow!("no active call")))
433        }
434    }
435
436    pub fn unshare_project(
437        &mut self,
438        project: Model<Project>,
439        cx: &mut ModelContext<Self>,
440    ) -> Result<()> {
441        if let Some((room, _)) = self.room.as_ref() {
442            self.report_call_event("unshare project", cx);
443            room.update(cx, |room, cx| room.unshare_project(project, cx))
444        } else {
445            Err(anyhow!("no active call"))
446        }
447    }
448
449    pub fn location(&self) -> Option<&WeakModel<Project>> {
450        self.location.as_ref()
451    }
452
453    pub fn set_location(
454        &mut self,
455        project: Option<&Model<Project>>,
456        cx: &mut ModelContext<Self>,
457    ) -> Task<Result<()>> {
458        if project.is_some() || !*ZED_ALWAYS_ACTIVE {
459            self.location = project.map(|project| project.downgrade());
460            if let Some((room, _)) = self.room.as_ref() {
461                return room.update(cx, |room, cx| room.set_location(project, cx));
462            }
463        }
464        Task::ready(Ok(()))
465    }
466
467    fn set_room(
468        &mut self,
469        room: Option<Model<Room>>,
470        cx: &mut ModelContext<Self>,
471    ) -> Task<Result<()>> {
472        if room.as_ref() != self.room.as_ref().map(|room| &room.0) {
473            cx.notify();
474            if let Some(room) = room {
475                if room.read(cx).status().is_offline() {
476                    self.room = None;
477                    Task::ready(Ok(()))
478                } else {
479                    let subscriptions = vec![
480                        cx.observe(&room, |this, room, cx| {
481                            if room.read(cx).status().is_offline() {
482                                this.set_room(None, cx).detach_and_log_err(cx);
483                            }
484
485                            cx.notify();
486                        }),
487                        cx.subscribe(&room, |_, _, event, cx| cx.emit(event.clone())),
488                    ];
489                    self.room = Some((room.clone(), subscriptions));
490                    let location = self
491                        .location
492                        .as_ref()
493                        .and_then(|location| location.upgrade());
494                    room.update(cx, |room, cx| room.set_location(location.as_ref(), cx))
495                }
496            } else {
497                self.room = None;
498                Task::ready(Ok(()))
499            }
500        } else {
501            Task::ready(Ok(()))
502        }
503    }
504
505    pub fn room(&self) -> Option<&Model<Room>> {
506        self.room.as_ref().map(|(room, _)| room)
507    }
508
509    pub fn client(&self) -> Arc<Client> {
510        self.client.clone()
511    }
512
513    pub fn pending_invites(&self) -> &HashSet<u64> {
514        &self.pending_invites
515    }
516
517    pub fn report_call_event(&self, operation: &'static str, cx: &mut AppContext) {
518        if let Some(room) = self.room() {
519            let room = room.read(cx);
520            report_call_event_for_room(operation, room.id(), room.channel_id(), &self.client, cx);
521        }
522    }
523}
524
525pub fn report_call_event_for_room(
526    operation: &'static str,
527    room_id: u64,
528    channel_id: Option<u64>,
529    client: &Arc<Client>,
530    cx: &mut AppContext,
531) {
532    let telemetry = client.telemetry();
533    let telemetry_settings = *TelemetrySettings::get_global(cx);
534
535    telemetry.report_call_event(telemetry_settings, operation, Some(room_id), channel_id)
536}
537
538pub fn report_call_event_for_channel(
539    operation: &'static str,
540    channel_id: u64,
541    client: &Arc<Client>,
542    cx: &AppContext,
543) {
544    let room = ActiveCall::global(cx).read(cx).room();
545
546    let telemetry = client.telemetry();
547
548    let telemetry_settings = *TelemetrySettings::get_global(cx);
549
550    telemetry.report_call_event(
551        telemetry_settings,
552        operation,
553        room.map(|r| r.read(cx).id()),
554        Some(channel_id),
555    )
556}
557
558pub struct Call {
559    active_call: Option<(Model<ActiveCall>, Vec<Subscription>)>,
560}
561
562impl Call {
563    pub fn new(cx: &mut ViewContext<'_, Workspace>) -> Box<dyn CallHandler> {
564        let mut active_call = None;
565        if cx.has_global::<Model<ActiveCall>>() {
566            let call = cx.global::<Model<ActiveCall>>().clone();
567            let subscriptions = vec![cx.subscribe(&call, Self::on_active_call_event)];
568            active_call = Some((call, subscriptions));
569        }
570        Box::new(Self { active_call })
571    }
572    fn on_active_call_event(
573        workspace: &mut Workspace,
574        _: Model<ActiveCall>,
575        event: &room::Event,
576        cx: &mut ViewContext<Workspace>,
577    ) {
578        match event {
579            room::Event::ParticipantLocationChanged { participant_id }
580            | room::Event::RemoteVideoTracksChanged { participant_id } => {
581                workspace.leader_updated(*participant_id, cx);
582            }
583            _ => {}
584        }
585    }
586}
587
588#[async_trait(?Send)]
589impl CallHandler for Call {
590    fn peer_state(
591        &mut self,
592        leader_id: PeerId,
593        project: &Model<Project>,
594        cx: &mut ViewContext<Workspace>,
595    ) -> Option<(bool, bool)> {
596        let (call, _) = self.active_call.as_ref()?;
597        let room = call.read(cx).room()?.read(cx);
598        let participant = room.remote_participant_for_peer_id(leader_id)?;
599
600        let leader_in_this_app;
601        let leader_in_this_project;
602        match participant.location {
603            ParticipantLocation::SharedProject { project_id } => {
604                leader_in_this_app = true;
605                leader_in_this_project = Some(project_id) == project.read(cx).remote_id();
606            }
607            ParticipantLocation::UnsharedProject => {
608                leader_in_this_app = true;
609                leader_in_this_project = false;
610            }
611            ParticipantLocation::External => {
612                leader_in_this_app = false;
613                leader_in_this_project = false;
614            }
615        };
616
617        Some((leader_in_this_project, leader_in_this_app))
618    }
619
620    fn shared_screen_for_peer(
621        &self,
622        peer_id: PeerId,
623        pane: &View<Pane>,
624        cx: &mut ViewContext<Workspace>,
625    ) -> Option<Box<dyn ItemHandle>> {
626        let (call, _) = self.active_call.as_ref()?;
627        let room = call.read(cx).room()?.read(cx);
628        let participant = room.remote_participant_for_peer_id(peer_id)?;
629        let track = participant.video_tracks.values().next()?.clone();
630        let user = participant.user.clone();
631        for item in pane.read(cx).items_of_type::<SharedScreen>() {
632            if item.read(cx).peer_id == peer_id {
633                return Some(Box::new(item));
634            }
635        }
636
637        Some(Box::new(cx.build_view(|cx| {
638            SharedScreen::new(&track, peer_id, user.clone(), cx)
639        })))
640    }
641    fn room_id(&self, cx: &AppContext) -> Option<u64> {
642        Some(self.active_call.as_ref()?.0.read(cx).room()?.read(cx).id())
643    }
644    fn hang_up(&self, cx: &mut AppContext) -> Task<Result<()>> {
645        let Some((call, _)) = self.active_call.as_ref() else {
646            return Task::ready(Err(anyhow!("Cannot exit a call; not in a call")));
647        };
648
649        call.update(cx, |this, cx| this.hang_up(cx))
650    }
651    fn active_project(&self, cx: &AppContext) -> Option<WeakModel<Project>> {
652        ActiveCall::global(cx).read(cx).location().cloned()
653    }
654    fn invite(
655        &mut self,
656        called_user_id: u64,
657        initial_project: Option<Model<Project>>,
658        cx: &mut AppContext,
659    ) -> Task<Result<()>> {
660        ActiveCall::global(cx).update(cx, |this, cx| {
661            this.invite(called_user_id, initial_project, cx)
662        })
663    }
664    fn remote_participants(&self, cx: &AppContext) -> Option<Vec<(Arc<User>, PeerId)>> {
665        self.active_call
666            .as_ref()
667            .map(|call| {
668                call.0.read(cx).room().map(|room| {
669                    room.read(cx)
670                        .remote_participants()
671                        .iter()
672                        .map(|participant| {
673                            (participant.1.user.clone(), participant.1.peer_id.clone())
674                        })
675                        .collect()
676                })
677            })
678            .flatten()
679    }
680    fn is_muted(&self, cx: &AppContext) -> Option<bool> {
681        self.active_call
682            .as_ref()
683            .map(|call| {
684                call.0
685                    .read(cx)
686                    .room()
687                    .map(|room| room.read(cx).is_muted(cx))
688            })
689            .flatten()
690    }
691    fn toggle_mute(&self, cx: &mut AppContext) {
692        self.active_call.as_ref().map(|call| {
693            call.0.update(cx, |this, cx| {
694                this.room().map(|room| {
695                    let room = room.clone();
696                    cx.spawn(|_, mut cx| async move {
697                        room.update(&mut cx, |this, cx| this.toggle_mute(cx))??
698                            .await
699                    })
700                    .detach_and_log_err(cx);
701                })
702            })
703        });
704    }
705    fn toggle_screen_share(&self, cx: &mut AppContext) {
706        self.active_call.as_ref().map(|call| {
707            call.0.update(cx, |this, cx| {
708                this.room().map(|room| {
709                    room.update(cx, |this, cx| {
710                        if this.is_screen_sharing() {
711                            this.unshare_screen(cx).log_err();
712                        } else {
713                            let t = this.share_screen(cx);
714                            cx.spawn(move |_, _| async move {
715                                t.await.log_err();
716                            })
717                            .detach();
718                        }
719                    })
720                })
721            })
722        });
723    }
724    fn toggle_deafen(&self, cx: &mut AppContext) {
725        self.active_call.as_ref().map(|call| {
726            call.0.update(cx, |this, cx| {
727                this.room().map(|room| {
728                    room.update(cx, |this, cx| {
729                        this.toggle_deafen(cx).log_err();
730                    })
731                })
732            })
733        });
734    }
735    fn is_deafened(&self, cx: &AppContext) -> Option<bool> {
736        self.active_call
737            .as_ref()
738            .map(|call| {
739                call.0
740                    .read(cx)
741                    .room()
742                    .map(|room| room.read(cx).is_deafened())
743            })
744            .flatten()
745            .flatten()
746    }
747}
748
749#[cfg(test)]
750mod test {
751    use gpui::TestAppContext;
752
753    use crate::OneAtATime;
754
755    #[gpui::test]
756    async fn test_one_at_a_time(cx: &mut TestAppContext) {
757        let mut one_at_a_time = OneAtATime { cancel: None };
758
759        assert_eq!(
760            cx.update(|cx| one_at_a_time.spawn(cx, |_| async { Ok(1) }))
761                .await
762                .unwrap(),
763            Some(1)
764        );
765
766        let (a, b) = cx.update(|cx| {
767            (
768                one_at_a_time.spawn(cx, |_| async {
769                    assert!(false);
770                    Ok(2)
771                }),
772                one_at_a_time.spawn(cx, |_| async { Ok(3) }),
773            )
774        });
775
776        assert_eq!(a.await.unwrap(), None);
777        assert_eq!(b.await.unwrap(), Some(3));
778
779        let promise = cx.update(|cx| one_at_a_time.spawn(cx, |_| async { Ok(4) }));
780        drop(one_at_a_time);
781
782        assert_eq!(promise.await.unwrap(), None);
783    }
784}