Skip to main content

ferroui_base/data/core/plugins/
binding_plugins.rs

1use super::{
2    ExceptionValidationPlugin, FerroPropertyAccessorPlugin, IDataValidationPlugin, IPropertyAccessorPlugin,
3    IStreamPlugin, IndeiValidationPlugin, InpcPropertyAccessorPlugin, MethodAccessorPlugin, ObservableStreamPlugin,
4    TaskStreamPlugin,
5};
6use std::cell::RefCell;
7use std::rc::Rc;
8
9crate::ferro_thread_local! {
10    static PROPERTY_ACCESSORS: RefCell<Vec<Rc<dyn IPropertyAccessorPlugin>>> = RefCell::new(vec![
11        Rc::new(FerroPropertyAccessorPlugin),
12        Rc::new(MethodAccessorPlugin),
13        Rc::new(InpcPropertyAccessorPlugin),
14    ]);
15    static DATA_VALIDATORS: RefCell<Vec<Rc<dyn IDataValidationPlugin>>> = RefCell::new(vec![
16        Rc::new(IndeiValidationPlugin),
17        Rc::new(ExceptionValidationPlugin),
18    ]);
19    static STREAM_HANDLERS: RefCell<Vec<Rc<dyn IStreamPlugin>>> = RefCell::new(vec![
20        Rc::new(TaskStreamPlugin),
21        Rc::new(ObservableStreamPlugin),
22    ]);
23}
24
25/// Holds the binding plugins registered for the current thread.
26pub struct BindingPlugins;
27
28impl BindingPlugins {
29    /// A snapshot of the property accessor plugins.
30    pub fn property_accessors() -> Vec<Rc<dyn IPropertyAccessorPlugin>> {
31        PROPERTY_ACCESSORS.with(|p| p.borrow().clone())
32    }
33
34    /// A snapshot of the data validation plugins.
35    pub fn data_validators() -> Vec<Rc<dyn IDataValidationPlugin>> {
36        DATA_VALIDATORS.with(|p| p.borrow().clone())
37    }
38
39    /// A snapshot of the stream plugins.
40    pub fn stream_handlers() -> Vec<Rc<dyn IStreamPlugin>> {
41        STREAM_HANDLERS.with(|p| p.borrow().clone())
42    }
43
44    /// Changes the property accessor plugins.
45    pub fn update_property_accessors(f: impl FnOnce(&mut Vec<Rc<dyn IPropertyAccessorPlugin>>)) {
46        PROPERTY_ACCESSORS.with(|p| f(&mut p.borrow_mut()))
47    }
48
49    /// Changes the data validation plugins.
50    pub fn update_data_validators(f: impl FnOnce(&mut Vec<Rc<dyn IDataValidationPlugin>>)) {
51        DATA_VALIDATORS.with(|p| f(&mut p.borrow_mut()))
52    }
53
54    /// Changes the stream plugins.
55    pub fn update_stream_handlers(f: impl FnOnce(&mut Vec<Rc<dyn IStreamPlugin>>)) {
56        STREAM_HANDLERS.with(|p| f(&mut p.borrow_mut()))
57    }
58}