@@ -15,7 +15,7 @@ use crate::rpc::{Error, Request, Result, RpcParams, Subscribe};
15
15
use jsonrpsee:: {
16
16
client_transport:: ws:: { Url , WsTransportClientBuilder } ,
17
17
core:: {
18
- client:: { Client , ClientBuilder , ClientT , SubscriptionClientT } ,
18
+ client:: { Client , ClientBuilder , ClientT , Error as JsonrpseeError , SubscriptionClientT } ,
19
19
traits:: ToRpcParams ,
20
20
} ,
21
21
} ;
@@ -33,6 +33,7 @@ pub struct JsonrpseeClient {
33
33
}
34
34
35
35
impl JsonrpseeClient {
36
+ /// Create a new client to a local Substrate node with default port.
36
37
pub async fn with_default_url ( ) -> Result < Self > {
37
38
Self :: new ( "ws://127.0.0.1:9944" ) . await
38
39
}
@@ -59,6 +60,42 @@ impl JsonrpseeClient {
59
60
let url = format ! ( "{address}:{port:?}" ) ;
60
61
Self :: new ( & url) . await
61
62
}
63
+
64
+ /// Create a new client with a user-generated Jsonrpsee Client.
65
+ pub fn new_with_client ( client : Client ) -> Self {
66
+ let inner = Arc :: new ( client) ;
67
+ Self { inner }
68
+ }
69
+ }
70
+
71
+ impl JsonrpseeClient {
72
+ /// Checks if the client is connected to the target.
73
+ pub fn is_connected ( & self ) -> bool {
74
+ self . inner . is_connected ( )
75
+ }
76
+
77
+ /// This is similar to [`Client::on_disconnect`] but it can be used to get
78
+ /// the reason why the client was disconnected but it's not cancel-safe.
79
+ ///
80
+ /// The typical use-case is that this method will be called after
81
+ /// [`Client::on_disconnect`] has returned in a "select loop".
82
+ ///
83
+ /// # Cancel-safety
84
+ ///
85
+ /// This method is not cancel-safe
86
+ pub async fn disconnect_reason ( & self ) -> JsonrpseeError {
87
+ self . inner . disconnect_reason ( ) . await
88
+ }
89
+
90
+ /// Completes when the client is disconnected or the client's background task encountered an error.
91
+ /// If the client is already disconnected, the future produced by this method will complete immediately.
92
+ ///
93
+ /// # Cancel safety
94
+ ///
95
+ /// This method is cancel safe.
96
+ pub async fn on_disconnect ( & self ) {
97
+ self . inner . on_disconnect ( ) . await ;
98
+ }
62
99
}
63
100
64
101
#[ maybe_async:: async_impl( ?Send ) ]
0 commit comments