use std::collections::VecDeque;
use idiolect_records::Nsid;
use crate::error::IndexerError;
use crate::event::IndexerAction;
#[derive(Debug, Clone)]
pub struct RawEvent {
pub seq: u64,
pub live: bool,
pub did: String,
pub rev: String,
pub collection: Nsid,
pub rkey: String,
pub action: IndexerAction,
pub cid: Option<String>,
pub body: Option<serde_json::Value>,
}
#[allow(async_fn_in_trait)]
pub trait EventStream: Send + Sync {
async fn next_event(&mut self) -> Result<Option<RawEvent>, IndexerError>;
}
#[derive(Debug, Default)]
pub struct InMemoryEventStream {
queue: VecDeque<RawEvent>,
}
impl InMemoryEventStream {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, event: RawEvent) {
self.queue.push_back(event);
}
pub fn extend<I>(&mut self, events: I)
where
I: IntoIterator<Item = RawEvent>,
{
self.queue.extend(events);
}
#[must_use]
pub fn len(&self) -> usize {
self.queue.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.queue.is_empty()
}
}
impl EventStream for InMemoryEventStream {
async fn next_event(&mut self) -> Result<Option<RawEvent>, IndexerError> {
Ok(self.queue.pop_front())
}
}