use crate::{BoxStream, MaybeSend};
use futures::stream::{Stream, StreamExt};
use std::collections::VecDeque;
use timvw_rust_runtime::sse;
pub enum Frame {
Event(sse::Event),
Done,
}
pub fn frames<S, B>(bytes: S) -> BoxStream<'static, Result<Frame, timvw_rust_runtime::Error>>
where
S: Stream<Item = Result<B, timvw_rust_runtime::Error>> + MaybeSend + 'static,
B: AsRef<[u8]> + MaybeSend + 'static,
{
struct State<S> {
bytes: Option<std::pin::Pin<Box<S>>>,
decoder: sse::Decoder,
ready: VecDeque<sse::Event>,
}
let state = State {
bytes: Some(Box::pin(bytes)),
decoder: sse::Decoder::new(),
ready: VecDeque::new(),
};
crate::box_stream(futures::stream::unfold(state, |mut state| async move {
loop {
if let Some(event) = state.ready.pop_front() {
if event.data == sse::DONE {
state.ready.clear();
state.bytes = None;
return Some((Ok(Frame::Done), state));
}
return Some((Ok(Frame::Event(event)), state));
}
let bytes = state.bytes.as_mut()?;
match bytes.next().await {
Some(Ok(chunk)) => state.decoder.feed(chunk.as_ref(), &mut state.ready),
Some(Err(error)) => {
state.bytes = None;
state.decoder.finish();
return Some((Err(error), state));
}
None => {
state.bytes = None;
state.decoder.finish();
}
}
}
}))
}
pub fn from_events(
events: sse::Events,
) -> BoxStream<'static, Result<Frame, timvw_rust_runtime::Error>> {
crate::box_stream(events.map(|event| event.map(Frame::Event)))
}