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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use crate::error::Error;
use cfg_if::cfg_if;
use chrome_sys::storage;
use js_sys::{Array, Object};
use wasm_bindgen::prelude::*;
use workflow_core::task::call_async_no_send;

pub struct LocalStorage;

impl LocalStorage {
    pub async fn set_item(key: &str, value: &str) -> Result<JsValue, JsValue> {
        let key = key.to_string();
        let value = value.to_string();
        call_async_no_send!(async move {
            let data = Object::new();
            js_sys::Reflect::set(&data, &key.into(), &value.into())?;
            storage::set(data.into()).await
        })
    }

    pub async fn get_item(key: &str) -> Result<Option<String>, JsValue> {
        let _key = key.to_string();
        let obj = call_async_no_send!(storage::get(_key).await)?;
        Ok(js_sys::Reflect::get(&obj, &key.into())?.as_string())
    }

    pub async fn get_items(keys: Vec<&str>) -> Result<StorageData, JsValue> {
        let keys = keys.iter().map(|k| k.to_string()).collect::<Vec<_>>();
        Ok(call_async_no_send!(async move {
            let query = Array::new();
            for key in keys {
                query.push(&key.into());
            }
            storage::get_items(query).await
        })?
        .try_into()?)
    }

    pub async fn get_all() -> Result<StorageData, JsValue> {
        Ok(call_async_no_send!(storage::get_all().await)?.try_into()?)
    }

    pub async fn keys() -> Result<Vec<String>, JsValue> {
        Ok(Self::get_all().await?.keys())
    }

    pub async fn remove_item(key: &str) -> Result<(), JsValue> {
        let key = key.to_string();
        call_async_no_send!(storage::remove(key).await)
    }

    pub async fn rename_item(from_key: &str, to_key: &str) -> Result<(), Error> {
        let from_key = from_key.to_string();
        let to_key = to_key.to_string();

        if Self::get_item(&to_key).await?.is_some() {
            return Err(Error::KeyExists(to_key));
        }
        if let Some(existing) = Self::get_item(&from_key).await? {
            Self::set_item(&to_key, &existing).await?;
            Self::remove_item(&from_key).await?;
            Ok(())
        } else {
            Err(Error::MissingKey(from_key))
        }
    }

    pub async fn remove_items(keys: Vec<&str>) -> Result<(), JsValue> {
        let keys = keys.iter().map(|k| k.to_string()).collect::<Vec<_>>();
        call_async_no_send!(async move {
            let query = Array::new();
            for key in keys {
                query.push(&key.into());
            }
            storage::remove_items(query).await
        })
    }

    pub async fn clear() -> Result<(), JsValue> {
        call_async_no_send!(storage::clear().await)
    }

