1
1
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
+ use futures:: Stream ;
4
5
use nym_crypto:: asymmetric:: identity;
5
6
use nym_gateway_client:: { AcknowledgementReceiver , MixnetMessageReceiver } ;
7
+ use std:: pin:: Pin ;
8
+ use std:: task:: { Context , Poll } ;
6
9
use tokio_stream:: StreamMap ;
7
10
8
11
pub ( crate ) type GatewayMessages = Vec < Vec < u8 > > ;
@@ -20,11 +23,7 @@ impl GatewaysReader {
20
23
}
21
24
}
22
25
23
- pub fn stream_map ( & mut self ) -> & mut StreamMap < String , MixnetMessageReceiver > {
24
- & mut self . stream_map
25
- }
26
-
27
- pub fn add_recievers (
26
+ pub fn add_receivers (
28
27
& mut self ,
29
28
id : identity:: PublicKey ,
30
29
message_receiver : MixnetMessageReceiver ,
@@ -35,8 +34,27 @@ impl GatewaysReader {
35
34
self . ack_map . insert ( channel_id, ack_receiver) ;
36
35
}
37
36
38
- pub fn remove_recievers ( & mut self , id : & str ) {
37
+ pub fn remove_receivers ( & mut self , id : & str ) {
39
38
self . stream_map . remove ( id) ;
40
39
self . ack_map . remove ( id) ;
41
40
}
42
41
}
42
+
43
+ impl Stream for GatewaysReader {
44
+ // just return whatever is returned by our main `stream_map`
45
+ type Item = <StreamMap < String , MixnetMessageReceiver > as Stream >:: Item ;
46
+
47
+ fn poll_next ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Option < Self :: Item > > {
48
+ // exhaust the ack map if possible
49
+ match Pin :: new ( & mut self . ack_map ) . poll_next ( cx) {
50
+ Poll :: Ready ( None ) => {
51
+ // this should have never happened!
52
+ return Poll :: Ready ( None ) ;
53
+ }
54
+ Poll :: Ready ( Some ( _item) ) => ( ) ,
55
+ Poll :: Pending => ( ) ,
56
+ }
57
+
58
+ Pin :: new ( & mut self . stream_map ) . poll_next ( cx)
59
+ }
60
+ }
0 commit comments