Expand description
A subscription based callback for data events that might occur within one or more structs. It is mainly used within the FX landscape to allow events to be published between multiple structs.
§Example
use fx_callback::{Callback, MultiThreadedCallback, Subscriber, Subscription};
/// The events of the struct that informs subscribers about changes to the data within the struct.
#[derive(Debug, Clone, PartialEq)]
enum MyEvent {
Foo,
}
/// The struct to which an interested subscriber can subscribe to.
#[derive(Debug)]
struct Example {
callbacks: MultiThreadedCallback<MyEvent>,
}
impl Example {
fn invoke_event(&self) {
self.callbacks.invoke(MyEvent::Foo);
}
}
impl Callback<MyEvent> for Example {
fn subscribe(&self) -> Subscription<MyEvent> {
self.callbacks.subscribe()
}
fn subscribe_with(&self, subscriber: Subscriber<MyEvent>) {
self.callbacks.subscribe_with(subscriber)
}
}
§Usage
§Subscription/event holder
To get started with adding callbacks to your structs, add one of the implementations of the Callback
trait.
Make sure that the struct implements the Debug
trait.
use fx_callback::{Callback, MultiThreadedCallback};
#[derive(Debug)]
pub struct MyStruct {
callbacks: MultiThreadedCallback<MyEvent>,
}
Add the Callback
trait implementation to your struct to allow adding callbacks.
impl Callback<MyEvent> for MyStruct {
fn subscribe(&self) -> Subscription<MyEvent> {
self.callbacks.subscribe()
}
fn subscribe_with(&self, subscriber: Subscriber<MyEvent>) {
self.callbacks.subscribe_with(subscriber)
}
}
When you want to inform subscribers about a certain event, call the invoke
method.
impl MyStruct {
pub fn invoke_event(&self) {
self.callbacks.invoke(MyEvent::Foo);
}
}
§Subscriber
The interested subscriber can subscribe to the interested event of a struct that implements the Callback
trait.
use fx_callback::{Callback, MultiThreadedCallback, Subscriber, Subscription};
use tokio::runtime::Runtime;
fn main() {
let runtime = Runtime::new().unwrap();
let struct_with_callback = MyStruct::new();
let mut receiver = struct_with_callback.subscribe();
runtime.spawn(async move {
loop {
if let Some(event) = receiver.recv().await {
println!("Received event: {}", event);
} else {
break;
}
}
});
struct_with_callback.invoke_event();
}
Structs§
- Multi
Threaded Callback - A multithreaded callback holder.
- Single
Threaded Callback - A single threaded or current threaded callback holder.
Traits§
- Callback
- Allows adding callbacks to the struct. The struct will inform the Subscription when a certain event occurs.
Type Aliases§
- Callback
Handle - The unique identifier for a callback.
- Subscriber
- The subscriber type for the interested event. This can be used to send the interested event from multiple sources into one receiver.
- Subscription
- The subscription type for the interested event. Drop this subscription to remove the callback.