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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use std::sync::{Arc, Mutex};

use handler::Handler;

use common::{RawFunc, Subscription, SubscriptionFunc};

#[derive(Clone)]
pub struct MonadIO<Y> {
    effect: Arc<Mutex<dyn FnMut() -> Y + Send + Sync + 'static>>,
    ob_handler: Option<Arc<Mutex<Handler>>>,
    sub_handler: Option<Arc<Mutex<Handler>>>,
}

pub fn of<Z: 'static + Send + Sync + Clone>(r: Z) -> impl FnMut() -> Z + Send + Sync + 'static {
    let _r = Box::new(r);

    return move || {
        let r = _r.clone();
        *r
    };
}

impl<Y: 'static + Send + Sync + Clone> MonadIO<Y> {
    pub fn just(r: Y) -> MonadIO<Y> {
        let _r = Box::new(r);

        return MonadIO::new(move || {
            let r = _r.clone();
            *r
        });
    }

    pub fn new(effect: impl FnMut() -> Y + Send + Sync + 'static) -> MonadIO<Y> {
        return MonadIO::new_with_handlers(effect, None, None);
    }

    pub fn new_with_handlers(
        effect: impl FnMut() -> Y + Send + Sync + 'static,
        ob: Option<Arc<Mutex<Handler + 'static>>>,
        sub: Option<Arc<Mutex<Handler + 'static>>>,
    ) -> MonadIO<Y> {
        return MonadIO {
            effect: Arc::new(Mutex::new(effect)),
            ob_handler: ob,
            sub_handler: sub,
        };
    }

    pub fn observe_on(&mut self, h: Option<Arc<Mutex<Handler + 'static>>>) {
        self.ob_handler = h;
    }

    pub fn subscribe_on(&mut self, h: Option<Arc<Mutex<Handler + 'static>>>) {
        self.sub_handler = h;
    }

    pub fn map<Z: 'static + Send + Sync + Clone>(
        self,
        func: impl FnMut(Y) -> Z + Send + Sync + 'static + Clone,
    ) -> MonadIO<Z> {
        let _func = Arc::new(func);
        let mut _effect = self.effect;

        return MonadIO::new_with_handlers(
            move || {
                let mut func = _func.clone();

                let effect = &mut *_effect.lock().unwrap();

                (Arc::make_mut(&mut func))((effect)())
            },
            self.ob_handler,
            self.sub_handler,
        );
    }
    pub fn fmap<Z: 'static + Send + Sync + Clone>(
        self,
        func: impl FnMut(Y) -> MonadIO<Z> + Send + Sync + 'static + Clone,
    ) -> MonadIO<Z> {
        let mut _func = Arc::new(func);

        return self.map(move |y: Y| {
            let mut func = _func.clone();
            let mut _effect = (Arc::make_mut(&mut func))(y).effect;

            let effect = &mut *_effect.lock().unwrap();

            (effect)()
        });
    }
    pub fn subscribe(&self, s: Arc<impl Subscription<Y> + Clone>) {
        let mut _effect = self.effect.clone();
        let mut _do_ob = Arc::new(move || {
            let effect = &mut *_effect.lock().unwrap();

            return (effect)();
        });
        let mut _s = s.clone();
        let mut _do_sub = Arc::new(move |y: Y| {
            Arc::make_mut(&mut _s).on_next(Arc::new(y));
        });

        match &self.ob_handler {
            Some(ob_handler) => {
                let mut sub_handler_thread = Arc::new(self.sub_handler.clone());
                ob_handler.lock().unwrap().post(RawFunc::new(move || {
                    let mut do_ob_thread_ob = _do_ob.clone();
                    let mut do_sub_thread_ob = _do_sub.clone();
                    let ob = Arc::make_mut(&mut do_ob_thread_ob);
                    let sub = Arc::make_mut(&mut do_sub_thread_ob);

                    match Arc::make_mut(&mut sub_handler_thread) {
                        Some(ref mut sub_handler) => {
                            let mut do_ob_thread_sub = _do_ob.clone();
                            let mut do_sub_thread_sub = _do_sub.clone();

                            sub_handler.lock().unwrap().post(RawFunc::new(move || {
                                let ob = Arc::make_mut(&mut do_ob_thread_sub);
                                let sub = Arc::make_mut(&mut do_sub_thread_sub);

                                (sub)((ob)());
                            }));
                        }
                        None => {
                            (sub)((ob)());
                        }
                    }
                }));
            }
            None => {
                let effect = Arc::make_mut(&mut _do_ob);
                let sub = Arc::make_mut(&mut _do_sub);
                sub(effect());
            }
        }
    }
    pub fn subscribe_fn(&self, func: impl FnMut(Arc<Y>) + Send + Sync + 'static + Clone) {
        self.subscribe(Arc::new(SubscriptionFunc::new(func)))
    }
}

