Crate event_queue

source ·
Expand description

This is a crate for broadcasting events to multiple clients and waiting for each client to receive it before sending another event to that client. If an event was failed to send, or if the number of events in the queue exceeds the limit, then the client is removed from the event queue.

Example

#[derive(Clone)]
struct FooEvent {
    state: String,
    progress: u8,
}

impl Event for FooEvent {
    fn can_merge(&self, other: &FooEvent) -> bool {
        self.state == other.state
    }
}

struct FooNotifier {
    proxy: FooProxy,
}

impl Notify for FooNotifier {
    type Event = FooEvent;
    type NotifyFuture = BoxFuture<'static, Result<(), ClosedClient>>;

    fn notify(&self, event: FooEvent) -> Self::NotifyFuture {
        self.proxy.on_event(&event).map(|result| result.map_err(|_| ClosedClient)).boxed()
    }
}

async fn foo(proxy: FooProxy) {
    let (event_queue, mut handle) = EventQueue::<FooNotifier>::new();
    let fut = async move {
        handle.add_client(FooNotifier { proxy }).await.unwrap();
        handle.queue_event(FooEvent { state: "new state".to_string(), progress: 0 }).await.unwrap();
    };
    future::join(fut, event_queue).await;
}

Structs

The client is closed and should be removed from the event queue.
A control handle that can control the event queue.
An event queue for broadcasting events to multiple clients. Clients that failed to receive events or do not receive events fast enough will be dropped.
The event queue future was dropped before calling control handle functions.
The flush operation timed out.

Traits

The event type need to implement this trait to tell the event queue whether two consecutive pending events can be merged into a single event, if can_merge returns true, the event queue will replace the last event in the queue with the latest event.
This trait defines how an event should be notified for a client. The struct that implements this trait can hold client specific data.