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
use crate::prelude::*;
use subject::Subject;

#[derive(Clone)]
pub struct Publish<'a, Item>
where
  Item: Clone + Send + Sync,
{
  sbj: subjects::Subject<'a, Item>,
  source: Observable<'a, Item>,
}

impl<'a, Item> Publish<'a, Item>
where
  Item: Clone + Send + Sync,
{
  pub fn new(source: Observable<'a, Item>) -> Publish<'a, Item> {
    Publish { sbj: Subject::<Item>::new(), source }
  }

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

  pub fn connect(&self) -> Subscription<'a> {
    let sbj_next = self.sbj.clone();
    let sbj_error = self.sbj.clone();
    let sbj_complete = self.sbj.clone();

    self.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 publish(&self) -> publish::Publish<'a, Item> {
    Publish::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::interval(
      time::Duration::from_millis(100),
      new_thread_scheduler(),
    )
    .tap(
      print_next_fmt!("tap {}"),
      print_error!(),
      print_complete!(),
    )
    .publish();
    let obs = o.observable();

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

    println!("connect");
    let breaker = o.connect();
    thread::sleep(time::Duration::from_millis(500));

    println!("start #1");
    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();
    thread::sleep(time::Duration::from_millis(500));

    println!("braker");
    breaker.unsubscribe();
    thread::sleep(time::Duration::from_millis(500));
  }

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

    println!("connect");
    let breaker = o.connect();
    thread::sleep(time::Duration::from_millis(500));

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

    println!("start #1");
    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();
    thread::sleep(time::Duration::from_millis(500));

    println!("braker");
    breaker.unsubscribe();
    thread::sleep(time::Duration::from_millis(500));
  }
}