use super::pubsub::PubSub;
use super::queue::Queue;
use super::ref_::Ref;
use super::semaphore::Semaphore;
use crate::kernel::Effect;
use crate::scope::Scope;
pub struct SubscriptionRef<A> {
cell: Ref<A>,
hub: PubSub<A>,
gate: Semaphore,
}
impl<A> Clone for SubscriptionRef<A> {
fn clone(&self) -> Self {
Self {
cell: self.cell.clone(),
hub: self.hub.clone(),
gate: self.gate.clone(),
}
}
}
impl<A: std::fmt::Debug> std::fmt::Debug for SubscriptionRef<A> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SubscriptionRef")
.field("value", &self.cell)
.field("subscribers", &self.hub.subscriber_count())
.finish()
}
}
impl<A: Clone + Send + 'static> SubscriptionRef<A> {
pub fn new(value: A) -> Self {
Self {
cell: Ref::new(value),
hub: PubSub::unbounded(),
gate: Semaphore::new(1),
}
}
pub fn get<E, R>(&self) -> Effect<A, E, R> {
self.cell.get()
}
pub fn set<E, R>(&self, value: A) -> Effect<(), E, R>
where
E: Send + 'static,
R: Send + Sync + 'static,
{
let hub = self.hub.clone();
let v = value.clone();
self
.gate
.with_permit(self.cell.set(value).then(hub.publish(v)).map(drop))
}
pub fn update<E, R>(&self, f: impl FnOnce(A) -> A + Send + 'static) -> Effect<(), E, R>
where
E: Send + 'static,
R: Send + Sync + 'static,
{
let hub = self.hub.clone();
self.gate.with_permit(
self
.cell
.update_and_get(f)
.flat_map(move |new| hub.publish(new))
.map(drop),
)
}
pub fn modify<B, E, R>(&self, f: impl FnOnce(A) -> (B, A) + Send + 'static) -> Effect<B, E, R>
where
B: Send + 'static,
E: Send + 'static,
R: Send + Sync + 'static,
{
let hub = self.hub.clone();
self.gate.with_permit(
self
.cell
.modify(move |a| {
let (b, new) = f(a);
((b, new.clone()), new)
})
.flat_map(move |(b, new)| hub.publish(new).map(move |_| b)),
)
}
pub fn subscribe<E, R>(&self, scope: &Scope) -> Effect<Queue<A>, E, R>
where
E: Send + 'static,
R: Send + Sync + 'static,
{
let cell = self.cell.clone();
self
.gate
.with_permit(self.hub.subscribe(scope).flat_map(move |q: Queue<A>| {
let q2 = q.clone();
cell
.get()
.flat_map(move |current| q2.offer(current))
.map(move |_| q)
}))
}
}