@@ -5,7 +5,7 @@ use std::collections::{HashMap,BTreeMap};
5
5
use async_std:: {
6
6
prelude:: * ,
7
7
stream:: Stream , stream, channel, sync:: { Arc , RwLock , Mutex } ,
8
- task, task:: { Context , Poll } , pin:: Pin ,
8
+ task, task:: { Waker , Context , Poll } , pin:: Pin ,
9
9
} ;
10
10
use desert:: ToBytes ;
11
11
pub type Keypair = ( [ u8 ; 32 ] , [ u8 ; 64 ] ) ;
@@ -20,6 +20,7 @@ struct LiveStream {
20
20
sender : channel:: Sender < Post > ,
21
21
receiver : channel:: Receiver < Post > ,
22
22
live_streams : Arc < RwLock < Vec < Self > > > ,
23
+ waker : Arc < Mutex < Option < Waker > > > ,
23
24
}
24
25
25
26
impl LiveStream {
@@ -29,13 +30,14 @@ impl LiveStream {
29
30
live_streams : Arc < RwLock < Vec < Self > > > ,
30
31
) -> Self {
31
32
let ( sender, receiver) = channel:: bounded ( options. limit ) ;
32
- Self { id, options, sender, receiver, live_streams }
33
+ Self { id, options, sender, receiver, live_streams, waker : Arc :: new ( Mutex :: new ( None ) ) }
33
34
}
34
- pub fn send ( & self , post : Post ) {
35
+ pub async fn send ( & mut self , post : Post ) {
35
36
if let Err ( _) = self . sender . try_send ( post) { }
37
+ if let Some ( waker) = self . waker . lock ( ) . await . as_ref ( ) {
38
+ waker. wake_by_ref ( ) ;
39
+ }
36
40
}
37
- }
38
- impl LiveStream {
39
41
pub fn matches ( & self , post : & Post ) -> bool {
40
42
if Some ( & self . options . channel ) != post. get_channel ( ) { return false }
41
43
match ( self . options . time_start , self . options . time_end ) {
@@ -50,10 +52,28 @@ impl LiveStream {
50
52
impl Stream for LiveStream {
51
53
type Item = Result < Post , Error > ;
52
54
fn poll_next ( self : Pin < & mut Self > , ctx : & mut Context ) -> Poll < Option < Self :: Item > > {
53
- let r = futures:: ready![ Pin :: new( & mut self . receiver. recv( ) ) . poll( ctx) ] . unwrap ( ) ;
54
- Poll :: Ready ( Some ( Ok ( r) ) )
55
+ let r = Pin :: new ( & mut self . receiver . recv ( ) ) . poll ( ctx) ;
56
+ match r {
57
+ Poll :: Ready ( Ok ( x) ) => {
58
+ let m_waker = self . waker . clone ( ) ;
59
+ task:: block_on ( async move { * m_waker. lock ( ) . await = None ; } ) ;
60
+ Poll :: Ready ( Some ( Ok ( x) ) )
61
+ } ,
62
+ Poll :: Ready ( Err ( x) ) => {
63
+ let m_waker = self . waker . clone ( ) ;
64
+ task:: block_on ( async move { * m_waker. lock ( ) . await = None ; } ) ;
65
+ Poll :: Ready ( Some ( Err ( x. into ( ) ) ) )
66
+ } ,
67
+ Poll :: Pending => {
68
+ let m_waker = self . waker . clone ( ) ;
69
+ let waker = ctx. waker ( ) . clone ( ) ;
70
+ task:: block_on ( async move { * m_waker. lock ( ) . await = Some ( waker) ; } ) ;
71
+ Poll :: Pending
72
+ } ,
73
+ }
55
74
}
56
75
}
76
+
57
77
impl Drop for LiveStream {
58
78
fn drop ( & mut self ) {
59
79
let live_streams = self . live_streams . clone ( ) ;
@@ -170,9 +190,9 @@ impl Store for MemoryStore {
170
190
}
171
191
}
172
192
if let Some ( senders) = self . live_streams . read ( ) . await . get ( channel) {
173
- for stream in senders. read ( ) . await . iter ( ) {
193
+ for stream in senders. write ( ) . await . iter_mut ( ) {
174
194
if stream. matches ( & post) {
175
- stream. send ( post. clone ( ) ) ;
195
+ stream. send ( post. clone ( ) ) . await ;
176
196
}
177
197
}
178
198
}
0 commit comments