#[test]
fn test_monadio_new() {
    use common::SubscriptionFunc;
    use handler::HandlerThread;
    use std::sync::{Arc, Condvar, Mutex};
    use std::{thread, time};

    let monadio_simple = MonadIO::just(3);
    // let mut monadio_simple = MonadIO::just(3);
    {
        let effect = &mut *monadio_simple.effect.lock().unwrap();
        assert_eq!(3, (effect)());
    }
    let monadio_simple_map = monadio_simple.map(|x| x * 3);

    monadio_simple_map.subscribe_fn(move |x| {
        println!("monadio_simple_map {:?}", x);
        assert_eq!(9, *Arc::make_mut(&mut x.clone()));
    });

    // fmap & map (sync)
    let mut _subscription = Arc::new(SubscriptionFunc::new(move |x: Arc<u16>| {
        println!("monadio_sync {:?}", x); // monadio_sync 36
        assert_eq!(36, *Arc::make_mut(&mut x.clone()));
    }));
    let subscription = _subscription.clone();
    let monadio_sync = MonadIO::just(1)
        .fmap(|x| MonadIO::new(move || x * 4))
        .map(|x| x * 3)
        .map(|x| x * 3);
    monadio_sync.subscribe(subscription);

    // fmap & map (async)
    let mut _handler_observe_on = HandlerThread::new_with_mutex();
    let mut _handler_subscribe_on = HandlerThread::new_with_mutex();
    let monadio_async = MonadIO::new_with_handlers(
        || {
            println!("In string");
            String::from("ok")
        },
        Some(_handler_observe_on.clone()),
        Some(_handler_subscribe_on.clone()),
    );

    let pair = Arc::new((Mutex::new(false), Condvar::new()));
    let pair2 = pair.clone();

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

    let subscription = Arc::new(SubscriptionFunc::new(move |x: Arc<String>| {
        println!("monadio_async {:?}", x); // monadio_async ok

        let &(ref lock, ref cvar) = &*pair2;
        let mut started = lock.lock().unwrap();
        *started = true;

        cvar.notify_one(); // Unlock here
    }));
    monadio_async.subscribe(subscription);
    monadio_async.subscribe(Arc::new(SubscriptionFunc::new(move |x: Arc<String>| {
        println!("monadio_async sub2 {:?}", x); // monadio_async sub2 ok
    })));
    {
        let mut handler_observe_on = _handler_observe_on.lock().unwrap();
        let mut handler_subscribe_on = _handler_subscribe_on.lock().unwrap();

        println!("hh2");
        handler_observe_on.start();
        handler_subscribe_on.start();
        println!("hh2 running");

        handler_observe_on.post(RawFunc::new(move || {}));
        handler_observe_on.post(RawFunc::new(move || {}));
        handler_observe_on.post(RawFunc::new(move || {}));
        handler_observe_on.post(RawFunc::new(move || {}));
        handler_observe_on.post(RawFunc::new(move || {}));
    }
    thread::sleep(time::Duration::from_millis(100));

    let &(ref lock, ref cvar) = &*pair;
    let mut started = lock.lock().unwrap();
    // Waiting for being unlcoked
    while !*started {
        started = cvar.wait(started).unwrap();
    }
}