| 
									
										
										
										
											2023-02-02 23:02:49 +08:00
										 |  |  | use flowy_core::AppFlowyCore;
 | 
					
						
							| 
									
										
										
										
											2023-01-17 16:27:17 +08:00
										 |  |  | use lib_dispatch::prelude::{
 | 
					
						
							| 
									
										
										
										
											2023-02-13 09:29:49 +08:00
										 |  |  |   AFPluginDispatcher, AFPluginEventResponse, AFPluginRequest, StatusCode,
 | 
					
						
							| 
									
										
										
										
											2023-01-17 16:27:17 +08:00
										 |  |  | };
 | 
					
						
							|  |  |  | use tauri::{AppHandle, Manager, State, Wry};
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[derive(Clone, Debug, serde::Deserialize)]
 | 
					
						
							|  |  |  | pub struct AFTauriRequest {
 | 
					
						
							| 
									
										
										
										
											2023-02-13 09:29:49 +08:00
										 |  |  |   ty: String,
 | 
					
						
							|  |  |  |   payload: Vec<u8>,
 | 
					
						
							| 
									
										
										
										
											2023-01-17 16:27:17 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl std::convert::From<AFTauriRequest> for AFPluginRequest {
 | 
					
						
							| 
									
										
										
										
											2023-02-13 09:29:49 +08:00
										 |  |  |   fn from(event: AFTauriRequest) -> Self {
 | 
					
						
							|  |  |  |     AFPluginRequest::new(event.ty).payload(event.payload)
 | 
					
						
							|  |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2023-01-17 16:27:17 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #[derive(Clone, serde::Serialize)]
 | 
					
						
							|  |  |  | pub struct AFTauriResponse {
 | 
					
						
							| 
									
										
										
										
											2023-02-13 09:29:49 +08:00
										 |  |  |   code: StatusCode,
 | 
					
						
							|  |  |  |   payload: Vec<u8>,
 | 
					
						
							| 
									
										
										
										
											2023-01-17 16:27:17 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | impl std::convert::From<AFPluginEventResponse> for AFTauriResponse {
 | 
					
						
							| 
									
										
										
										
											2023-02-13 09:29:49 +08:00
										 |  |  |   fn from(response: AFPluginEventResponse) -> Self {
 | 
					
						
							|  |  |  |     Self {
 | 
					
						
							|  |  |  |       code: response.status_code,
 | 
					
						
							|  |  |  |       payload: response.payload.to_vec(),
 | 
					
						
							| 
									
										
										
										
											2023-01-17 16:27:17 +08:00
										 |  |  |     }
 | 
					
						
							| 
									
										
										
										
											2023-02-13 09:29:49 +08:00
										 |  |  |   }
 | 
					
						
							| 
									
										
										
										
											2023-01-17 16:27:17 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
 | 
					
						
							|  |  |  | #[tauri::command]
 | 
					
						
							|  |  |  | pub async fn invoke_request(
 | 
					
						
							| 
									
										
										
										
											2023-02-13 09:29:49 +08:00
										 |  |  |   request: AFTauriRequest,
 | 
					
						
							|  |  |  |   app_handler: AppHandle<Wry>,
 | 
					
						
							| 
									
										
										
										
											2023-01-17 16:27:17 +08:00
										 |  |  | ) -> AFTauriResponse {
 | 
					
						
							| 
									
										
										
										
											2023-02-13 09:29:49 +08:00
										 |  |  |   let request: AFPluginRequest = request.into();
 | 
					
						
							|  |  |  |   let state: State<AppFlowyCore> = app_handler.state();
 | 
					
						
							|  |  |  |   let dispatcher = state.inner().dispatcher();
 | 
					
						
							| 
									
										
										
										
											2024-01-30 05:36:27 +08:00
										 |  |  |   let response = AFPluginDispatcher::async_send(dispatcher.as_ref(), request).await;
 | 
					
						
							| 
									
										
										
										
											2023-02-13 09:29:49 +08:00
										 |  |  |   response.into()
 | 
					
						
							| 
									
										
										
										
											2023-01-17 16:27:17 +08:00
										 |  |  | }
 |