pub struct ReactiveCollection<T, K, IdFn, Cmp, Flt = AcceptAll>where
T: Clone + PartialEq + 'static,
K: Ord + Clone + 'static,
IdFn: Fn(&T) -> K + 'static,
Cmp: Fn(&T, &T) -> Ordering + 'static,
Flt: ItemFilter<T> + 'static,{ /* private fields */ }Expand description
Reactive bridge wrapping zwire_collections::Collection with zwire_core::Scope.
Exposes Self::top_k_signal and Self::top_k_filtered_signal as first-class F_EXTERNAL
reactive Computed<Vec<T>> signals with O(1) Top-K boundary pruning, batched single-pass
materialization via core flush hooks, O(1) per-key Self::get_signal/Self::has_signal,
and STM Draft compatibility.
Implementations§
Source§impl<T, K, IdFn, Cmp> ReactiveCollection<T, K, IdFn, Cmp, AcceptAll>
impl<T, K, IdFn, Cmp> ReactiveCollection<T, K, IdFn, Cmp, AcceptAll>
Source§impl<T, K, IdFn, Cmp, Flt> ReactiveCollection<T, K, IdFn, Cmp, Flt>
impl<T, K, IdFn, Cmp, Flt> ReactiveCollection<T, K, IdFn, Cmp, Flt>
Sourcepub fn with_filter(
scope: &Scope,
get_id: IdFn,
compare: Cmp,
filter: Flt,
) -> Self
pub fn with_filter( scope: &Scope, get_id: IdFn, compare: Cmp, filter: Flt, ) -> Self
Creates a filtered reactive incremental collection bound to scope.
Examples found in repository?
18fn main() {
19 let scope = Scope::new();
20
21 // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22 let bids = ReactiveCollection::with_filter(
23 &scope,
24 |b: &Bid| b.id,
25 |a, b| b.price_cents.cmp(&a.price_cents),
26 |b| b.active && b.qty > 0,
27 );
28
29 bids.upsert_many((1..=10_000u64).map(|id| Bid {
30 id,
31 price_cents: i64::try_from(id).unwrap() * 10,
32 qty: 5,
33 active: true,
34 }));
35
36 let top5 = bids.top_k_signal(5);
37 let best_bid =
38 scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40 let renders = Rc::new(Cell::new(0usize));
41 let renders_c = Rc::clone(&renders);
42 let _sub = scope.subscribe(best_bid, move |price| {
43 renders_c.set(renders_c.get() + 1);
44 println!(
45 "[book] best bid updated -> ${}.{:02}",
46 price / 100,
47 price.abs() % 100
48 );
49 });
50
51 let init_price = scope.get(best_bid);
52 println!(
53 "Initial best bid across {} active bids: ${}.{:02}",
54 bids.indexed_len(),
55 init_price / 100,
56 init_price.abs() % 100
57 );
58
59 // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60 for id in 1..=100u64 {
61 bids.mutate(&id, |b| b.price_cents += 1);
62 }
63 assert_eq!(renders.get(), 0);
64 println!(
65 "After 100 off-top updates: {} UI renders (O(1) pruned)",
66 renders.get()
67 );
68
69 // New aggressive bid beats top-of-book -> triggers exactly 1 render
70 bids.upsert(Bid {
71 id: 99_999,
72 price_cents: 105_000,
73 qty: 12,
74 active: true,
75 });
76 assert_eq!(renders.get(), 1);
77
78 // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79 let available_usd = scope.atom(10_000i64);
80
81 let mut order_a = OptimisticLayer::new(&scope);
82 order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84 let mut order_b = OptimisticLayer::new(&scope);
85 order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86 println!(
87 "After 2 optimistic orders -> displayed balance: ${}",
88 scope.get(available_usd)
89 );
90
91 // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92 scope.set(available_usd, 15_000);
93 assert_eq!(scope.get(available_usd), 13_000);
94 println!(
95 "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96 scope.get(available_usd)
97 );
98
99 // Order A rejected by venue -> surgical revert preserves Order B
100 order_a.revert();
101 assert_eq!(scope.get(available_usd), 14_200);
102 println!(
103 "After Order A rejected -> balance: ${}",
104 scope.get(available_usd)
105 );
106
107 // Order B confirmed by venue -> atomic zero-flicker settle
108 order_b.settle_with(|| scope.set(available_usd, 14_200));
109 assert_eq!(scope.get(available_usd), 14_200);
110 println!(
111 "After Order B settled -> final committed balance: ${}",
112 scope.get(available_usd)
113 );
114}Source§impl<T, K, IdFn, Cmp, Flt> ReactiveCollection<T, K, IdFn, Cmp, Flt>
impl<T, K, IdFn, Cmp, Flt> ReactiveCollection<T, K, IdFn, Cmp, Flt>
Sourcepub fn from_parts(
scope: &Scope,
get_id: IdFn,
compare: Cmp,
filter: Flt,
) -> Self
pub fn from_parts( scope: &Scope, get_id: IdFn, compare: Cmp, filter: Flt, ) -> Self
Creates a reactive incremental collection with any ItemFilter<T> implementation.
Sourcepub fn indexed_len(&self) -> usize
pub fn indexed_len(&self) -> usize
Number of items currently passing the filter and present in the sorted block index.
Examples found in repository?
18fn main() {
19 let scope = Scope::new();
20
21 // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22 let bids = ReactiveCollection::with_filter(
23 &scope,
24 |b: &Bid| b.id,
25 |a, b| b.price_cents.cmp(&a.price_cents),
26 |b| b.active && b.qty > 0,
27 );
28
29 bids.upsert_many((1..=10_000u64).map(|id| Bid {
30 id,
31 price_cents: i64::try_from(id).unwrap() * 10,
32 qty: 5,
33 active: true,
34 }));
35
36 let top5 = bids.top_k_signal(5);
37 let best_bid =
38 scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40 let renders = Rc::new(Cell::new(0usize));
41 let renders_c = Rc::clone(&renders);
42 let _sub = scope.subscribe(best_bid, move |price| {
43 renders_c.set(renders_c.get() + 1);
44 println!(
45 "[book] best bid updated -> ${}.{:02}",
46 price / 100,
47 price.abs() % 100
48 );
49 });
50
51 let init_price = scope.get(best_bid);
52 println!(
53 "Initial best bid across {} active bids: ${}.{:02}",
54 bids.indexed_len(),
55 init_price / 100,
56 init_price.abs() % 100
57 );
58
59 // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60 for id in 1..=100u64 {
61 bids.mutate(&id, |b| b.price_cents += 1);
62 }
63 assert_eq!(renders.get(), 0);
64 println!(
65 "After 100 off-top updates: {} UI renders (O(1) pruned)",
66 renders.get()
67 );
68
69 // New aggressive bid beats top-of-book -> triggers exactly 1 render
70 bids.upsert(Bid {
71 id: 99_999,
72 price_cents: 105_000,
73 qty: 12,
74 active: true,
75 });
76 assert_eq!(renders.get(), 1);
77
78 // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79 let available_usd = scope.atom(10_000i64);
80
81 let mut order_a = OptimisticLayer::new(&scope);
82 order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84 let mut order_b = OptimisticLayer::new(&scope);
85 order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86 println!(
87 "After 2 optimistic orders -> displayed balance: ${}",
88 scope.get(available_usd)
89 );
90
91 // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92 scope.set(available_usd, 15_000);
93 assert_eq!(scope.get(available_usd), 13_000);
94 println!(
95 "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96 scope.get(available_usd)
97 );
98
99 // Order A rejected by venue -> surgical revert preserves Order B
100 order_a.revert();
101 assert_eq!(scope.get(available_usd), 14_200);
102 println!(
103 "After Order A rejected -> balance: ${}",
104 scope.get(available_usd)
105 );
106
107 // Order B confirmed by venue -> atomic zero-flicker settle
108 order_b.settle_with(|| scope.set(available_usd, 14_200));
109 assert_eq!(scope.get(available_usd), 14_200);
110 println!(
111 "After Order B settled -> final committed balance: ${}",
112 scope.get(available_usd)
113 );
114}Sourcepub fn contains_key(&self, id: &K) -> bool
pub fn contains_key(&self, id: &K) -> bool
Returns true if id exists in the collection.
Sourcepub fn get_top_k(&self, k: usize) -> Vec<T>
pub fn get_top_k(&self, k: usize) -> Vec<T>
Returns a cloned snapshot of the current top k indexed items.
Sourcepub fn get_range(&self, offset: usize, limit: usize) -> Vec<T>
pub fn get_range(&self, offset: usize, limit: usize) -> Vec<T>
Returns a sorted window, measured after filtering.
Sourcepub fn iter_sorted(&self) -> IntoIter<T> ⓘ
pub fn iter_sorted(&self) -> IntoIter<T> ⓘ
Iterates over a detached snapshot in exact index order.
Sourcepub fn range_signal(&self, offset: usize, limit: usize) -> Computed<Vec<T>>
pub fn range_signal(&self, offset: usize, limit: usize) -> Computed<Vec<T>>
Tracks a sorted window and suppresses equal snapshots.
Sourcepub fn len_signal(&self) -> Computed<usize>
pub fn len_signal(&self) -> Computed<usize>
Tracks the number of stored rows.
Sourcepub fn indexed_len_signal(&self) -> Computed<usize>
pub fn indexed_len_signal(&self) -> Computed<usize>
Tracks the number of indexed rows.
Sourcepub fn get_signal(&self, id: K) -> Computed<Option<T>>
pub fn get_signal(&self, id: K) -> Computed<Option<T>>
Tracks one row in O(1) per write, including insertions and deletions.
Sourcepub fn has_signal(&self, id: K) -> Computed<bool>
pub fn has_signal(&self, id: K) -> Computed<bool>
Tracks membership in O(1) per write independently of the current index filter.
Sourcepub fn set_ordering(
&self,
compare: impl Fn(&T, &T) -> Ordering + 'static,
filter: impl Fn(&T) -> bool + 'static,
) -> bool
pub fn set_ordering( &self, compare: impl Fn(&T, &T) -> Ordering + 'static, filter: impl Fn(&T) -> bool + 'static, ) -> bool
Rebuilds under a new comparator and filter. Returns false when a callback changed the collection while building the replacement.
Sourcepub fn top_k_signal(&self, k: usize) -> Computed<Vec<T>>
pub fn top_k_signal(&self, k: usize) -> Computed<Vec<T>>
Creates (or returns a cached live) first-class reactive Computed<Vec<T>> tracking the
exact top k items.
Mutations strictly outside the K-th boundary skip materialization and propagation in O(1).
Examples found in repository?
18fn main() {
19 let scope = Scope::new();
20
21 // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22 let bids = ReactiveCollection::with_filter(
23 &scope,
24 |b: &Bid| b.id,
25 |a, b| b.price_cents.cmp(&a.price_cents),
26 |b| b.active && b.qty > 0,
27 );
28
29 bids.upsert_many((1..=10_000u64).map(|id| Bid {
30 id,
31 price_cents: i64::try_from(id).unwrap() * 10,
32 qty: 5,
33 active: true,
34 }));
35
36 let top5 = bids.top_k_signal(5);
37 let best_bid =
38 scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40 let renders = Rc::new(Cell::new(0usize));
41 let renders_c = Rc::clone(&renders);
42 let _sub = scope.subscribe(best_bid, move |price| {
43 renders_c.set(renders_c.get() + 1);
44 println!(
45 "[book] best bid updated -> ${}.{:02}",
46 price / 100,
47 price.abs() % 100
48 );
49 });
50
51 let init_price = scope.get(best_bid);
52 println!(
53 "Initial best bid across {} active bids: ${}.{:02}",
54 bids.indexed_len(),
55 init_price / 100,
56 init_price.abs() % 100
57 );
58
59 // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60 for id in 1..=100u64 {
61 bids.mutate(&id, |b| b.price_cents += 1);
62 }
63 assert_eq!(renders.get(), 0);
64 println!(
65 "After 100 off-top updates: {} UI renders (O(1) pruned)",
66 renders.get()
67 );
68
69 // New aggressive bid beats top-of-book -> triggers exactly 1 render
70 bids.upsert(Bid {
71 id: 99_999,
72 price_cents: 105_000,
73 qty: 12,
74 active: true,
75 });
76 assert_eq!(renders.get(), 1);
77
78 // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79 let available_usd = scope.atom(10_000i64);
80
81 let mut order_a = OptimisticLayer::new(&scope);
82 order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84 let mut order_b = OptimisticLayer::new(&scope);
85 order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86 println!(
87 "After 2 optimistic orders -> displayed balance: ${}",
88 scope.get(available_usd)
89 );
90
91 // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92 scope.set(available_usd, 15_000);
93 assert_eq!(scope.get(available_usd), 13_000);
94 println!(
95 "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96 scope.get(available_usd)
97 );
98
99 // Order A rejected by venue -> surgical revert preserves Order B
100 order_a.revert();
101 assert_eq!(scope.get(available_usd), 14_200);
102 println!(
103 "After Order A rejected -> balance: ${}",
104 scope.get(available_usd)
105 );
106
107 // Order B confirmed by venue -> atomic zero-flicker settle
108 order_b.settle_with(|| scope.set(available_usd, 14_200));
109 assert_eq!(scope.get(available_usd), 14_200);
110 println!(
111 "After Order B settled -> final committed balance: ${}",
112 scope.get(available_usd)
113 );
114}Sourcepub fn top_k_filtered_signal<P>(
&self,
k: usize,
predicate: P,
) -> Computed<Vec<T>>
pub fn top_k_filtered_signal<P>( &self, k: usize, predicate: P, ) -> Computed<Vec<T>>
Creates a first-class reactive Computed<Vec<T>> with an additional per-view predicate
and re-entrancy-safe O(1) Top-K boundary pruning.
Sourcepub fn dispose_top_k_signal(&self, sig: Computed<Vec<T>>) -> bool
pub fn dispose_top_k_signal(&self, sig: Computed<Vec<T>>) -> bool
Unregisters a previously created Top-K signal view and disposes its reactive node in the
owning Scope.
Sourcepub fn upsert(&self, item: T)
pub fn upsert(&self, item: T)
Inserts or replaces an item and incrementally updates any affected watched Top-K signals.
Examples found in repository?
18fn main() {
19 let scope = Scope::new();
20
21 // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22 let bids = ReactiveCollection::with_filter(
23 &scope,
24 |b: &Bid| b.id,
25 |a, b| b.price_cents.cmp(&a.price_cents),
26 |b| b.active && b.qty > 0,
27 );
28
29 bids.upsert_many((1..=10_000u64).map(|id| Bid {
30 id,
31 price_cents: i64::try_from(id).unwrap() * 10,
32 qty: 5,
33 active: true,
34 }));
35
36 let top5 = bids.top_k_signal(5);
37 let best_bid =
38 scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40 let renders = Rc::new(Cell::new(0usize));
41 let renders_c = Rc::clone(&renders);
42 let _sub = scope.subscribe(best_bid, move |price| {
43 renders_c.set(renders_c.get() + 1);
44 println!(
45 "[book] best bid updated -> ${}.{:02}",
46 price / 100,
47 price.abs() % 100
48 );
49 });
50
51 let init_price = scope.get(best_bid);
52 println!(
53 "Initial best bid across {} active bids: ${}.{:02}",
54 bids.indexed_len(),
55 init_price / 100,
56 init_price.abs() % 100
57 );
58
59 // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60 for id in 1..=100u64 {
61 bids.mutate(&id, |b| b.price_cents += 1);
62 }
63 assert_eq!(renders.get(), 0);
64 println!(
65 "After 100 off-top updates: {} UI renders (O(1) pruned)",
66 renders.get()
67 );
68
69 // New aggressive bid beats top-of-book -> triggers exactly 1 render
70 bids.upsert(Bid {
71 id: 99_999,
72 price_cents: 105_000,
73 qty: 12,
74 active: true,
75 });
76 assert_eq!(renders.get(), 1);
77
78 // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79 let available_usd = scope.atom(10_000i64);
80
81 let mut order_a = OptimisticLayer::new(&scope);
82 order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84 let mut order_b = OptimisticLayer::new(&scope);
85 order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86 println!(
87 "After 2 optimistic orders -> displayed balance: ${}",
88 scope.get(available_usd)
89 );
90
91 // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92 scope.set(available_usd, 15_000);
93 assert_eq!(scope.get(available_usd), 13_000);
94 println!(
95 "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96 scope.get(available_usd)
97 );
98
99 // Order A rejected by venue -> surgical revert preserves Order B
100 order_a.revert();
101 assert_eq!(scope.get(available_usd), 14_200);
102 println!(
103 "After Order A rejected -> balance: ${}",
104 scope.get(available_usd)
105 );
106
107 // Order B confirmed by venue -> atomic zero-flicker settle
108 order_b.settle_with(|| scope.set(available_usd, 14_200));
109 assert_eq!(scope.get(available_usd), 14_200);
110 println!(
111 "After Order B settled -> final committed balance: ${}",
112 scope.get(available_usd)
113 );
114}Sourcepub fn try_upsert(&self, item: T) -> bool
pub fn try_upsert(&self, item: T) -> bool
Returns false if a reentrant policy callback changed the index during this edit.
Sourcepub fn upsert_many<I>(&self, items: I)where
I: IntoIterator<Item = T>,
pub fn upsert_many<I>(&self, items: I)where
I: IntoIterator<Item = T>,
Inserts or replaces a batch of items.
Examples found in repository?
18fn main() {
19 let scope = Scope::new();
20
21 // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22 let bids = ReactiveCollection::with_filter(
23 &scope,
24 |b: &Bid| b.id,
25 |a, b| b.price_cents.cmp(&a.price_cents),
26 |b| b.active && b.qty > 0,
27 );
28
29 bids.upsert_many((1..=10_000u64).map(|id| Bid {
30 id,
31 price_cents: i64::try_from(id).unwrap() * 10,
32 qty: 5,
33 active: true,
34 }));
35
36 let top5 = bids.top_k_signal(5);
37 let best_bid =
38 scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40 let renders = Rc::new(Cell::new(0usize));
41 let renders_c = Rc::clone(&renders);
42 let _sub = scope.subscribe(best_bid, move |price| {
43 renders_c.set(renders_c.get() + 1);
44 println!(
45 "[book] best bid updated -> ${}.{:02}",
46 price / 100,
47 price.abs() % 100
48 );
49 });
50
51 let init_price = scope.get(best_bid);
52 println!(
53 "Initial best bid across {} active bids: ${}.{:02}",
54 bids.indexed_len(),
55 init_price / 100,
56 init_price.abs() % 100
57 );
58
59 // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60 for id in 1..=100u64 {
61 bids.mutate(&id, |b| b.price_cents += 1);
62 }
63 assert_eq!(renders.get(), 0);
64 println!(
65 "After 100 off-top updates: {} UI renders (O(1) pruned)",
66 renders.get()
67 );
68
69 // New aggressive bid beats top-of-book -> triggers exactly 1 render
70 bids.upsert(Bid {
71 id: 99_999,
72 price_cents: 105_000,
73 qty: 12,
74 active: true,
75 });
76 assert_eq!(renders.get(), 1);
77
78 // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79 let available_usd = scope.atom(10_000i64);
80
81 let mut order_a = OptimisticLayer::new(&scope);
82 order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84 let mut order_b = OptimisticLayer::new(&scope);
85 order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86 println!(
87 "After 2 optimistic orders -> displayed balance: ${}",
88 scope.get(available_usd)
89 );
90
91 // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92 scope.set(available_usd, 15_000);
93 assert_eq!(scope.get(available_usd), 13_000);
94 println!(
95 "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96 scope.get(available_usd)
97 );
98
99 // Order A rejected by venue -> surgical revert preserves Order B
100 order_a.revert();
101 assert_eq!(scope.get(available_usd), 14_200);
102 println!(
103 "After Order A rejected -> balance: ${}",
104 scope.get(available_usd)
105 );
106
107 // Order B confirmed by venue -> atomic zero-flicker settle
108 order_b.settle_with(|| scope.set(available_usd, 14_200));
109 assert_eq!(scope.get(available_usd), 14_200);
110 println!(
111 "After Order B settled -> final committed balance: ${}",
112 scope.get(available_usd)
113 );
114}Sourcepub fn try_upsert_many(&self, items: impl IntoIterator<Item = T>) -> bool
pub fn try_upsert_many(&self, items: impl IntoIterator<Item = T>) -> bool
Atomically applies all rows. A competing reentrant edit supersedes this batch.
Sourcepub fn update(&self, id: &K, updater: impl FnOnce(&T) -> T) -> bool
pub fn update(&self, id: &K, updater: impl FnOnce(&T) -> T) -> bool
Updates a snapshot of the item. Returns false if the key was absent or the callback replaced or deleted it before the edit could be applied.
Sourcepub fn mutate(&self, id: &K, mutator: impl FnOnce(&mut T)) -> bool
pub fn mutate(&self, id: &K, mutator: impl FnOnce(&mut T)) -> bool
Mutates a detached copy with the same conflict semantics as Self::update.
Examples found in repository?
18fn main() {
19 let scope = Scope::new();
20
21 // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22 let bids = ReactiveCollection::with_filter(
23 &scope,
24 |b: &Bid| b.id,
25 |a, b| b.price_cents.cmp(&a.price_cents),
26 |b| b.active && b.qty > 0,
27 );
28
29 bids.upsert_many((1..=10_000u64).map(|id| Bid {
30 id,
31 price_cents: i64::try_from(id).unwrap() * 10,
32 qty: 5,
33 active: true,
34 }));
35
36 let top5 = bids.top_k_signal(5);
37 let best_bid =
38 scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40 let renders = Rc::new(Cell::new(0usize));
41 let renders_c = Rc::clone(&renders);
42 let _sub = scope.subscribe(best_bid, move |price| {
43 renders_c.set(renders_c.get() + 1);
44 println!(
45 "[book] best bid updated -> ${}.{:02}",
46 price / 100,
47 price.abs() % 100
48 );
49 });
50
51 let init_price = scope.get(best_bid);
52 println!(
53 "Initial best bid across {} active bids: ${}.{:02}",
54 bids.indexed_len(),
55 init_price / 100,
56 init_price.abs() % 100
57 );
58
59 // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60 for id in 1..=100u64 {
61 bids.mutate(&id, |b| b.price_cents += 1);
62 }
63 assert_eq!(renders.get(), 0);
64 println!(
65 "After 100 off-top updates: {} UI renders (O(1) pruned)",
66 renders.get()
67 );
68
69 // New aggressive bid beats top-of-book -> triggers exactly 1 render
70 bids.upsert(Bid {
71 id: 99_999,
72 price_cents: 105_000,
73 qty: 12,
74 active: true,
75 });
76 assert_eq!(renders.get(), 1);
77
78 // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79 let available_usd = scope.atom(10_000i64);
80
81 let mut order_a = OptimisticLayer::new(&scope);
82 order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84 let mut order_b = OptimisticLayer::new(&scope);
85 order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86 println!(
87 "After 2 optimistic orders -> displayed balance: ${}",
88 scope.get(available_usd)
89 );
90
91 // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92 scope.set(available_usd, 15_000);
93 assert_eq!(scope.get(available_usd), 13_000);
94 println!(
95 "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96 scope.get(available_usd)
97 );
98
99 // Order A rejected by venue -> surgical revert preserves Order B
100 order_a.revert();
101 assert_eq!(scope.get(available_usd), 14_200);
102 println!(
103 "After Order A rejected -> balance: ${}",
104 scope.get(available_usd)
105 );
106
107 // Order B confirmed by venue -> atomic zero-flicker settle
108 order_b.settle_with(|| scope.set(available_usd, 14_200));
109 assert_eq!(scope.get(available_usd), 14_200);
110 println!(
111 "After Order B settled -> final committed balance: ${}",
112 scope.get(available_usd)
113 );
114}