1
1
use crate :: {
2
2
connection:: { BaseConnection , Buffer , UnixSocketConnection } ,
3
- models:: BaseMessage ,
3
+ models:: { BaseMessage , Value } ,
4
4
protocol:: BaseProtocol ,
5
5
utils:: { self , Error , Result } ,
6
6
} ;
7
+ use std:: collections:: HashMap ;
7
8
use async_std:: {
8
9
prelude:: * ,
9
10
sync:: { Arc , Mutex } ,
@@ -14,11 +15,9 @@ use futures::channel::{
14
15
oneshot:: { channel, Sender } ,
15
16
} ;
16
17
use futures_util:: sink:: SinkExt ;
17
- use serde_json:: { Map , Value } ;
18
- use std:: collections:: HashMap ;
19
18
20
19
type ArcRequestList = Arc < Mutex < HashMap < String , Sender < Result < Value > > > > > ;
21
- type ArcFunctionList = Arc < Mutex < HashMap < String , fn ( Map < String , Value > ) -> Value > > > ;
20
+ type ArcFunctionList = Arc < Mutex < HashMap < String , fn ( HashMap < String , Value > ) -> Value > > > ;
22
21
type ArcHookListenerList = Arc < Mutex < HashMap < String , Vec < fn ( Value ) > > > > ;
23
22
24
23
pub struct GothamModule {
@@ -74,7 +73,7 @@ impl GothamModule {
74
73
pub async fn declare_function (
75
74
& mut self ,
76
75
fn_name : String ,
77
- function : fn ( Map < String , Value > ) -> Value ,
76
+ function : fn ( HashMap < String , Value > ) -> Value ,
78
77
) -> Result < ( ) > {
79
78
self . functions
80
79
. lock ( )
@@ -89,7 +88,7 @@ impl GothamModule {
89
88
pub async fn call_function (
90
89
& mut self ,
91
90
fn_name : String ,
92
- args : Map < String , Value > ,
91
+ args : HashMap < String , Value > ,
93
92
) -> Result < Value > {
94
93
self . ensure_registered ( ) ?;
95
94
let request = self . protocol . call_function ( fn_name, args) ;
@@ -172,19 +171,18 @@ impl GothamModule {
172
171
}
173
172
}
174
173
175
- let mut encoded = self . protocol . encode ( & request) ;
176
- if self . registered || request. get_type ( ) == 1 {
174
+ let request_type = request. get_type ( ) ;
175
+ let request_id = request. get_request_id ( ) . clone ( ) ;
176
+ let mut encoded = self . protocol . encode ( request) ;
177
+ if self . registered || request_type == 1 {
177
178
self . connection . send ( encoded) . await ;
178
179
} else {
179
180
self . message_buffer . append ( & mut encoded) ;
180
181
}
181
182
182
183
let ( sender, receiver) = channel :: < Result < Value > > ( ) ;
183
184
184
- self . requests
185
- . lock ( )
186
- . await
187
- . insert ( request. get_request_id ( ) . clone ( ) , sender) ;
185
+ self . requests . lock ( ) . await . insert ( request_id, sender) ;
188
186
189
187
match receiver. await {
190
188
Ok ( value) => value,
@@ -213,11 +211,11 @@ async fn on_data_listener(
213
211
BaseMessage :: FunctionCallRequest { .. } => {
214
212
let result = execute_function_call ( message, & functions) . await ;
215
213
let write_buffer = match result {
216
- Ok ( value) => protocol. encode ( & BaseMessage :: FunctionCallResponse {
214
+ Ok ( value) => protocol. encode ( BaseMessage :: FunctionCallResponse {
217
215
request_id : request_id. clone ( ) ,
218
216
data : value,
219
217
} ) ,
220
- Err ( error) => protocol. encode ( & BaseMessage :: Error {
218
+ Err ( error) => protocol. encode ( BaseMessage :: Error {
221
219
request_id : request_id. clone ( ) ,
222
220
error : match error {
223
221
Error :: Internal ( _) => 0 ,
0 commit comments