tauri_react/lib.rs
1/*
2 * File: lib.rs
3 * Author: MarkAtk
4 * Date: 09.12.20
5 *
6 * MIT License
7 *
8 * Copyright (c) 2020 MarkAtk
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in all
18 * copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29use std::collections::HashMap;
30use std::sync::Arc;
31use once_cell::sync::Lazy;
32use tauri::Webview;
33
34mod command;
35
36use command::{Cmd, CommandError};
37
38pub use command::{StoreState, ApplicationState, ActionCallback};
39
40pub const INIT_FUNC: &str = "initialize";
41
42pub fn command_handler<T: ApplicationState + 'static>(
43 webview: &mut Webview,
44 arg: &str,
45 state: &'static Lazy<Arc<dyn StoreState<T>>>,
46 commands: &'static Lazy<HashMap<String, Box<dyn Fn(T, serde_json::Value) -> tauri::Result<T> + Send + Sync + 'static>>>,
47) -> Result<(), String> {
48 match serde_json::from_str(arg) {
49 Err(err) => {
50 println!("{}", err);
51
52 Err(err.to_string())
53 },
54 Ok(command) => {
55 match command {
56 Cmd::InitializeStore { callback, error } => tauri::execute_promise(webview, move || {
57 println!("locking init state");
58 let mut state_data = state.get_data()?;
59 println!("init state lock");
60
61 match commands.get(INIT_FUNC) {
62 Some(ref func) => {
63 match func((*state_data).clone(), serde_json::Value::Null) {
64 Ok(s) => {
65 *state_data = s;
66 println!("Init state: {}", *state_data);
67
68 Ok((*state_data).clone())
69 },
70 Err(err) => Err(err)
71 }
72 },
73 _ => Ok((*state_data).clone())
74 }
75 }, callback, error),
76 Cmd::Action { action, data, callback, error } => tauri::execute_promise(webview, move || {
77 // try to get function for action and execute it, otherwise return command error
78 match commands.get(&action) {
79 Some(ref func) => {
80 let mut state_data = state.get_data()?;
81
82 match func((*state_data).clone(), data) {
83 Ok(s) => {
84 *state_data = s;
85 println!("New state: {}", *state_data);
86
87 Ok((*state_data).clone())
88 },
89 Err(err) => Err(err)
90 }
91 },
92 _ => {
93 println!("Unknown action: {}", action);
94
95 Err(CommandError::new(format!("Unknown action: {}", action)).into())
96 }
97 }
98 }, callback, error)
99 }
100
101 Ok(())
102 }
103 }
104}
105
106pub fn update_state<T: ApplicationState>(webview: &mut tauri::WebviewMut, state: T) -> tauri::Result<()> {
107 tauri::event::emit(webview, String::from("state-update"), Some(state))
108}