2020-01-20 04:47:36 +01:00
|
|
|
//! Run commands and keep track of subscriptions.
|
|
|
|
|
use crate::{subscription, Command, Executor, Subscription};
|
2020-01-19 09:06:48 +01:00
|
|
|
|
2020-02-04 03:28:47 +01:00
|
|
|
use futures::{channel::mpsc, Sink};
|
2020-01-19 09:06:48 +01:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
2020-01-20 09:49:17 +01:00
|
|
|
/// A batteries-included runtime of commands and subscriptions.
|
|
|
|
|
///
|
|
|
|
|
/// If you have an [`Executor`], a [`Runtime`] can be leveraged to run any
|
|
|
|
|
/// [`Command`] or [`Subscription`] and get notified of the results!
|
2020-01-19 09:06:48 +01:00
|
|
|
#[derive(Debug)]
|
2020-01-19 11:08:32 +01:00
|
|
|
pub struct Runtime<Hasher, Event, Executor, Sender, Message> {
|
2020-01-19 09:06:48 +01:00
|
|
|
executor: Executor,
|
2020-01-19 11:08:32 +01:00
|
|
|
sender: Sender,
|
2020-01-19 10:17:08 +01:00
|
|
|
subscriptions: subscription::Tracker<Hasher, Event>,
|
2020-01-19 09:06:48 +01:00
|
|
|
_message: PhantomData<Message>,
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-19 11:08:32 +01:00
|
|
|
impl<Hasher, Event, Executor, Sender, Message>
|
|
|
|
|
Runtime<Hasher, Event, Executor, Sender, Message>
|
2020-01-19 09:06:48 +01:00
|
|
|
where
|
|
|
|
|
Hasher: std::hash::Hasher + Default,
|
|
|
|
|
Event: Send + Clone + 'static,
|
|
|
|
|
Executor: self::Executor,
|
2020-02-04 03:28:47 +01:00
|
|
|
Sender:
|
|
|
|
|
Sink<Message, Error = mpsc::SendError> + Unpin + Send + Clone + 'static,
|
2020-01-19 09:06:48 +01:00
|
|
|
Message: Send + 'static,
|
|
|
|
|
{
|
2020-01-20 09:49:17 +01:00
|
|
|
/// Creates a new empty [`Runtime`].
|
|
|
|
|
///
|
|
|
|
|
/// You need to provide:
|
|
|
|
|
/// - an [`Executor`] to spawn futures
|
|
|
|
|
/// - a `Sender` implementing `Sink` to receive the results
|
2020-01-19 11:08:32 +01:00
|
|
|
pub fn new(executor: Executor, sender: Sender) -> Self {
|
2020-01-19 09:06:48 +01:00
|
|
|
Self {
|
2020-01-19 10:17:08 +01:00
|
|
|
executor,
|
2020-01-19 11:08:32 +01:00
|
|
|
sender,
|
2020-01-19 10:17:08 +01:00
|
|
|
subscriptions: subscription::Tracker::new(),
|
2020-01-19 09:06:48 +01:00
|
|
|
_message: PhantomData,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 09:49:17 +01:00
|
|
|
/// Runs the given closure inside the [`Executor`] of the [`Runtime`].
|
|
|
|
|
///
|
|
|
|
|
/// See [`Executor::enter`] to learn more.
|
2020-01-19 10:17:08 +01:00
|
|
|
pub fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
|
|
|
|
|
self.executor.enter(f)
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 09:49:17 +01:00
|
|
|
/// Spawns a [`Command`] in the [`Runtime`].
|
|
|
|
|
///
|
|
|
|
|
/// The resulting `Message` will be forwarded to the `Sender` of the
|
|
|
|
|
/// [`Runtime`].
|
2020-01-19 09:06:48 +01:00
|
|
|
pub fn spawn(&mut self, command: Command<Message>) {
|
|
|
|
|
use futures::{FutureExt, SinkExt};
|
|
|
|
|
|
|
|
|
|
let futures = command.futures();
|
|
|
|
|
|
|
|
|
|
for future in futures {
|
2020-01-19 11:08:32 +01:00
|
|
|
let mut sender = self.sender.clone();
|
2020-01-19 09:06:48 +01:00
|
|
|
|
2020-02-05 04:14:26 +01:00
|
|
|
let future = future.then(|message| async move {
|
2020-02-04 03:28:47 +01:00
|
|
|
let _ = sender.send(message).await;
|
2020-01-19 09:06:48 +01:00
|
|
|
|
2020-02-04 03:28:47 +01:00
|
|
|
()
|
2020-02-05 04:14:26 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
self.executor.spawn(future);
|
2020-01-19 09:06:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 09:49:17 +01:00
|
|
|
/// Tracks a [`Subscription`] in the [`Runtime`].
|
|
|
|
|
///
|
|
|
|
|
/// It will spawn new streams or close old ones as necessary! See
|
|
|
|
|
/// [`Tracker::update`] to learn more about this!
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Tracker::update`]: subscription::Tracker::update
|
2020-01-19 09:06:48 +01:00
|
|
|
pub fn track(
|
|
|
|
|
&mut self,
|
|
|
|
|
subscription: Subscription<Hasher, Event, Message>,
|
|
|
|
|
) {
|
2020-02-16 11:31:37 +01:00
|
|
|
let Runtime {
|
|
|
|
|
executor,
|
|
|
|
|
subscriptions,
|
|
|
|
|
sender,
|
|
|
|
|
..
|
|
|
|
|
} = self;
|
|
|
|
|
|
|
|
|
|
let futures = executor
|
|
|
|
|
.enter(|| subscriptions.update(subscription, sender.clone()));
|
2020-01-19 09:06:48 +01:00
|
|
|
|
|
|
|
|
for future in futures {
|
2020-02-16 11:31:37 +01:00
|
|
|
executor.spawn(future);
|
2020-01-19 09:06:48 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-20 09:49:17 +01:00
|
|
|
/// Broadcasts an event to all the subscriptions currently alive in the
|
|
|
|
|
/// [`Runtime`].
|
|
|
|
|
///
|
|
|
|
|
/// See [`Tracker::broadcast`] to learn more.
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Tracker::broadcast`]: subscription::Tracker::broadcast
|
2020-01-19 09:06:48 +01:00
|
|
|
pub fn broadcast(&mut self, event: Event) {
|
|
|
|
|
self.subscriptions.broadcast(event);
|
|
|
|
|
}
|
|
|
|
|
}
|