@@ -89,38 +89,62 @@ impl Response for GatewayRespV1 {
89
89
90
90
#[ derive( Debug ) ]
91
91
pub struct StateChannelFollowService {
92
- tx : mpsc:: Sender < GatewayScFollowReqV1 > ,
93
- rx : Streaming ,
92
+ tx : Option < mpsc:: Sender < GatewayScFollowReqV1 > > ,
93
+ rx : Option < Streaming > ,
94
94
}
95
95
96
96
impl StateChannelFollowService {
97
- pub async fn new ( mut client : GatewayClient , verifier : Arc < PublicKey > ) -> Result < Self > {
98
- let ( tx, client_rx) = mpsc:: channel ( 3 ) ;
99
- let streaming = client
100
- . follow_sc ( ReceiverStream :: new ( client_rx) )
101
- . await ?
102
- . into_inner ( ) ;
103
- let rx = Streaming {
104
- streaming,
105
- verifier,
106
- } ;
107
- Ok ( Self { tx, rx } )
97
+ pub async fn new ( gateway : & mut GatewayService ) -> Result < Self > {
98
+ let mut result = Self { tx : None , rx : None } ;
99
+ result. set_gateway ( Some ( gateway) ) . await ?;
100
+ Ok ( result)
108
101
}
109
102
110
103
pub async fn send ( & mut self , id : & [ u8 ] , owner : & [ u8 ] ) -> Result {
111
- let msg = GatewayScFollowReqV1 {
112
- sc_id : id. into ( ) ,
113
- sc_owner : owner. into ( ) ,
104
+ match self . tx . as_mut ( ) {
105
+ Some ( tx) => {
106
+ let msg = GatewayScFollowReqV1 {
107
+ sc_id : id. into ( ) ,
108
+ sc_owner : owner. into ( ) ,
109
+ } ;
110
+ Ok ( tx. send ( msg) . await ?)
111
+ }
112
+ None => Err ( Error :: no_service ( ) ) ,
113
+ }
114
+ }
115
+
116
+ pub async fn set_gateway ( & mut self , gateway : Option < & mut GatewayService > ) -> Result {
117
+ let ( tx, rx) = match gateway {
118
+ Some ( gateway) => {
119
+ let ( tx, client_rx) = mpsc:: channel ( 3 ) ;
120
+ let streaming = gateway
121
+ . client
122
+ . follow_sc ( ReceiverStream :: new ( client_rx) )
123
+ . await ?
124
+ . into_inner ( ) ;
125
+ let rx = Streaming {
126
+ streaming,
127
+ verifier : gateway. uri . pubkey . clone ( ) ,
128
+ } ;
129
+ ( Some ( tx) , Some ( rx) )
130
+ }
131
+ None => ( None , None ) ,
114
132
} ;
115
- Ok ( self . tx . send ( msg) . await ?)
133
+ self . tx = tx;
134
+ self . rx = rx;
135
+ Ok ( ( ) )
116
136
}
117
137
}
118
138
119
139
impl Stream for StateChannelFollowService {
120
140
type Item = Result < GatewayRespV1 > ;
121
141
122
142
fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
123
- Pin :: new ( & mut self . rx ) . poll_next ( cx)
143
+ if let Some ( rx) = self . rx . as_mut ( ) {
144
+ Pin :: new ( rx) . poll_next ( cx)
145
+ } else {
146
+ Poll :: Pending
147
+ }
124
148
}
125
149
}
126
150
@@ -222,7 +246,7 @@ impl GatewayService {
222
246
}
223
247
224
248
pub async fn follow_sc ( & mut self ) -> Result < StateChannelFollowService > {
225
- StateChannelFollowService :: new ( self . client . clone ( ) , self . uri . pubkey . clone ( ) ) . await
249
+ StateChannelFollowService :: new ( self ) . await
226
250
}
227
251
228
252
pub async fn close_sc ( & mut self , close_txn : BlockchainTxnStateChannelCloseV1 ) -> Result {
0 commit comments