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
use crate::internals::function_wrapper::FunctionWrapper;
use crate::internals::stream_controller::*;
use crate::prelude::*;

#[derive(Clone)]
pub struct CombineLatest<'a, Item, Out>
where
  Item: Clone + Send + Sync,
  Out: Clone + Send + Sync,
{
  zip_op: operators::Zip<'a, Item>,
  combine_f: FunctionWrapper<'a, Vec<Item>, Out>,
}

impl<'a, Item, Out> CombineLatest<'a, Item, Out>
where
  Item: Clone + Send + Sync,
  Out: Clone + Send + Sync,
{
  pub fn new<F>(
    observables: &[Observable<'a, Item>],
    f: F,
  ) -> CombineLatest<'a, Item, Out>
  where
    F: Fn(Vec<Item>) -> Out + Send + Sync + 'a,
  {
    CombineLatest {
      zip_op: operators::Zip::new(observables),
      combine_f: FunctionWrapper::new(f),
    }
  }
  pub fn execute(&self, source: Observable<'a, Item>) -> Observable<'a, Out> {
    let zip_op = self.zip_op.clone();
    let combine_f = self.combine_f.clone();

    Observable::create(move |s| {
      let source = source.clone();
      let combine_f = combine_f.clone();

      let sctl = StreamController::new(s);

      let sctl_next = sctl.clone();
      let sctl_error = sctl.clone();
      let sctl_complete = sctl.clone();

      zip_op.execute(source).inner_subscribe(sctl.new_observer(
        move |_, x| {
          sctl_next.sink_next(combine_f.call(x));
        },
        move |_, e| {
          sctl_error.sink_error(e);
        },
        move |serial| {
          sctl_complete.sink_complete(&serial);
        },
      ));
    })
  }
}

impl<'a, Item> Observable<'a, Item>
where
  Item: Clone + Send + Sync,
{
  pub fn combine_latest<Out, F>(
    &self,
    observables: &[Observable<'a, Item>],
    f: F,
  ) -> Observable<'a, Out>
  where
    Out: Clone + Send + Sync,
    F: Fn(Vec<Item>) -> Out + Send + Sync + 'a,
  {
    CombineLatest::new(observables, f).execute(self.clone())
  }
}

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

  #[test]
  fn basic() {
    observables::from_iter(0..10)
      .combine_latest(
        &[
          observables::from_iter(10..20),
          observables::from_iter(20..30),
        ],
        |v| format!("function {:?}", v),
      )
      .subscribe(
        print_next_fmt!("{:?}"),
        print_error!(),
        print_complete!(),
      );
  }

  #[test]
  fn thread() {
    observables::from_iter(0..10)
      .observe_on(schedulers::new_thread_scheduler())
      .combine_latest(
        &[
          observables::from_iter(10..20)
            .observe_on(schedulers::new_thread_scheduler()),
          observables::from_iter(20..30)
            .observe_on(schedulers::new_thread_scheduler()),
        ],
        |v| format!("function {:?}", v),
      )
      .subscribe(
        print_next_fmt!("{:?}"),
        print_error!(),
        print_complete!(),
      );
    thread::sleep(time::Duration::from_millis(1000));
  }
}