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
use std::marker::PhantomData;

use crate::internals::function_wrapper::*;
use crate::internals::stream_controller::*;
use crate::prelude::*;

#[derive(Clone)]
pub struct RetryWhen<'a, Item>
where
  Item: Clone + Send + Sync,
{
  predicate_f: FunctionWrapper<'a, RxError, bool>,
  _item: PhantomData<Item>,
}

impl<'a, Item> RetryWhen<'a, Item>
where
  Item: Clone + Send + Sync,
{
  pub fn new<F>(f: F) -> RetryWhen<'a, Item>
  where
    F: Fn(RxError) -> bool + Send + Sync + 'a,
  {
    RetryWhen {
      predicate_f: FunctionWrapper::new(f),
      _item: PhantomData,
    }
  }

  pub fn execute(&self, source: Observable<'a, Item>) -> Observable<'a, Item> {
    let f = self.predicate_f.clone();

    Observable::<Item>::create(move |s| {
      fn do_subscribe<'a, Item>(
        predicate: FunctionWrapper<'a, RxError, bool>,
        source: Observable<'a, Item>,
        sctl: StreamController<'a, Item>,
      ) where
        Item: Clone + Send + Sync,
      {
        let sctl_next = sctl.clone();
        let sctl_error = sctl.clone();
        let sctl_complete = sctl.clone();
        let source_error = source.clone();
        source.inner_subscribe(sctl.new_observer(
          move |_, x: Item| {
            sctl_next.sink_next(x);
          },
          move |serial, e| {
            if predicate.call(e.clone()) {
              sctl_error.upstream_abort_observe(&serial);
              do_subscribe(
                predicate.clone(),
                source_error.clone(),
                sctl_error.clone(),
              );
            } else {
              sctl_error.sink_error(e);
            }
          },
          move |serial| sctl_complete.sink_complete(&serial),
        ));
      }

      let sctl = StreamController::new(s);
      do_subscribe(f.clone(), source.clone(), sctl.clone());
    })
  }
}

impl<'a, Item> Observable<'a, Item>
where
  Item: Clone + Send + Sync,
{
  pub fn retry_when<F>(&self, f: F) -> Observable<'a, Item>
  where
    F: Fn(RxError) -> bool + Send + Sync + 'a,
  {
    RetryWhen::new(f).execute(self.clone())
  }
}

#[cfg(test)]
mod test {
  use crate::prelude::*;
  use std::sync::{Arc, RwLock};

  #[test]
  fn basic() {
    let counter = Arc::new(RwLock::new(0));
    let counter_ob = Arc::clone(&counter);
    let o = Observable::create(move |s| {
      let c = *counter_ob.read().unwrap();
      println!("#{}", c + 1);
      s.next(c * 100 + 0);
      s.next(c * 100 + 1);
      *counter_ob.write().unwrap() += 1;
      if c < 5 {
        s.error(RxError::from_error("ERR!"));
      } else {
        s.complete();
      }
    });

    o.retry_when(|e| {
      println!(
        "retry_when {:?}",
        e.downcast_ref::<&str>()
      );
      *counter.read().unwrap() < 2
    })
    .subscribe(
      print_next_fmt!("{}"),
      print_error_as!(&str),
      print_complete!(),
    );
  }
}