|
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 | +}; |
2 | 5 | use sodiumoxide::crypto;
|
3 | 6 | use std::convert::TryInto;
|
4 | 7 | 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}}; |
10 | 9 | use desert::ToBytes;
|
11 | 10 | pub type Keypair = ([u8;32],[u8;64]);
|
12 | 11 | 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 |
| -} |
86 | 12 |
|
87 | 13 | #[async_trait::async_trait]
|
88 | 14 | pub trait Store: Clone+Send+Sync+Unpin+'static {
|
|
0 commit comments