use std::collections::VecDeque;
use std::rc::Rc;
use gpui_kit::{AnyWeakEntity, App, Global};
use subspace_contracts::{MessageKey, Request};
use super::{BoardContracts, ContractError, Role};
pub(super) const MAX_DEPTH: usize = 8;
pub(super) type Delivery = Box<dyn FnOnce(&[&'static str], &mut App)>;
pub(super) type Receive<E> = Rc<dyn Fn(&E, &mut App)>;
pub(super) type Answer<R> = Rc<dyn Fn(&R, &mut App) -> Option<<R as Request>::Response>>;
struct Pending {
publisher: &'static str,
key: MessageKey,
depth: usize,
deliver: Delivery,
}
#[derive(Default)]
pub(super) struct Broker {
boards: Vec<(&'static str, BoardContracts)>,
queue: VecDeque<Pending>,
flushing: bool,
depth: Option<usize>,
answering: Vec<&'static str>,
}
impl Global for Broker {}
impl Broker {
pub(super) fn install(&mut self, boards: Vec<(&'static str, BoardContracts)>) {
self.boards = boards;
}
pub(super) fn check(
&self,
board: &'static str,
role: Role,
key: MessageKey,
) -> Result<(), ContractError> {
let declared = self
.boards
.iter()
.any(|(id, contracts)| *id == board && contracts.declares(role, key));
match declared {
true => Ok(()),
false => Err(ContractError::Undeclared { board, role, key }),
}
}
pub(super) fn enqueue(
&mut self,
publisher: &'static str,
key: MessageKey,
deliver: Delivery,
) -> Result<bool, ContractError> {
self.check(publisher, Role::Publishes, key)?;
let depth = self.depth.map_or(0, |depth| depth + 1);
if depth > MAX_DEPTH {
return Err(ContractError::TooDeep {
board: publisher,
key,
});
}
self.queue.push_back(Pending {
publisher,
key,
depth,
deliver,
});
Ok(!std::mem::replace(&mut self.flushing, true))
}
pub(super) fn next_delivery(&mut self) -> Option<(Delivery, Vec<&'static str>)> {
let Some(pending) = self.queue.pop_front() else {
self.flushing = false;
self.depth = None;
return None;
};
self.depth = Some(pending.depth);
let boards = self
.boards
.iter()
.filter(|(id, contracts)| {
*id != pending.publisher && contracts.declares(Role::Subscribes, pending.key)
})
.map(|(id, _)| *id)
.collect();
Some((pending.deliver, boards))
}
pub(super) fn responders(
&self,
requester: &'static str,
key: MessageKey,
) -> Result<Vec<&'static str>, ContractError> {
self.check(requester, Role::Requests, key)?;
Ok(self
.boards
.iter()
.filter(|(id, contracts)| {
*id != requester
&& !self.answering.contains(id)
&& contracts.declares(Role::Responds, key)
})
.map(|(id, _)| *id)
.collect())
}
pub(super) fn begin_answer(&mut self, board: &'static str) {
self.answering.push(board);
}
pub(super) fn end_answer(&mut self, board: &'static str) {
if let Some(index) = self.answering.iter().rposition(|id| *id == board) {
self.answering.remove(index);
}
}
}
pub(super) struct Endpoint<F> {
pub(super) board: &'static str,
pub(super) entity: AnyWeakEntity,
pub(super) call: F,
}
pub(super) struct Endpoints<F>(Vec<Endpoint<F>>);
impl<F> Default for Endpoints<F> {
fn default() -> Self {
Self(Vec::new())
}
}
impl<F: Clone> Endpoints<F> {
pub(super) fn attach(
&mut self,
endpoint: Endpoint<F>,
replaces: fn(&Endpoint<F>, &Endpoint<F>) -> bool,
) {
self.0
.retain(|old| old.entity.is_upgradable() && !replaces(old, &endpoint));
self.0.push(endpoint);
}
pub(super) fn calls(&mut self, boards: &[&'static str]) -> Vec<(&'static str, F)> {
self.0.retain(|endpoint| endpoint.entity.is_upgradable());
boards
.iter()
.flat_map(|board| {
self.0
.iter()
.filter(move |endpoint| endpoint.board == *board)
})
.map(|endpoint| (endpoint.board, endpoint.call.clone()))
.collect()
}
#[cfg(test)]
pub(super) fn len(&self) -> usize {
self.0.len()
}
}
pub(super) struct Receivers<E: 'static>(pub(super) Endpoints<Receive<E>>);
impl<E: 'static> Default for Receivers<E> {
fn default() -> Self {
Self(Endpoints::default())
}
}
impl<E: 'static> Global for Receivers<E> {}
pub(super) struct Responders<R: Request>(pub(super) Endpoints<Answer<R>>);
impl<R: Request> Default for Responders<R> {
fn default() -> Self {
Self(Endpoints::default())
}
}
impl<R: Request> Global for Responders<R> {}