@@ -64,13 +64,13 @@ impl JunoModule {
64
64
pub fn new ( protocol : BaseProtocol , connection : Box < dyn BaseConnection + Send + Sync > ) -> Self {
65
65
JunoModule {
66
66
module_impl : Arc :: new ( JunoModuleImpl {
67
- protocol : Mutex :: new ( protocol) ,
67
+ protocol : RwLock :: new ( protocol) ,
68
68
connection : RwLock :: new ( connection) ,
69
- requests : Mutex :: new ( HashMap :: new ( ) ) ,
70
- functions : Mutex :: new ( HashMap :: new ( ) ) ,
71
- hook_listeners : Mutex :: new ( HashMap :: new ( ) ) ,
69
+ requests : RwLock :: new ( HashMap :: new ( ) ) ,
70
+ functions : RwLock :: new ( HashMap :: new ( ) ) ,
71
+ hook_listeners : RwLock :: new ( HashMap :: new ( ) ) ,
72
72
message_buffer : Mutex :: new ( vec ! [ ] ) ,
73
- registered : Mutex :: new ( false ) ,
73
+ registered : RwLock :: new ( false ) ,
74
74
} ) ,
75
75
}
76
76
}
@@ -83,7 +83,7 @@ impl JunoModule {
83
83
) -> Result < ( ) > {
84
84
self . setup_connections ( ) . await ?;
85
85
86
- let request = self . module_impl . protocol . lock ( ) . await . initialize (
86
+ let request = self . module_impl . protocol . write ( ) . await . initialize (
87
87
String :: from ( module_id) ,
88
88
String :: from ( version) ,
89
89
dependencies,
@@ -100,14 +100,14 @@ impl JunoModule {
100
100
let fn_name = fn_name. to_string ( ) ;
101
101
self . module_impl
102
102
. functions
103
- . lock ( )
103
+ . write ( )
104
104
. await
105
105
. insert ( fn_name. clone ( ) , function) ;
106
106
107
107
let request = self
108
108
. module_impl
109
109
. protocol
110
- . lock ( )
110
+ . read ( )
111
111
. await
112
112
. declare_function ( fn_name) ;
113
113
self . send_request ( request) . await ?;
@@ -123,23 +123,23 @@ impl JunoModule {
123
123
let request = self
124
124
. module_impl
125
125
. protocol
126
- . lock ( )
126
+ . read ( )
127
127
. await
128
128
. call_function ( fn_name, args) ;
129
129
self . send_request ( request) . await
130
130
}
131
131
132
132
pub async fn register_hook ( & mut self , hook : & str , callback : fn ( Value ) ) -> Result < ( ) > {
133
133
let hook = hook. to_string ( ) ;
134
- let mut hook_listeners = self . module_impl . hook_listeners . lock ( ) . await ;
134
+ let mut hook_listeners = self . module_impl . hook_listeners . write ( ) . await ;
135
135
if hook_listeners. contains_key ( & hook) {
136
136
hook_listeners. get_mut ( & hook) . unwrap ( ) . push ( callback) ;
137
137
} else {
138
138
hook_listeners. insert ( hook. clone ( ) , vec ! [ callback] ) ;
139
139
}
140
140
drop ( hook_listeners) ;
141
141
142
- let request = self . module_impl . protocol . lock ( ) . await . register_hook ( hook) ;
142
+ let request = self . module_impl . protocol . read ( ) . await . register_hook ( hook) ;
143
143
self . send_request ( request) . await ?;
144
144
Ok ( ( ) )
145
145
}
@@ -149,7 +149,7 @@ impl JunoModule {
149
149
let request = self
150
150
. module_impl
151
151
. protocol
152
- . lock ( )
152
+ . read ( )
153
153
. await
154
154
. trigger_hook ( hook, data) ;
155
155
self . send_request ( request) . await ?;
@@ -175,7 +175,7 @@ impl JunoModule {
175
175
let mut connection = module. connection . write ( ) . await ;
176
176
if let Some ( data) = connection. read_data ( ) . await {
177
177
drop ( connection) ;
178
- let mut protocol = module. protocol . lock ( ) . await ;
178
+ let mut protocol = module. protocol . write ( ) . await ;
179
179
protocol. append_buffer ( data) ;
180
180
while let Some ( message) = protocol. get_next_message ( ) {
181
181
let request_id = message. get_request_id ( ) . clone ( ) ;
@@ -189,29 +189,28 @@ impl JunoModule {
189
189
let result =
190
190
module. execute_function_call ( function, arguments) . await ;
191
191
let write_buffer = match result {
192
- Ok ( value) => module . protocol . lock ( ) . await . encode (
193
- BaseMessage :: FunctionCallResponse {
192
+ Ok ( value) => {
193
+ protocol . encode ( BaseMessage :: FunctionCallResponse {
194
194
request_id : request_id. clone ( ) ,
195
195
data : value,
196
- } ,
197
- ) ,
198
- Err ( error) => {
199
- module. protocol . lock ( ) . await . encode ( BaseMessage :: Error {
200
- request_id : request_id. clone ( ) ,
201
- error : match error {
202
- Error :: Internal ( _) => 0 ,
203
- Error :: FromJuno ( error_code) => error_code,
204
- } ,
205
196
} )
206
197
}
198
+ Err ( error) => protocol. encode ( BaseMessage :: Error {
199
+ request_id : request_id. clone ( ) ,
200
+ error : match error {
201
+ Error :: Internal ( _) => 0 ,
202
+ Error :: FromJuno ( error_code) => error_code,
203
+ } ,
204
+ } ) ,
207
205
} ;
208
- if let Err ( err) =
209
- module. connection . write ( ) . await . send ( write_buffer) . await
210
- {
211
- Err ( err)
212
- } else {
213
- Ok ( Value :: Null )
214
- }
206
+ module
207
+ . connection
208
+ . write ( )
209
+ . await
210
+ . send ( write_buffer)
211
+ . await
212
+ . unwrap ( ) ;
213
+ Ok ( Value :: Null )
215
214
}
216
215
BaseMessage :: TriggerHookResponse { hook, data, .. } => {
217
216
if let Err ( err) = module. execute_hook_triggered ( hook, data) . await {
@@ -223,13 +222,14 @@ impl JunoModule {
223
222
BaseMessage :: Error { error, .. } => Err ( Error :: FromJuno ( error) ) ,
224
223
_ => Ok ( Value :: Null ) ,
225
224
} ;
226
- let mut requests = module. requests . lock ( ) . await ;
225
+ let mut requests = module. requests . write ( ) . await ;
227
226
if !requests. contains_key ( & request_id) {
228
227
continue ;
229
228
}
230
229
if requests. remove ( & request_id) . unwrap ( ) . send ( value) . is_err ( ) {
231
230
println ! ( "Error sending response of requestId: {}" , & request_id) ;
232
231
}
232
+ drop ( requests) ;
233
233
}
234
234
drop ( protocol) ;
235
235
task:: sleep ( std:: time:: Duration :: from_millis ( 10 ) ) . await ;
@@ -244,15 +244,15 @@ impl JunoModule {
244
244
245
245
async fn send_request ( & mut self , request : BaseMessage ) -> Result < Value > {
246
246
if let BaseMessage :: RegisterModuleRequest { .. } = request {
247
- if * self . module_impl . registered . lock ( ) . await {
247
+ if * self . module_impl . registered . read ( ) . await {
248
248
return Err ( Error :: Internal ( String :: from ( "Module already registered" ) ) ) ;
249
249
}
250
250
}
251
251
252
252
let request_type = request. get_type ( ) ;
253
253
let request_id = request. get_request_id ( ) . clone ( ) ;
254
- let mut encoded = self . module_impl . protocol . lock ( ) . await . encode ( request) ;
255
- if * self . module_impl . registered . lock ( ) . await || request_type == 1 {
254
+ let mut encoded = self . module_impl . protocol . read ( ) . await . encode ( request) ;
255
+ if * self . module_impl . registered . read ( ) . await || request_type == 1 {
256
256
self . module_impl
257
257
. connection
258
258
. write ( )
@@ -271,7 +271,7 @@ impl JunoModule {
271
271
272
272
self . module_impl
273
273
. requests
274
- . lock ( )
274
+ . write ( )
275
275
. await
276
276
. insert ( request_id, sender) ;
277
277
0 commit comments