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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
use crate::prelude::*;
use std::sync::{Arc, RwLock};
use subject::Subject;

#[derive(Clone)]
pub struct RefCount<'a, Item>
where
  Item: Clone + Send + Sync,
{
  subject: subjects::Subject<'a, Item>,
  source: Observable<'a, Item>,
  subscription: Arc<RwLock<Option<Subscription<'a>>>>,
}

impl<'a, Item> RefCount<'a, Item>
where
  Item: Clone + Send + Sync,
{
  pub fn new(source: Observable<'a, Item>) -> RefCount<'a, Item> {
    let _self = RefCount {
      subject: Subject::<Item>::new(),
      source,
      subscription: Arc::new(RwLock::new(None)),
    };
    _self.set_ref_count();
    _self
  }

  pub fn observable(&self) -> Observable<'a, Item> {
    self.subject.observable()
  }

  fn set_ref_count(&self) {
    {
      let subscription = Arc::clone(&self.subscription);
      self.subject.set_on_unsubscribe(move |count| {
        if count == 0 {
          if let Some(sbsc) = &*subscription.read().unwrap() {
            sbsc.unsubscribe();
          }
        }
      });
    }

    let source = self.source.clone();
    let subject = self.subject.clone();
    let subscription = Arc::clone(&self.subscription);

    self.subject.set_on_subscribe(move |count| {
      if count == 1 {
        // connect
        let sbj_next = subject.clone();
        let sbj_error = subject.clone();
        let sbj_complete = subject.clone();

        let mut subscription = subscription.write().unwrap();
        if subscription.is_some() {
          return;
        }

        *subscription = Some(source.subscribe(
          move |x| {
            sbj_next.next(x);
          },
          move |e| {
            sbj_error.error(e);
          },
          move || {
            sbj_complete.complete();
          },
        ));
      }
    });
  }
}

impl<'a, Item> Observable<'a, Item>
where
  Item: Clone + Send + Sync,
{
  pub fn ref_count(&self) -> RefCount<'a, Item> {
    RefCount::new(self.clone())
  }
}

#[cfg(all(test, not(feature = "web")))]
mod test {
  use crate::prelude::*;
  use crate::{print_complete, print_error, print_next_fmt};
  use schedulers::new_thread_scheduler;
  use std::{thread, time};

  #[test]
  fn basic() {
    let o = observables::from_iter(0..10)
      .tap(
        print_next_fmt!("tap {}"),
        print_error!(),
        print_complete!(),
      )
      .ref_count();
    let obs = o.observable();

    println!("start #1");
    let sbsc1 = obs.subscribe(
      print_next_fmt!("#1 {}"),
      print_error!(),
      print_complete!(),
    );

    println!("start #2");
    let sbsc2 = obs.subscribe(
      print_next_fmt!("#2 {}"),
      print_error!(),
      print_complete!(),
    );

    println!("end #1");
    sbsc1.unsubscribe();

    println!("end #2");
    sbsc2.unsubscribe();
  }

  #[test]
  fn thread() {
    let o = observables::interval(
      time::Duration::from_millis(100),
      new_thread_scheduler(),
    )
    .tap(
      print_next_fmt!("tap {}"),
      print_error!(),
      print_complete!(),
    )
    .ref_count();
    let obs = o.observable();

    println!("start #1");
    let sbsc1 = obs.subscribe(
      print_next_fmt!("#1 {}"),
      print_error!(),
      print_complete!(),
    );

    thread::sleep(time::Duration::from_millis(500));

    println!("start #2");
    let sbsc2 = obs.subscribe(
      print_next_fmt!("#2 {}"),
      print_error!(),
      print_complete!(),
    );

    thread::sleep(time::Duration::from_millis(500));

    println!("end #1");
    sbsc1.unsubscribe();

    thread::sleep(time::Duration::from_millis(500));

    println!("end #2");
    sbsc2.unsubscribe();

    println!("final wait start");
    thread::sleep(time::Duration::from_millis(1000));
    println!("final wait end");
  }
}