1+ use ractor:: ActorCell ;
2+ use ractor_supervisor:: dynamic:: DynamicSupervisorMsg ;
13use tauri:: Manager ;
24use tokio:: sync:: Mutex ;
35
@@ -7,17 +9,26 @@ mod error;
79mod events;
810mod ext;
911pub mod fsm;
12+ mod supervisor;
1013
1114pub use error:: * ;
1215pub use events:: * ;
1316pub use ext:: * ;
17+ pub use supervisor:: { SupervisorHandle , SupervisorRef , SUPERVISOR_NAME } ;
1418
1519const PLUGIN_NAME : & str = "listener" ;
1620
17- pub type SharedState = Mutex < State > ;
21+ pub type SharedState = std :: sync :: Arc < Mutex < State > > ;
1822
1923pub struct State {
20- app : tauri:: AppHandle ,
24+ pub app : tauri:: AppHandle ,
25+ pub listener_supervisor : Option < ractor:: ActorRef < DynamicSupervisorMsg > > ,
26+ pub supervisor_handle : Option < SupervisorHandle > ,
27+ }
28+
29+ #[ derive( Default ) ]
30+ pub struct InitOptions {
31+ pub parent_supervisor : Option < ActorCell > ,
2132}
2233
2334impl State {
@@ -48,7 +59,7 @@ fn make_specta_builder<R: tauri::Runtime>() -> tauri_specta::Builder<R> {
4859 . error_handling ( tauri_specta:: ErrorHandlingMode :: Result )
4960}
5061
51- pub fn init ( ) -> tauri:: plugin:: TauriPlugin < tauri:: Wry > {
62+ pub fn init ( options : InitOptions ) -> tauri:: plugin:: TauriPlugin < tauri:: Wry > {
5263 let specta_builder = make_specta_builder ( ) ;
5364
5465 tauri:: plugin:: Builder :: new ( PLUGIN_NAME )
@@ -58,9 +69,29 @@ pub fn init() -> tauri::plugin::TauriPlugin<tauri::Wry> {
5869
5970 let app_handle = app. app_handle ( ) . clone ( ) ;
6071
61- let state: SharedState = Mutex :: new ( State { app : app_handle } ) ;
72+ let state: SharedState = std:: sync:: Arc :: new ( Mutex :: new ( State {
73+ app : app_handle,
74+ listener_supervisor : None ,
75+ supervisor_handle : None ,
76+ } ) ) ;
77+
78+ app. manage ( state. clone ( ) ) ;
79+
80+ let parent = options. parent_supervisor . clone ( ) ;
81+ tauri:: async_runtime:: spawn ( async move {
82+ match supervisor:: spawn_listener_supervisor ( parent) . await {
83+ Ok ( ( supervisor, handle) ) => {
84+ let mut guard = state. lock ( ) . await ;
85+ guard. listener_supervisor = Some ( supervisor) ;
86+ guard. supervisor_handle = Some ( handle) ;
87+ tracing:: info!( "listener_supervisor_spawned" ) ;
88+ }
89+ Err ( e) => {
90+ tracing:: error!( "failed_to_spawn_listener_supervisor: {:?}" , e) ;
91+ }
92+ }
93+ } ) ;
6294
63- app. manage ( state) ;
6495 Ok ( ( ) )
6596 } )
6697 . build ( )
0 commit comments