pub struct FeagiSignal<T> { /* private fields */ }Implementations§
Source§impl<T> FeagiSignal<T>
impl<T> FeagiSignal<T>
Sourcepub fn connect<F>(&mut self, f: F) -> FeagiSignalIndex
pub fn connect<F>(&mut self, f: F) -> FeagiSignalIndex
Connects a callback to this signal.
The callback will be invoked whenever emit() is called.
Returns a handle that can be used to disconnect later.
§Example
use feagi_structures::FeagiSignal;
let mut signal = FeagiSignal::new();
let handle = signal.connect(|value: &i32| {
println!("Received: {}", value);
});
signal.emit(&42);
signal.disconnect(handle).unwrap();Sourcepub fn disconnect(
&mut self,
index: FeagiSignalIndex,
) -> Result<(), FeagiDataError>
pub fn disconnect( &mut self, index: FeagiSignalIndex, ) -> Result<(), FeagiDataError>
Disconnects a previously connected callback.
Returns an error if no callback with the given index exists.
Sourcepub fn emit(&mut self, value: &T)
pub fn emit(&mut self, value: &T)
Emits an event to all connected callbacks.
Callbacks are invoked in arbitrary order.
Helper to connect a closure that captures an Arc<Mutex<S>>.
This is a convenience method for the common pattern of calling methods on shared mutable state from within the callback.
§Example
use std::sync::{Arc, Mutex};
use feagi_structures::FeagiSignal;
struct Counter { value: i32 }
impl Counter {
fn increment(&mut self) { self.value += 1; }
}
let counter = Arc::new(Mutex::new(Counter { value: 0 }));
let mut signal = FeagiSignal::new();
signal.connect_with_shared_state(
Arc::clone(&counter),
|state, _event| state.increment()
);
signal.emit(&"event");
assert_eq!(counter.lock().unwrap().value, 1);Sourcepub fn listener_count(&self) -> usize
pub fn listener_count(&self) -> usize
Returns the number of connected listeners.
Sourcepub fn disconnect_all(&mut self)
pub fn disconnect_all(&mut self)
Removes all connected listeners.
Trait Implementations§
Source§impl<T> Debug for FeagiSignal<T>
impl<T> Debug for FeagiSignal<T>
Auto Trait Implementations§
impl<T> Freeze for FeagiSignal<T>
impl<T> !RefUnwindSafe for FeagiSignal<T>
impl<T> Send for FeagiSignal<T>
impl<T> !Sync for FeagiSignal<T>
impl<T> Unpin for FeagiSignal<T>
impl<T> !UnwindSafe for FeagiSignal<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more