11//! Core client type and message wrapper.
22use crate :: events:: { ConnectionState , Error , Event , Result } ;
3+ #[ cfg( feature = "scripts" ) ]
4+ use crate :: extensions:: scripts:: ScriptManager ;
35use crate :: types:: ClientId ;
46use std:: cell:: { Cell , RefCell } ;
57use std:: sync:: atomic:: { AtomicU64 , Ordering } ;
@@ -43,6 +45,8 @@ pub struct Client {
4345 state : Cell < ConnectionState > ,
4446 hooks : RefCell < ClientHooks > ,
4547 bus : RefCell < bus:: EventBus > ,
48+ #[ cfg( feature = "scripts" ) ]
49+ scripts : RefCell < Option < ScriptManager > > ,
4650 auto_reconnect : RefCell < AutoReconnectState > ,
4751 cache : RefCell < cache:: CacheState > ,
4852}
@@ -63,6 +67,8 @@ impl Client {
6367 state : Cell :: new ( ConnectionState :: Idle ) ,
6468 hooks : RefCell :: new ( ClientHooks :: default ( ) ) ,
6569 bus : RefCell :: new ( bus:: EventBus :: default ( ) ) ,
70+ #[ cfg( feature = "scripts" ) ]
71+ scripts : RefCell :: new ( None ) ,
6672 auto_reconnect : RefCell :: new ( AutoReconnectState :: default ( ) ) ,
6773 cache : RefCell :: new ( cache:: CacheState :: default ( ) ) ,
6874 } )
@@ -89,6 +95,8 @@ impl Client {
8995 state : Cell :: new ( ConnectionState :: Idle ) ,
9096 hooks : RefCell :: new ( ClientHooks :: default ( ) ) ,
9197 bus : RefCell :: new ( bus:: EventBus :: default ( ) ) ,
98+ #[ cfg( feature = "scripts" ) ]
99+ scripts : RefCell :: new ( None ) ,
92100 auto_reconnect : RefCell :: new ( AutoReconnectState :: default ( ) ) ,
93101 cache : RefCell :: new ( cache:: CacheState :: default ( ) ) ,
94102 } )
@@ -178,6 +186,32 @@ impl Client {
178186 * self . hooks . borrow_mut ( ) = ClientHooks :: default ( ) ;
179187 }
180188
189+ #[ cfg( feature = "scripts" ) ]
190+ pub fn enable_scripts ( & self ) {
191+ let mut scripts = self . scripts . borrow_mut ( ) ;
192+ if scripts. is_none ( ) {
193+ * scripts = Some ( ScriptManager :: new ( ) ) ;
194+ }
195+ }
196+
197+ #[ cfg( feature = "scripts" ) ]
198+ pub fn set_script_manager ( & self , manager : ScriptManager ) {
199+ * self . scripts . borrow_mut ( ) = Some ( manager) ;
200+ }
201+
202+ #[ cfg( feature = "scripts" ) ]
203+ pub fn clear_scripts ( & self ) {
204+ * self . scripts . borrow_mut ( ) = None ;
205+ }
206+
207+ #[ cfg( feature = "scripts" ) ]
208+ pub fn scripts_mut < F , R > ( & self , f : F ) -> Option < R >
209+ where
210+ F : FnOnce ( & mut ScriptManager ) -> R ,
211+ {
212+ self . scripts . borrow_mut ( ) . as_mut ( ) . map ( f)
213+ }
214+
181215 pub ( crate ) fn set_connection_state ( & self , state : ConnectionState ) {
182216 self . state . set ( state) ;
183217 }
@@ -190,6 +224,13 @@ impl Client {
190224 self . bus . borrow_mut ( ) . dispatch ( self , event, msg) ;
191225 }
192226
227+ #[ cfg( feature = "scripts" ) ]
228+ pub ( crate ) fn dispatch_scripts ( & self , event : crate :: events:: Event , msg : & Message ) {
229+ if let Some ( manager) = self . scripts . borrow ( ) . as_ref ( ) {
230+ let _ = manager. handle_event ( event, msg) ;
231+ }
232+ }
233+
193234 pub ( crate ) fn invoke_joined_hook ( & self , channel_id : crate :: types:: ChannelId ) {
194235 self . hooks . borrow_mut ( ) . fire_joined ( self , channel_id) ;
195236 }
0 commit comments