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
228
229
230
231
232
233
234
235
236
237
238
239
240
use super::error::Error;
use super::result::Result;
use cfg_if::cfg_if;
use std::str::FromStr;
use wasm_bindgen::convert::TryFromJsValue;
use wasm_bindgen::prelude::*;
use workflow_core::time::Duration;

/// `ConnectionStrategy` specifies how the WebSocket `async fn connect()`
/// function should behave during the first-time connectivity phase.
/// @category WebSocket
#[wasm_bindgen]
#[derive(Default, Clone, Copy, Debug, Eq, PartialEq)]
pub enum ConnectStrategy {
    /// Continuously attempt to connect to the server. This behavior will
    /// block `connect()` function until the connection is established.
    #[default]
    Retry,
    /// Causes `connect()` to return immediately if the first-time connection
    /// has failed.
    Fallback,
}

impl FromStr for ConnectStrategy {
    type Err = Error;
    fn from_str(s: &str) -> Result<Self> {
        match s {
            "retry" => Ok(ConnectStrategy::Retry),
            "fallback" => Ok(ConnectStrategy::Fallback),
            _ => Err(Error::InvalidConnectStrategyArg(s.to_string())),
        }
    }
}

impl ConnectStrategy {
    pub fn new(retry: bool) -> Self {
        if retry {
            ConnectStrategy::Retry
        } else {
            ConnectStrategy::Fallback
        }
    }

    pub fn is_fallback(&self) -> bool {
        matches!(self, ConnectStrategy::Fallback)
    }
}

impl TryFrom<JsValue> for ConnectStrategy {
    type Error = Error;
    fn try_from(value: JsValue) -> Result<Self> {
        if value.is_undefined() || value.is_null() {
            Ok(ConnectStrategy::default())
        } else if let Some(string) = value.as_string() {
            Ok(string.parse()?)
        } else {
            Ok(ConnectStrategy::try_from_js_value(value)?)
        }
    }
}

///
/// `ConnectOptions` is used to configure the `WebSocket` connectivity behavior.
///
/// @category WebSocket
#[derive(Clone, Debug)]
pub struct ConnectOptions {
    /// Indicates if the `async fn connect()` method should return immediately
    /// or block until the connection is established.
    pub block_async_connect: bool,
    /// [`ConnectStrategy`] used to configure the retry or fallback behavior.
    pub strategy: ConnectStrategy,
    /// Optional `url` that will change the current URL of the WebSocket.
    /// Note that the URL overrides the use of resolver.
    pub url: Option<String>,
    /// Optional `timeout` that will change the timeout of the WebSocket connection process.
    /// `Timeout` is the period after which the async connection attempt is aborted. `Timeout`
    /// is followed by the retry delay if the [`ConnectionStrategy`] is set to `Retry`.
    pub connect_timeout: Option<Duration>,
    /// Retry interval denotes the time to wait before attempting to reconnect.
    pub retry_interval: Option<Duration>,
}

pub const DEFAULT_CONNECT_TIMEOUT_MILLIS: u64 = 5_000;
pub const DEFAULT_CONNECT_RETRY_MILLIS: u64 = 5_000;

impl Default for ConnectOptions {
    fn default() -> Self {
        Self {
            block_async_connect: true,
            strategy: ConnectStrategy::Retry,
            url: None,
            connect_timeout: None,
            retry_interval: None,
        }
    }
}

impl ConnectOptions {
    pub fn fallback() -> Self {
        Self {
            block_async_connect: true,
            strategy: ConnectStrategy::Fallback,
            url: None,
            connect_timeout: None,
            retry_interval: None,
        }
    }
    pub fn reconnect_defaults() -> Self {
        Self {
            block_async_connect: true,
            strategy: ConnectStrategy::Retry,
            url: None,
            connect_timeout: None,
            retry_interval: None,
        }
    }

    pub fn passive_retry_with_defaults() -> Self {
        Self {
            block_async_connect: false,
            strategy: ConnectStrategy::Retry,
            url: None,
            connect_timeout: None,
            retry_interval: None,
        }
    }

    pub fn connect_timeout(&self) -> Duration {
        self.connect_timeout
            .unwrap_or(Duration::from_millis(DEFAULT_CONNECT_TIMEOUT_MILLIS))
    }

    pub fn retry_interval(&self) -> Duration {
        self.retry_interval
            .unwrap_or(Duration::from_millis(DEFAULT_CONNECT_RETRY_MILLIS))
    }
}

cfg_if! {
    if #[cfg(feature = "wasm32-sdk")] {
        use js_sys::Object;
        use wasm_bindgen::JsCast;
        use workflow_wasm::extensions::object::*;

        #[wasm_bindgen(typescript_custom_section)]
        const TS_CONNECT_OPTIONS: &'static str = r#"

        /**
         * `ConnectOptions` is used to configure the `WebSocket` connectivity behavior.
         * 
         * @category WebSocket
         */
        export interface IConnectOptions {
            /**
             * Indicates if the `async fn connect()` method should return immediately
             * or wait for connection to occur or fail before returning.
             * (default is `true`)
             */
            blockAsyncConnect? : boolean,
            /**
             * ConnectStrategy used to configure the retry or fallback behavior.
             * In retry mode, the WebSocket will continuously attempt to connect to the server.
             * (default is {link ConnectStrategy.Retry}).
             */
            strategy?: ConnectStrategy | string,
            /** 
             * A custom URL that will change the current URL of the WebSocket.
             * If supplied, the URL will override the use of resolver.
             */
            url?: string,
            /**
             * A custom connection timeout in milliseconds.
             */
            timeoutDuration?: number,
            /** 
             * A custom retry interval in milliseconds.
             */
            retryInterval?: number,
        }
        "#;

        #[wasm_bindgen]
        extern "C" {
            #[wasm_bindgen(typescript_type = "IConnectOptions | undefined")]
            pub type IConnectOptions;
        }

        impl TryFrom<IConnectOptions> for ConnectOptions {
            type Error = Error;
            fn try_from(args: IConnectOptions) -> Result<Self> {
                Self::try_from(&args)
            }
        }

        impl TryFrom<&IConnectOptions> for ConnectOptions {
            type Error = Error;
            fn try_from(args: &IConnectOptions) -> Result<Self> {
                let options = if let Some(args) = args.dyn_ref::<Object>() {
                    let url = args.get_value("url")?.as_string();
                    let block_async_connect = args
                        .get_value("blockAsyncConnect")?
                        .as_bool()
                        .unwrap_or(true);
                    let strategy = ConnectStrategy::try_from(args.get_value("strategy")?)?;
                    let timeout = args
                        .get_value("timeoutDuration")?
                        .as_f64()
                        .map(|f| Duration::from_millis(f as u64));
                    let retry_interval = args
                        .get_value("retryInterval")?
                        .as_f64()
                        .map(|f| Duration::from_millis(f as u64));

                    ConnectOptions {
                        block_async_connect,
                        strategy,
                        url,
                        connect_timeout: timeout,
                        retry_interval,
                        ..Default::default()
                    }
                } else if let Some(retry) = args.as_bool() {
                    ConnectOptions {
                        block_async_connect: true,
                        strategy: ConnectStrategy::new(retry),
                        url: None,
                        connect_timeout: None,
                        retry_interval: None,
                        ..Default::default()
                    }
                } else {
                    ConnectOptions::default()
                };

                Ok(options)
            }
        }
    }
}