1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use std::sync::{Arc, Mutex};

use crate::{
    receive::{Receive, ReceiverResult},
    view::{DeleteView, View},
};

#[derive(Clone, Debug)]
pub struct ArcLinked<R> {
    pub(super) link: Arc<Mutex<Option<R>>>,
}

impl<R> ArcLinked<R> {
    pub fn get_reciever(&self) -> &Mutex<Option<R>> {
        self.link.as_ref()
    }
}

impl<E, R: Receive<E>> Receive<E> for ArcLinked<R> {
    type Output = R::Output;

    fn send(&mut self, event: E) -> ReceiverResult<E, Self::Output> {
        match self.link.lock().unwrap().as_mut() {
            Some(t0) => t0.send(event),
            None => ReceiverResult::Delete(event),
        }
    }
}

impl<E, R: View<E>> View<E> for ArcLinked<R> {
    fn view(&mut self, event: &E) -> Option<DeleteView> {
        match self.link.lock().unwrap().as_mut() {
            Some(viewer) => viewer.view(event),
            None => Some(DeleteView),
        }
    }
}