    #[cfg(debug_assertions)]
    pub async fn unit_tests() -> Result<(), String> {
        use workflow_core::sendable::Sendable;

        let old_data = Sendable(Self::get_all().await.unwrap());
        let error = Sendable(test_impl().await.err());
        let old_data_clone = old_data.clone();
        call_async_no_send!(storage::set(old_data.unwrap().inner.into()).await).unwrap();

        let new_data = Self::get_all().await.unwrap();
        for key in new_data.keys() {
            let new_value = new_data.get(&key).unwrap();
            let old_value = old_data_clone.get(&key).unwrap();
            if new_value != old_value {
                return Err(format!(
                    "[WARNING] Data restore failed: {key} => {old_value:?} != {new_value:?}"
                ));
            }
        }

        if let Some(err) = error.unwrap() {
            return Err(err.as_string().unwrap_or(format!("{err:?}")));
        }
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub struct StorageData {
    pub inner: Object,
}

impl TryFrom<JsValue> for StorageData {
    type Error = JsError;
    fn try_from(inner: JsValue) -> Result<Self, Self::Error> {
        if !inner.is_object() {
            return Err(JsError::new(&format!(
                "Invalid JsValue: cant convert JsValue ({inner:?}) to StorageData."
            )));
        }
        let inner = Object::from(inner);
        Ok(Self { inner })
    }
}

impl StorageData {
    pub fn keys(&self) -> Vec<String> {
        let mut keys = vec![];
        for key in Object::keys(&self.inner) {
            keys.push(key.as_string().unwrap());
        }

        keys
    }

    pub fn has(&self, key: &str) -> bool {
        self.inner.has_own_property(&key.into())
    }

    pub fn get_value(&self, key: &str) -> Result<Option<JsValue>, JsValue> {
        let value = js_sys::Reflect::get(&self.inner, &key.into())?;
        if value.eq(&JsValue::UNDEFINED) {
            Ok(None)
        } else {
            Ok(Some(value))
        }
    }

    pub fn get(&self, key: &str) -> Result<Option<String>, JsValue> {
        let value = js_sys::Reflect::get(&self.inner, &key.into())?;
        if value.eq(&JsValue::UNDEFINED) {
            Ok(None)
        } else {
            Ok(value.as_string())
        }
    }
}

#[cfg(debug_assertions)]
macro_rules! assert_test {
    ($name:literal, $cond1:expr, $cond2:expr) => {{
        if $cond1 != $cond2 {
            return Result::<(), JsValue>::Err(
                format!(
                    "{} => {}, {:?} != {:?}",
                    $name,
                    stringify!($cond1),
                    $cond1,
                    $cond2
                )
                .into(),
            );
        }
    }};
}

#[cfg(debug_assertions)]
async fn test_impl() -> Result<(), JsValue> {
    LocalStorage::clear().await?;
    {
        let empty_data = LocalStorage::get_all().await?;
        assert_test!("Key length should be 0", empty_data.keys().len(), 0);
    }
    {
        LocalStorage::set_item("key1", "value-1").await?;
        let data = LocalStorage::get_all().await?;
        assert_test!("Key length should be 1", data.keys().len(), 1);
        assert_test!("'key1' key should be there", data.has("key1"), true);
        assert_test!(
            "value for 'key1' key should be 'value-1'",
            data.get("key1")?.unwrap(),
            "value-1"
        );
    }
    {
        let item = LocalStorage::get_item("key1").await?.unwrap();
        assert_test!("value for 'key1' key should be 'value-1'", item, "value-1");
    }
    {
        LocalStorage::set_item("key2", "value-2").await?;
        let data = LocalStorage::get_all().await?;
        assert_test!("Key length should be 2", data.keys().len(), 2);
        assert_test!("'key2' key should be there", data.has("key2"), true);
        assert_test!(
            "value for 'key2' key should be 'value-2'",
            data.get("key2")?.unwrap(),
            "value-2"
        );
    }
    {
        let item = LocalStorage::get_item("key2").await?.unwrap();
        assert_test!("value for 'key2' key should be 'value-2'", item, "value-2");
    }
    {
        LocalStorage::set_item("key3", "value-3").await?;
        let data = LocalStorage::get_all().await?;
        assert_test!("Key length should be 3", data.keys().len(), 3);
        assert_test!("'key3' key should be there", data.has("key3"), true);
        assert_test!(
            "value for 'key3' key should be 'value-3'",
            data.get("key3")?.unwrap(),
            "value-3"
        );
    }
    {
        let data = LocalStorage::get_items(vec!["key2", "key3"]).await?;
        assert_test!("Key length should be 2", data.keys().len(), 2);
        assert_test!("'key2' key should be there", data.has("key2"), true);
        assert_test!("'key3' key should be there", data.has("key3"), true);
        assert_test!(
            "value for 'key2' key should be 'value-2'",
            data.get("key2")?.unwrap(),
            "value-2"
        );
        assert_test!(
            "value for 'key3' key should be 'value-3'",
            data.get("key3")?.unwrap(),
            "value-3"
        );
    }
    {
        LocalStorage::remove_item("key2").await?;
        let data = LocalStorage::get_all().await?;
        assert_test!(
            "After remove_item, Key length should be 2",
            data.keys().len(),
            2
        );
        assert_test!("'key2' key should not be there", data.has("key2"), false);
        assert_test!(
            "value for 'key2' key should be None",
            data.get("key2")?,
            Option::<String>::None
        );
    }
    {
        LocalStorage::clear().await?;
        let data = LocalStorage::get_all().await?;
        assert_test!("After clear, Key length should be 0", data.keys().len(), 0);
        assert_test!("'key1' key should not be there", data.has("key1"), false);
        assert_test!(
            "value for 'key2' key should be None",
            data.get("key2")?,
            Option::<String>::None
        );
    }

    Ok(())
}

pub async fn __chrome_storage_unit_test() {
    cfg_if! {
        if #[cfg(all(target_arch = "wasm32", debug_assertions))] {
            if !workflow_core::runtime::is_chrome_extension() {
                workflow_log::log_info!("ChromeStorage::test() FAILED: these are unit tests for chrome extension storage api.");
                return
            }
            use LocalStorage as ChromeStorage;
            match ChromeStorage::unit_tests().await{
                Ok(_)=>workflow_log::log_info!("ChromeStorage::test() PASSED"),
                Err(err)=>workflow_log::log_error!("ChromeStorage::test() FAILED: {err:?}")
            };
        }
    }
}