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
//!
//! EventDispatcher - subscription-based channel multiplexer client - WASM client.
//!

use crate::abi::ref_from_abi;
use crate::error::Error;
use crate::result::Result;
use futures::{select, FutureExt};
use js_sys::Function;
use serde::Serialize;
use serde_wasm_bindgen::to_value;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use wasm_bindgen::prelude::*;
use workflow_core::channel::{DuplexChannel, Multiplexer, MultiplexerChannel};
use workflow_core::sendable::Sendable;
use workflow_core::task::*;
use workflow_log::log_error;

pub struct Inner {
    callback: Mutex<Option<Sendable<Function>>>,
    task_running: AtomicBool,
    task_ctl: DuplexChannel,
}

///
/// [`EventDispatcher`] is an object meant to be used in WASM environment to
/// process channel events.
///
#[wasm_bindgen(inspectable)]
#[derive(Clone)]
pub struct EventDispatcher {
    inner: Arc<Inner>,
}

impl Default for EventDispatcher {
    fn default() -> Self {
        EventDispatcher::new()
    }
}

impl EventDispatcher {
    pub async fn start_notification_task<T>(&self, multiplexer: &Multiplexer<T>) -> Result<()>
    where
        T: Clone + Serialize + Send + Sync + 'static,
    {
        let inner = self.inner.clone();

        if inner.task_running.load(Ordering::SeqCst) {
            panic!("ReflectorClient task is already running");
        }
        let ctl_receiver = inner.task_ctl.request.receiver.clone();
        let ctl_sender = inner.task_ctl.response.sender.clone();
        inner.task_running.store(true, Ordering::SeqCst);

        let channel = MultiplexerChannel::from(multiplexer);

        spawn(async move {
            loop {
                select! {
                    _ = ctl_receiver.recv().fuse() => {
                        break;
                    },
                    msg = channel.receiver.recv().fuse() => {
                        // log_info!("notification: {:?}",msg);
                        if let Ok(notification) = &msg {
                            if let Some(callback) = inner.callback.lock().unwrap().as_ref() {
                                // if let Ok(event) = JsValue::try_from(notification) {
                                if let Ok(event) = to_value(notification) {
                                    if let Err(err) = callback.0.call1(&JsValue::undefined(), &event) {
                                        log_error!("Error while executing notification callback: {:?}", err);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            channel.close();
            ctl_sender.send(()).await.ok();
        });

        Ok(())
    }
}

#[wasm_bindgen]
impl EventDispatcher {
    #[wasm_bindgen(constructor)]
    pub fn new() -> EventDispatcher {
        EventDispatcher {
            inner: Arc::new(Inner {
                callback: Mutex::new(None),
                task_running: AtomicBool::new(false),
                task_ctl: DuplexChannel::oneshot(),
            }),
        }
    }

    #[wasm_bindgen(getter)]
    pub fn listener(&self) -> JsValue {
        if let Some(callback) = self.inner.callback.lock().unwrap().as_ref() {
            callback.as_ref().clone().into()
        } else {
            JsValue::UNDEFINED
        }
    }

    #[wasm_bindgen(setter, js_name = "listener")]
    pub fn listener_setter(&self, callback: JsValue) -> Result<()> {
        if callback.is_function() {
            let fn_callback: Function = callback.into();
            self.inner
                .callback
                .lock()
                .unwrap()
                .replace(fn_callback.into());
        } else {
            self.remove_listener()?;
        }
        Ok(())
    }

    #[wasm_bindgen(js_name = "registerListener")]
    pub fn register_listener(&self, callback: JsValue) -> Result<()> {
        if callback.is_function() {
            let fn_callback: Function = callback.into();
            self.inner
                .callback
                .lock()
                .unwrap()
                .replace(fn_callback.into());
        } else {
            self.remove_listener()?;
        }
        Ok(())
    }

    /// `removeListenet` must be called when releasing ReflectorClient
    /// to stop the background event processing task
    #[wasm_bindgen(js_name = "removeListener")]
    pub fn remove_listener(&self) -> Result<()> {
        *self.inner.callback.lock().unwrap() = None;
        Ok(())
    }

    #[wasm_bindgen(js_name = "stop")]
    pub async fn stop_notification_task(&self) -> Result<()> {
        let inner = &self.inner;
        if inner.task_running.load(Ordering::SeqCst) {
            inner.task_running.store(false, Ordering::SeqCst);
            inner
                .task_ctl
                .signal(())
                .await
                .map_err(|err| JsValue::from_str(&err.to_string()))?;
        }
        Ok(())
    }
}

impl TryFrom<JsValue> for EventDispatcher {
    type Error = Error;

    fn try_from(js_value: JsValue) -> std::result::Result<Self, Self::Error> {
        Ok(ref_from_abi!(EventDispatcher, &js_value)?)
    }
}