Skip to main content

ankurah_signals/
lib.rs

1/*!
2A signals library designed to support Ankurah's needs - but can be used independently as well.
3Supports both native Rust and WebAssembly/React use cases.
4
5# Basic Usage in Rust
6
7```rust
8# use ankurah_signals::{Mut, Subscribe, CallbackObserver};
9# use std::sync::Arc;
10
11let day = Mut::new("Friday");
12let vibe = Mut::new("grindcore");
13
14// Sometimes directly subscribing to the signal can be useful
15// your listener will not be called immediately, only when the signal changes
16// the subscription will be removed when the SubscriptionGuard is dropped
17let _guard1 = day.subscribe(|v| println!("The day is: {}", v));
18let _guard2 = vibe.subscribe(|v| println!("Vibe: {}", v));
19
20// For things that need to track signal dependencies, we can use an Observer
21let renderer = {
22    let day = day.read(); // Read<T> signals can be constructed from a Mut<T> signal
23    let vibe = vibe.read();
24    CallbackObserver::new(Arc::new(move ||{
25        // Your "render" function that uses signals
26        // The Observer will automatically subscribe to the signals used
27        // during this dispatch using the thread-local CurrentObserver
28        println!("It's {day} and I'm {vibe}") // the Observer will automatically subscribe to the signals
29    }))
30};
31
32renderer.trigger(); // trigger the initial "render". Signals used will be tracked by the observer.
33// Should print:
34// It's Friday and I'm grindcore
35
36vibe.set("chillmaxing"); // triggers the direct signal subscription AND the observer
37// Should print:
38// Vibe: chillmaxing
39// It's Friday and I'm chillmaxing
40
41day.set("Saturday");
42// Should print:
43// The day is: Saturday
44// It's Saturday and I'm chillmaxing
45```
46
47*/
48
49pub mod broadcast;
50mod context;
51pub mod observer;
52pub mod porcelain;
53pub mod signal;
54mod value;
55
56#[cfg(feature = "reactive-graph")]
57pub mod reactive_graph;
58
59#[cfg(feature = "react")]
60pub mod react;
61
62#[cfg(feature = "react-native")]
63pub mod react_native;
64
65// UniFFI scaffolding for react-native feature
66#[cfg(feature = "react-native")]
67uniffi::setup_scaffolding!();
68
69#[cfg(feature = "jsvalue")]
70pub mod jsvalue;
71
72pub use broadcast::BroadcastId;
73pub use context::*;
74pub use observer::*;
75pub use porcelain::*;
76pub use signal::*;
77
78#[cfg(feature = "reactive-graph")]
79pub use reactive_graph::*;
80
81#[cfg(feature = "react")]
82pub use react::*;
83
84#[cfg(feature = "react-native")]
85pub use react_native::*;
86
87#[cfg(feature = "jsvalue")]
88pub use jsvalue::*;