pub fn create_effect<'a, F: Fn() + Send + Sync + 'static>(&'a mut self, effect: F, dependencies: &[&'a dyn Changeable]) {
        // === Bind to Dependencies === //
        let notification = crate::notify(effect);
        let mut lifetimes = Vec::default();
        for dependency in dependencies {
            let lifetime = dependency.when_changed(notification.clone());
            lifetimes.push(lifetime);
        }

        // === Create Invoking Function === //
        // Create a temporary Binding to allow us to invoke the effect if needed
        let notify_clone = notification.clone();
        let invoke_effect = move || {
            let control = crate::bind(false);
            let mut control_life = control.when_changed(notify_clone.clone());
            control.set(true);
            control_life.done();
        };

        // === Insert Effect === //
        let effects = self.widget_effects.entry(self.current_id).or_insert(resources::Resources::default());
        if effects.contains::<MultiState<Vec<Box<dyn Releasable>>>>() {
            let mut state = effects.get_mut::<MultiState<Vec<Box<dyn Releasable>>>>().unwrap();
            let old_size = state.data.len();
            state.get_or_add(lifetimes, &mut self.current_effect_index);
            if old_size != state.data.len() {
                // Just added -> invoke effect
                invoke_effect();
            }
        } else {
            let state = MultiState::new(lifetimes);
            effects.insert(state);
            invoke_effect();
            self.current_effect_index += 1;
        }
    }
/// Creates a callback that runs as a side-effect of its dependencies, running only when one of them is updated.
///
/// All dependencies must be implement the [Changeable](crate::Changeable) trait, which means it will generally
/// work best with [Binding](crate::Binding) values.
///
/// For more details, check out [React's documentation](https://reactjs.org/docs/hooks-effect.html),
/// upon which this method is based.
///
/// # Arguments
///
/// * `effect`: The side-effect function
/// * `dependencies`: The dependencies the effect relies on
///
/// returns: ()
///
/// # Examples
///
/// ```
/// # use kayak_core::{bind, Binding, Bound, KayakContext};
/// # let mut context = KayakContext::new();
///
/// let my_state: Binding<i32> = bind(0i32);
/// let my_state_clone = my_state.clone();
/// context.create_effect(move || {
///     println!("Value: {}", my_state_clone.get());
/// }, &[&my_state]);
/// ```
Creates a callback that runs as a side-effect of its dependencies, running only when one of them is updated.
All dependencies must be implement the [Changeable](crate::Changeable) trait, which means it will generally
work best with [Binding](crate::Binding) values.

For more details, check out [React's documentation],
upon which this method is based.
Creates a callback that runs as a side - effect of its dependencies running only when one of them is updated . All dependencies must be implement the [ Changeable ] ( crate :: Changeable ) trait which means it will generally work best with [ Binding ] ( crate :: Binding ) values . For more details check out [ React ' s documentation ] upon which this method is based .
pub fn create_effect<'a, F: Fn() + Send + Sync + 'static>(&'a mut self, effect: F, dependencies: &[&'a dyn Changeable]) {
        // === Bind to Dependencies === //
        let notification = crate::notify(effect);
        let mut lifetimes = Vec::default();
        for dependency in dependencies {
            let lifetime = dependency.when_changed(notification.clone());
            lifetimes.push(lifetime);
        }
        // === Create Invoking Function === //
        // Create a temporary Binding to allow us to invoke the effect if needed
        let notify_clone = notification.clone();
        let invoke_effect = move || {
            let control = crate::bind(false);
            let mut control_life = control.when_changed(notify_clone.clone());
            control.set(true);
            control_life.done();
        };
        // === Insert Effect === //
        let effects = self.widget_effects.entry(self.current_id).or_insert(resources::Resources::default());
        if effects.contains::<MultiState<Vec<Box<dyn Releasable>>>>() {
            let mut state = effects.get_mut::<MultiState<Vec<Box<dyn Releasable>>>>().unwrap();
            let old_size = state.data.len();
            state.get_or_add(lifetimes, &mut self.current_effect_index);
            if old_size != state.data.len() {
                // Just added -> invoke effect
                invoke_effect();
            }
        } else {
            let state = MultiState::new(lifetimes);
            effects.insert(state);
            invoke_effect();
            self.current_effect_index += 1;
        }
    }
pub fn create_effect < ' a , F : Fn ( ) + Send + Sync + ' static > ( & ' a mut self , effect : F , dependencies : & [ & ' a dyn Changeable ] ) { // === Bind to Dependencies === // let notification = crate :: notify ( effect ) ; let mut lifetimes = Vec :: default ( ) ; for dependency in dependencies { let lifetime = dependency . when_changed ( notification . clone ( ) ) ; lifetimes . push ( lifetime ) ; } // === Create Invoking Function === // // Create a temporary Binding to allow us to invoke the effect if needed let notify_clone = notification . clone ( ) ; let invoke_effect = move | | { let control = crate :: bind ( false ) ; let mut control_life = control . when_changed ( notify_clone . clone ( ) ) ; control . set ( true ) ; control_life . done ( ) ; } ; // === Insert Effect === // let effects = self . widget_effects . entry ( self . current_id ) . or_insert ( resources :: Resources :: default ( ) ) ; if effects . contains :: < MultiState < Vec < Box < dyn Releasable > > > > ( ) { let mut state = effects . get_mut :: < MultiState < Vec < Box < dyn Releasable > > > > ( ) . unwrap ( ) ; let old_size = state . data . len ( ) ; state . get_or_add ( lifetimes , & mut self . current_effect_index ) ; if old_size != state . data . len ( ) { // Just added -> invoke effect invoke_effect ( ) ; } } else { let state = MultiState :: new ( lifetimes ) ; effects . insert ( state ) ; invoke_effect ( ) ; self . current_effect_index += 1 ; } }
Creates a callback that runs as a side-effect of its dependencies, running only when one of them is updated.
Creates a callback that runs as a side - effect of its dependencies running only when one of them is updated .
&'a mut self
effect
F
dependencies
&[&'a dyn Changeable]
&'a mut self
effect
F
dependencies
&[&'a dyn Changeable]
examples
None
use kayak_core::{bind, binding, bound, kayakcontext};
None
let mut context = kayakcontext::new();
let my_state: Binding = bind(0i32);
let my_state_clone = my_state.clone();
context.create_effect(move || {
println!("Value: {}", my_state_clone.get());
}, &[&my_state]);
```
let my_state : Binding = bind ( 0i32 ) ; let my_state_clone = my_state . clone () ; context . create_effect ( move || { println! ( " Value : {} " my_state_clone . get () ) ; } & [ &my_state ] ) ; ` ` `