workflow_nw/
window.rs

1use crate::result::Result;
2use nw_sys::prelude::*;
3use wasm_bindgen::prelude::*;
4use workflow_core::channel::oneshot;
5use workflow_wasm::callback::Callback;
6
7pub async fn get_all_async() -> Result<Vec<Window>> {
8    let (sender, receiver) = oneshot();
9    let callback = Callback::new(move |windows: JsValue| {
10        let array = windows
11            .dyn_into::<js_sys::Array>()
12            .expect("nw_sys::window::get_all_async() error converting to window array");
13        let windows = array
14            .to_vec()
15            .into_iter()
16            .map(|window: JsValue| window.unchecked_into::<Window>())
17            .collect::<Vec<Window>>();
18
19        sender.try_send(windows).unwrap();
20    });
21
22    nw_sys::window::get_all(callback.as_ref());
23    let result = receiver.recv().await?;
24
25    Ok(result)
26}
27
28// this can be used to test...
29// pub async fn get_all_async() -> Result<Vec<Window>> {
30//     let (sender, receiver) = oneshot();
31//     let callback = Callback::new_with_args_2(move |windows: JsValue, v2: JsValue| {
32//         log_info!("windows: {:?}", windows);
33//         log_info!("v2: {:?}", v2);
34
35//         sender.try_send(Sendable(windows)).unwrap();
36//     });
37
38//     nw_sys::window::get_all(callback.as_ref());
39//     let windows = receiver.recv().await?;
40//     let length = Reflect::get(&windows, &"length".into())?;
41//     log_info!("length: {:?}", length);
42//     let length = length.as_f64().unwrap() as usize;
43//     let result = (0..length)
44//         .into_iter()
45//         .map(|index| {
46//             Reflect::get(&windows, &index.into())
47//                 .expect("failed to get nw window")
48//                 .unchecked_into::<Window>()
49//         })
50//         .collect::<Vec<_>>();
51
52//     Ok(result)
53// }