Skip to content

Commit 2291cc3

Browse files
author
substack
committed
move stream code out of store
1 parent d321d32 commit 2291cc3

File tree

4 files changed

+87
-87
lines changed

4 files changed

+87
-87
lines changed

src/cable.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ mod store;
2222
pub use store::*;
2323
mod error;
2424
pub use error::*;
25+
mod stream;
26+
pub use stream::*;
2527
use length_prefixed_stream::{decode_with_options,DecodeOptions};
2628

2729
#[derive(Clone,Debug,PartialEq)]

src/store.rs

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,14 @@
1-
use crate::{Error,Post,PostBody,Channel,Hash,Payload,ChannelOptions};
1+
use crate::{
2+
Error,Post,PostBody,Channel,Hash,Payload,ChannelOptions,
3+
stream::{LiveStream,PostStream,HashStream},
4+
};
25
use sodiumoxide::crypto;
36
use std::convert::TryInto;
47
use std::collections::{HashMap,BTreeMap};
5-
use async_std::{
6-
prelude::*,
7-
stream::Stream,stream,channel,sync::{Arc,RwLock,Mutex},
8-
task,task::{Waker,Context,Poll},pin::Pin,
9-
};
8+
use async_std::{prelude::*,task,stream,sync::{Arc,RwLock,Mutex}};
109
use desert::ToBytes;
1110
pub type Keypair = ([u8;32],[u8;64]);
1211
pub type GetPostOptions = ChannelOptions;
13-
pub type PostStream<'a> = Box<dyn Stream<Item=Result<Post,Error>>+Unpin+Send+'a>;
14-
pub type HashStream<'a> = Box<dyn Stream<Item=Result<Hash,Error>>+Unpin+Send+'a>;
15-
16-
#[derive(Clone)]
17-
struct LiveStream {
18-
id: usize,
19-
options: ChannelOptions,
20-
sender: channel::Sender<Post>,
21-
receiver: channel::Receiver<Post>,
22-
live_streams: Arc<RwLock<Vec<Self>>>,
23-
waker: Arc<Mutex<Option<Waker>>>,
24-
}
25-
26-
impl LiveStream {
27-
pub fn new(
28-
id: usize,
29-
options: ChannelOptions,
30-
live_streams: Arc<RwLock<Vec<Self>>>,
31-
) -> Self {
32-
let (sender,receiver) = channel::bounded(options.limit);
33-
Self { id, options, sender, receiver, live_streams, waker: Arc::new(Mutex::new(None)) }
34-
}
35-
pub async fn send(&mut self, post: Post) {
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-
}
40-
}
41-
pub fn matches(&self, post: &Post) -> bool {
42-
if Some(&self.options.channel) != post.get_channel() { return false }
43-
match (self.options.time_start, self.options.time_end) {
44-
(0,0) => true,
45-
(0,end) => post.get_timestamp().map(|t| t <= end).unwrap_or(false),
46-
(start,0) => post.get_timestamp().map(|t| start <= t).unwrap_or(false),
47-
(start,end) => post.get_timestamp().map(|t| start <= t && t <= end).unwrap_or(false),
48-
}
49-
}
50-
}
51-
52-
impl Stream for LiveStream {
53-
type Item = Result<Post,Error>;
54-
fn poll_next(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> {
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-
}
74-
}
75-
}
76-
77-
impl Drop for LiveStream {
78-
fn drop(&mut self) {
79-
let live_streams = self.live_streams.clone();
80-
let id = self.id;
81-
task::block_on(async move {
82-
live_streams.write().await.drain_filter(|s| s.id == id);
83-
});
84-
}
85-
}
8612

8713
#[async_trait::async_trait]
8814
pub trait Store: Clone+Send+Sync+Unpin+'static {

src/stream.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use crate::{Error,ChannelOptions,Post,Hash};
2+
use async_std::{
3+
prelude::*,
4+
task,channel,stream::Stream,pin::Pin,
5+
task::{Waker,Context,Poll},sync::{Arc,RwLock,Mutex},
6+
};
7+
8+
pub type PostStream<'a> = Box<dyn Stream<Item=Result<Post,Error>>+Unpin+Send+'a>;
9+
pub type HashStream<'a> = Box<dyn Stream<Item=Result<Hash,Error>>+Unpin+Send+'a>;
10+
11+
#[derive(Clone)]
12+
pub struct LiveStream {
13+
id: usize,
14+
options: ChannelOptions,
15+
sender: channel::Sender<Post>,
16+
receiver: channel::Receiver<Post>,
17+
live_streams: Arc<RwLock<Vec<Self>>>,
18+
waker: Arc<Mutex<Option<Waker>>>,
19+
}
20+
21+
impl LiveStream {
22+
pub fn new(
23+
id: usize,
24+
options: ChannelOptions,
25+
live_streams: Arc<RwLock<Vec<Self>>>,
26+
) -> Self {
27+
let (sender,receiver) = channel::bounded(options.limit);
28+
Self { id, options, sender, receiver, live_streams, waker: Arc::new(Mutex::new(None)) }
29+
}
30+
pub async fn send(&mut self, post: Post) {
31+
if let Err(_) = self.sender.try_send(post) {}
32+
if let Some(waker) = self.waker.lock().await.as_ref() {
33+
waker.wake_by_ref();
34+
}
35+
}
36+
pub fn matches(&self, post: &Post) -> bool {
37+
if Some(&self.options.channel) != post.get_channel() { return false }
38+
match (self.options.time_start, self.options.time_end) {
39+
(0,0) => true,
40+
(0,end) => post.get_timestamp().map(|t| t <= end).unwrap_or(false),
41+
(start,0) => post.get_timestamp().map(|t| start <= t).unwrap_or(false),
42+
(start,end) => post.get_timestamp().map(|t| start <= t && t <= end).unwrap_or(false),
43+
}
44+
}
45+
}
46+
47+
impl Stream for LiveStream {
48+
type Item = Result<Post,Error>;
49+
fn poll_next(self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> {
50+
let r = Pin::new(&mut self.receiver.recv()).poll(ctx);
51+
match r {
52+
Poll::Ready(Ok(x)) => {
53+
let m_waker = self.waker.clone();
54+
task::block_on(async move { *m_waker.lock().await = None; });
55+
Poll::Ready(Some(Ok(x)))
56+
},
57+
Poll::Ready(Err(x)) => {
58+
let m_waker = self.waker.clone();
59+
task::block_on(async move { *m_waker.lock().await = None; });
60+
Poll::Ready(Some(Err(x.into())))
61+
},
62+
Poll::Pending => {
63+
let m_waker = self.waker.clone();
64+
let waker = ctx.waker().clone();
65+
task::block_on(async move { *m_waker.lock().await = Some(waker); });
66+
Poll::Pending
67+
},
68+
}
69+
}
70+
}
71+
72+
impl Drop for LiveStream {
73+
fn drop(&mut self) {
74+
let live_streams = self.live_streams.clone();
75+
let id = self.id;
76+
task::block_on(async move {
77+
live_streams.write().await.drain_filter(|s| s.id == id);
78+
});
79+
}
80+
}

0 commit comments

Comments
 (0)