Skip to main content

fx_callback/
lib.rs

1/*!
2![Build](https://github.com/yoep/fx-callback/workflows/Build/badge.svg)
3![Version](https://img.shields.io/github/v/tag/yoep/fx-callback?label=version)
4[![Crates](https://img.shields.io/crates/v/fx-callback)](https://crates.io/crates/fx-callback)
5[![License: Apache-2.0](https://img.shields.io/github/license/yoep/fx-callback)](./LICENSE)
6[![codecov](https://codecov.io/gh/yoep/fx-callback/branch/master/graph/badge.svg?token=A801IOOZAH)](https://codecov.io/gh/yoep/fx-callback)
7
8A subscription based callback for data events that might occur within one or more structs.
9It is mainly used within the FX landscape to allow events to be published between multiple structs.
10
11## Example
12
13```rust
14use fx_callback::{Callback, MultiThreadedCallback, Subscription};
15
16/// The events of the struct that informs subscribers about changes to the data within the struct.
17#[derive(Debug, Clone, PartialEq)]
18enum MyEvent {
19    Foo,
20}
21
22/// The struct to which an interested subscriber can subscribe to.
23#[derive(Debug)]
24struct Example {
25    callbacks: MultiThreadedCallback<MyEvent>,
26}
27
28impl Example {
29    fn invoke_event(&self) {
30        self.callbacks.invoke(MyEvent::Foo);
31    }
32}
33
34impl Callback<MyEvent> for Example {
35    fn subscribe(&self) -> Subscription<MyEvent> {
36        self.callbacks.subscribe()
37    }
38}
39```
40
41## Usage
42
43### Subscription/event holder
44
45To get started with adding callbacks to your structs, add one of the implementations of the `Callback` trait.
46Make sure that the struct implements the `Debug` trait.
47
48```rust
49use fx_callback::{Callback, MultiThreadedCallback};
50
51#[derive(Debug)]
52pub struct MyStruct {
53    callbacks: MultiThreadedCallback<MyEvent>,
54}
55```
56
57Add the `Callback` trait implementation to your struct to allow adding callbacks.
58
59```rust
60impl Callback<MyEvent> for MyStruct {
61    fn subscribe(&self) -> Subscription<MyEvent> {
62        self.callbacks.subscribe()
63    }
64
65    fn subscribe_with(&self, subscriber: Subscriber<MyEvent>) {
66        self.callbacks.subscribe_with(subscriber)
67    }
68}
69```
70
71When you want to inform subscribers about a certain event, call the `invoke` method.
72
73```rust
74impl MyStruct {
75    pub fn invoke_event(&self) {
76        self.callbacks.invoke(MyEvent::Foo);
77    }
78}
79```
80
81### Subscriber
82
83The interested subscriber can subscribe to the interested event of a struct that implements the `Callback` trait.
84
85```rust
86use fx_callback::{Callback, MultiThreadedCallback, Subscription};
87use std::io;
88
89#[tokio::main]
90async fn main() -> Result<(), io::Error>{
91    let struct_with_callback = MyStruct::new();
92
93    let mut receiver = struct_with_callback.subscribe();
94    tokio::spawn(async move {
95       while let Ok(event) = receiver.recv().await {
96           println!("Received event: {}", event);
97       }
98    });
99
100    struct_with_callback.invoke_event();
101    Ok(())
102}
103
104#[derive(Debug)]
105pub struct MyStruct {
106    callbacks: MultiThreadedCallback<MyEvent>,
107}
108
109impl Callback<MyEvent> for MyStruct {
110    fn subscribe(&self) -> Subscription<MyEvent> {
111        self.callbacks.subscribe()
112    }
113}
114```
115*/
116
117#[doc(inline)]
118pub use callback::*;
119
120#[cfg(test)]
121#[macro_use]
122mod macros;
123mod callback;