use crate::data::core::WeakValue;
use crate::reactive::{IObservable, Observable};
use crate::{BoxedValue, PropertyValue};
use std::rc::Rc;
pub trait IStreamPlugin {
fn match_(&self, reference: &WeakValue) -> bool;
fn start(&self, reference: &WeakValue) -> Option<Rc<dyn IObservable<Option<BoxedValue>>>>;
}
#[derive(Clone)]
pub struct ObservableValue(pub Rc<dyn IObservable<Option<BoxedValue>>>);
impl ObservableValue {
pub fn new<T: PropertyValue>(source: Rc<dyn IObservable<T>>) -> Self {
use crate::reactive::ObservableExt;
Self(source.select(|v| Some(Rc::new(v) as BoxedValue)))
}
}
impl PartialEq for ObservableValue {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
}
pub struct ObservableStreamPlugin;
impl IStreamPlugin for ObservableStreamPlugin {
fn match_(&self, reference: &WeakValue) -> bool {
reference.upgrade().is_some_and(|t| t.is::<ObservableValue>())
}
fn start(&self, reference: &WeakValue) -> Option<Rc<dyn IObservable<Option<BoxedValue>>>> {
match reference.upgrade() {
Some(target) => target.downcast_ref::<ObservableValue>().map(|o| o.0.clone()),
None => Some(Observable::empty()),
}
}
}