11use std:: sync:: Arc ;
22
3- use parking_lot :: Mutex ;
3+ use tokio :: sync :: { Mutex , RwLock } ;
44use uuid:: Uuid ;
55
66use crate :: wallet_browser:: {
@@ -14,7 +14,7 @@ use crate::wallet_browser::{
1414#[ derive( Debug , Clone ) ]
1515pub ( crate ) struct BrowserWalletState {
1616 /// Current information about the wallet connection.
17- connection : Arc < Mutex < Option < Connection > > > ,
17+ connection : Arc < RwLock < Option < Connection > > > ,
1818 /// Request/response queue for transactions.
1919 transactions : Arc < Mutex < RequestQueue < BrowserTransactionRequest , BrowserTransactionResponse > > > ,
2020 /// Request/response queue for signings.
@@ -33,7 +33,7 @@ impl BrowserWalletState {
3333 /// Create a new browser wallet state.
3434 pub fn new ( session_token : String , development : bool ) -> Self {
3535 Self {
36- connection : Arc :: new ( Mutex :: new ( None ) ) ,
36+ connection : Arc :: new ( RwLock :: new ( None ) ) ,
3737 transactions : Arc :: new ( Mutex :: new ( RequestQueue :: new ( ) ) ) ,
3838 signings : Arc :: new ( Mutex :: new ( RequestQueue :: new ( ) ) ) ,
3939 session_token,
@@ -55,83 +55,83 @@ impl BrowserWalletState {
5555 }
5656
5757 /// Check if wallet is connected.
58- pub fn is_connected ( & self ) -> bool {
59- self . connection . lock ( ) . is_some ( )
58+ pub async fn is_connected ( & self ) -> bool {
59+ self . connection . read ( ) . await . is_some ( )
6060 }
6161
6262 /// Get current connection information.
63- pub fn get_connection ( & self ) -> Option < Connection > {
64- * self . connection . lock ( )
63+ pub async fn get_connection ( & self ) -> Option < Connection > {
64+ * self . connection . read ( ) . await
6565 }
6666
6767 /// Set connection information.
68- pub fn set_connection ( & self , connection : Option < Connection > ) {
69- * self . connection . lock ( ) = connection;
68+ pub async fn set_connection ( & self , connection : Option < Connection > ) {
69+ * self . connection . write ( ) . await = connection;
7070 }
7171
7272 /// Add a transaction request.
73- pub fn add_transaction_request ( & self , request : BrowserTransactionRequest ) {
74- self . transactions . lock ( ) . add_request ( request) ;
73+ pub async fn add_transaction_request ( & self , request : BrowserTransactionRequest ) {
74+ self . transactions . lock ( ) . await . add_request ( request) ;
7575 }
7676
7777 /// Check if a transaction request exists.
78- pub fn has_transaction_request ( & self , id : & Uuid ) -> bool {
79- self . transactions . lock ( ) . has_request ( id)
78+ pub async fn has_transaction_request ( & self , id : & Uuid ) -> bool {
79+ self . transactions . lock ( ) . await . has_request ( id)
8080 }
8181
8282 /// Read the next transaction request.
83- pub fn read_next_transaction_request ( & self ) -> Option < BrowserTransactionRequest > {
84- self . transactions . lock ( ) . read_request ( ) . cloned ( )
83+ pub async fn read_next_transaction_request ( & self ) -> Option < BrowserTransactionRequest > {
84+ self . transactions . lock ( ) . await . read_request ( ) . cloned ( )
8585 }
8686
8787 // Remove a transaction request.
88- pub fn remove_transaction_request ( & self , id : & Uuid ) {
89- self . transactions . lock ( ) . remove_request ( id) ;
88+ pub async fn remove_transaction_request ( & self , id : & Uuid ) {
89+ self . transactions . lock ( ) . await . remove_request ( id) ;
9090 }
9191
9292 /// Add transaction response.
93- pub fn add_transaction_response ( & self , response : BrowserTransactionResponse ) {
93+ pub async fn add_transaction_response ( & self , response : BrowserTransactionResponse ) {
9494 let id = response. id ;
95- let mut transactions = self . transactions . lock ( ) ;
95+ let mut transactions = self . transactions . lock ( ) . await ;
9696 transactions. add_response ( id, response) ;
9797 transactions. remove_request ( & id) ;
9898 }
9999
100100 /// Get transaction response, removing it from the queue.
101- pub fn get_transaction_response ( & self , id : & Uuid ) -> Option < BrowserTransactionResponse > {
102- self . transactions . lock ( ) . get_response ( id)
101+ pub async fn get_transaction_response ( & self , id : & Uuid ) -> Option < BrowserTransactionResponse > {
102+ self . transactions . lock ( ) . await . get_response ( id)
103103 }
104104
105105 /// Add a signing request.
106- pub fn add_signing_request ( & self , request : BrowserSignRequest ) {
107- self . signings . lock ( ) . add_request ( request) ;
106+ pub async fn add_signing_request ( & self , request : BrowserSignRequest ) {
107+ self . signings . lock ( ) . await . add_request ( request) ;
108108 }
109109
110110 /// Check if a signing request exists.
111- pub fn has_signing_request ( & self , id : & Uuid ) -> bool {
112- self . signings . lock ( ) . has_request ( id)
111+ pub async fn has_signing_request ( & self , id : & Uuid ) -> bool {
112+ self . signings . lock ( ) . await . has_request ( id)
113113 }
114114
115115 /// Read the next signing request.
116- pub fn read_next_signing_request ( & self ) -> Option < BrowserSignRequest > {
117- self . signings . lock ( ) . read_request ( ) . cloned ( )
116+ pub async fn read_next_signing_request ( & self ) -> Option < BrowserSignRequest > {
117+ self . signings . lock ( ) . await . read_request ( ) . cloned ( )
118118 }
119119
120120 /// Remove a signing request.
121- pub fn remove_signing_request ( & self , id : & Uuid ) {
122- self . signings . lock ( ) . remove_request ( id) ;
121+ pub async fn remove_signing_request ( & self , id : & Uuid ) {
122+ self . signings . lock ( ) . await . remove_request ( id) ;
123123 }
124124
125125 /// Add signing response.
126- pub fn add_signing_response ( & self , response : BrowserSignResponse ) {
126+ pub async fn add_signing_response ( & self , response : BrowserSignResponse ) {
127127 let id = response. id ;
128- let mut signings = self . signings . lock ( ) ;
128+ let mut signings = self . signings . lock ( ) . await ;
129129 signings. add_response ( id, response) ;
130130 signings. remove_request ( & id) ;
131131 }
132132
133133 /// Get signing response, removing it from the queue.
134- pub fn get_signing_response ( & self , id : & Uuid ) -> Option < BrowserSignResponse > {
135- self . signings . lock ( ) . get_response ( id)
134+ pub async fn get_signing_response ( & self , id : & Uuid ) -> Option < BrowserSignResponse > {
135+ self . signings . lock ( ) . await . get_response ( id)
136136 }
137137}
0 commit comments