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
//! ## Usage
//!
//! Cargo.toml:
//!
//! ```toml
//! [dependencies]
//! tauri-async-handler = "0.4"
//! ```
//!
//! src-tauri/main.rs:
//!
//! ```rust
//! mod cmd;
//!
//!
//! use serde_json::json;
//! use tauri_async_handler::*;
//!
//! fn main() {
//!   tauri::AppBuilder::new()
//!     .async_handler(None, |cmd: cmd::Cmd| async {
//!       use cmd::Cmd::*;
//!       Ok(match cmd {
//!         MyCustomCommand{ argument } => {
//!           println!("arg {}", argument);
//!           let world = "world";
//!           json!({
//!             "hello": world
//!           })
//!         }
//!       })
//!     })
//!     .build()
//!     .run();
//! }
//!
//! ```
//!
//! JavaScript:
//!
//! ```javascript
//! const myCustomCommand = (argument) => {
//!   return window.tauri.promisified({
//!     cmd: 'myCustomCommand',
//!     argument,
//!   })
//! }
//! myCustomCommand.then((r) => console.log('myCustomCommand', r))
//! ```

use async_std::task::spawn;
use futures_channel::mpsc;
use futures_util::stream::StreamExt;
use serde_derive::Deserialize;
use serde_json::Value;
use tauri::AppBuilder;
use tauri::{Result, WebviewMut};

fn map_err<E: std::error::Error>(e: E) -> String {
    e.to_string()
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct CallbackCmd<T> {
    #[serde(flatten)]
    cmd: T,
    callback: String,
    error: String,
}

struct Command<T>(T, WebviewMut);

pub trait AppBuilderExt {
    fn async_handler<C, F, Fut>(self, limit: impl Into<Option<usize>>, invoke_handler: F) -> Self
    where
        C: serde::de::DeserializeOwned + Send + 'static,
        F: FnMut(C) -> Fut + Send + 'static,
        Fut: std::future::Future<Output = Result<Value>> + Send;
}

fn json_string(value: Value) -> String {
    serde_json::to_string(&value).expect("Failed to encode json")
}

fn execute_callback(
    mut handle: WebviewMut,
    result: Result<Value>,
    callback: String,
    error: String,
) {
    handle
        .dispatch(|mut webview| {
            tauri::execute_promise_sync(&mut webview, || result.map(json_string), callback, error)
                .expect("Failed to execute promise");
        })
        .expect("Failed to dispatch");
}

impl AppBuilderExt for AppBuilder {
    fn async_handler<C, F, Fut>(
        self,
        limit: impl Into<Option<usize>>,
        mut invoke_handler: F,
    ) -> Self
    where
        C: serde::de::DeserializeOwned + Send + 'static,
        F: FnMut(C) -> Fut + Send + 'static,
        Fut: std::future::Future<Output = Result<Value>> + Send,
    {
        let limit = limit.into();
        let (mut tx, rx) = mpsc::channel::<Command<CallbackCmd<C>>>(10);

        spawn(async move {
            rx.for_each_concurrent(limit, move |command| {
                let Command(
                    CallbackCmd {
                        cmd,
                        callback,
                        error,
                    },
                    handle,
                ) = command;
                let fut = invoke_handler(cmd);
                async {
                    execute_callback(handle, fut.await, callback, error);
                }
            })
            .await
        });
        self.invoke_handler(move |webview, arg| {
            let handle = webview.as_mut();
            let command: CallbackCmd<C> = serde_json::from_str(arg).map_err(map_err)?;
            if let Err(e) = tx.try_send(Command(command, handle.clone())) {
                let command = e.into_inner();
                execute_callback(
                    handle,
                    Err(anyhow::anyhow!("Failed to execute command")),
                    command.0.callback,
                    command.0.error,
                );
            }
            Ok(())
        })
    }
}