@@ -15,7 +15,7 @@ use crate::rpc::{Error, Request, Result, RpcParams, Subscribe};
1515use jsonrpsee:: {
1616 client_transport:: ws:: { Url , WsTransportClientBuilder } ,
1717 core:: {
18- client:: { Client , ClientBuilder , ClientT , SubscriptionClientT } ,
18+ client:: { Client , ClientBuilder , ClientT , Error as JsonrpseeError , SubscriptionClientT } ,
1919 traits:: ToRpcParams ,
2020 } ,
2121} ;
@@ -33,6 +33,7 @@ pub struct JsonrpseeClient {
3333}
3434
3535impl JsonrpseeClient {
36+ /// Create a new client to a local Substrate node with default port.
3637 pub async fn with_default_url ( ) -> Result < Self > {
3738 Self :: new ( "ws://127.0.0.1:9944" ) . await
3839 }
@@ -59,6 +60,42 @@ impl JsonrpseeClient {
5960 let url = format ! ( "{address}:{port:?}" ) ;
6061 Self :: new ( & url) . await
6162 }
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+ }
6299}
63100
64101#[ maybe_async:: async_impl( ?Send ) ]
0 commit comments