cranpose-core 0.1.123

Core runtime for a Jetpack Compose inspired UI framework in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use super::*;

struct SumPolicy;

impl MutationPolicy<i32> for SumPolicy {
    fn equivalent(&self, a: &i32, b: &i32) -> bool {
        a == b
    }

    fn merge(&self, previous: &i32, current: &i32, applied: &i32) -> Option<i32> {
        Some((current - previous) + (applied - previous) + previous)
    }
}

#[test]
fn snapshot_state_global_write_then_read() {
    let _guard = reset_snapshot_runtime();
    let state = SnapshotMutableState::new_in_arc(0, Arc::new(SumPolicy));
    assert_eq!(state.get(), 0);
    state.set(1);
    assert_eq!(state.get(), 1);
}

#[test]
fn snapshot_state_global_equivalent_write_preserves_mutation_policy() {
    let _guard = reset_snapshot_runtime();
    let state = SnapshotMutableState::new_in_arc(0, Arc::new(SumPolicy));
    let notifications = Rc::new(RefCell::new(Vec::new()));
    let notifications_for_observer = Rc::clone(&notifications);
    let _handle =
        crate::snapshot_v2::register_apply_observer(Rc::new(move |modified, snapshot_id| {
            notifications_for_observer
                .borrow_mut()
                .push((modified.len(), snapshot_id));
        }));

    state.set(0);

    let notifications = notifications.borrow();
    assert_eq!(
        notifications.len(),
        0,
        "global equivalent writes must not notify apply observers"
    );
}

#[test]
fn snapshot_state_child_isolation_and_apply() {
    let _guard = reset_snapshot_runtime();
    let state = SnapshotMutableState::new_in_arc(0, Arc::new(SumPolicy));

    let child = take_mutable_snapshot(None, None);
    child.enter(|| {
        state.set(2);
        assert_eq!(state.get(), 2);
    });

    assert_eq!(state.get(), 0);

    child.apply().check();
    assert_eq!(state.get(), 2);
}

#[test]
fn snapshot_state_concurrent_children_merge() {
    let _guard = reset_snapshot_runtime();
    let state = SnapshotMutableState::new_in_arc(0, Arc::new(SumPolicy));

    let first = take_mutable_snapshot(None, None);
    let second = take_mutable_snapshot(None, None);

    first.enter(|| state.set(1));
    second.enter(|| state.set(2));

    first.apply().check();
    second.apply().check();
    assert_eq!(state.get(), 3);
}

#[test]
fn snapshot_state_child_apply_after_parent_history() {
    let _guard = reset_snapshot_runtime();
    let state = SnapshotMutableState::new_in_arc(0, Arc::new(SumPolicy));

    for value in 1..=5 {
        state.set(value);
    }

    let child = take_mutable_snapshot(None, None);
    child.enter(|| state.set(42));

    child.apply().check();
    assert_eq!(state.get(), 42);
}

#[composable]
fn anchor_progress_content(toggle: MutableState<bool>, stats: MutableState<i32>) {
    let show_progress = toggle.value();
    cranpose_core::with_current_composer(|composer| {
        composer.with_group(location_key(file!(), line!(), column!()), |composer| {
            if show_progress {
                composer.with_group(location_key(file!(), line!(), column!()), |composer| {
                    composer.emit_node(|| TestDummyNode);
                });
            }
        });
    });
    let _ = stats.value();
}

#[test]
fn stats_watchers_survive_conditional_toggle() {
    fn drain_all(composition: &mut Composition<MemoryApplier>) -> Result<(), NodeError> {
        loop {
            if !composition.process_invalid_scopes()? {
                break;
            }
        }
        Ok(())
    }

    let mut composition = test_composition();
    let runtime = composition.runtime_handle();
    let toggle = MutableState::with_runtime(true, runtime.clone());
    let stats = MutableState::with_runtime(0i32, runtime.clone());

    let mut render = { move || anchor_progress_content(toggle, stats) };

    let key = location_key(file!(), line!(), column!());
    composition
        .render(key, &mut render)
        .expect("initial render");
    drain_all(&mut composition).expect("initial drain");
    assert!(
        stats.watcher_count() > 0,
        "initial render should register stats watcher"
    );

    toggle.set_value(false);
    composition
        .render(key, &mut render)
        .expect("render without progress");
    drain_all(&mut composition).expect("drain without progress");
    assert!(
        stats.watcher_count() > 0,
        "conditional removal should not drop stats watcher"
    );

    toggle.set_value(true);
    composition
        .render(key, &mut render)
        .expect("render with progress again");
    drain_all(&mut composition).expect("drain with progress");
    assert!(
        stats.watcher_count() > 0,
        "restoring progress should keep stats watcher"
    );
}

#[test]
fn state_write_prunes_dropped_watchers() {
    let runtime = TestRuntime::new();
    let handle = runtime.handle();
    let state = MutableState::with_runtime(0i32, handle.clone());
    let scope = RecomposeScope::new_for_test(handle);

    state.subscribe_scope_for_test(&scope);
    assert_eq!(state.watcher_count(), 1);

    drop(scope);
    state.set_value(1);

    assert_eq!(state.watcher_count(), 0);
}

#[test]
fn state_subscriber_callback_tracks_first_live_scope() {
    let runtime = TestRuntime::new();
    let handle = runtime.handle();
    let state = MutableState::with_runtime(0i32, handle.clone());
    let notifications = Rc::new(Cell::new(0));
    let notifications_for_callback = Rc::clone(&notifications);
    let callback = Rc::new(move || {
        notifications_for_callback.set(notifications_for_callback.get() + 1);
    });
    state.as_state().on_subscriber(callback.clone());
    let observer = SnapshotStateObserver::new(|callback| callback());

    observer.observe_reads(1usize, |_| {}, || state.get());
    assert_eq!(notifications.get(), 1);

    observer.observe_reads(2usize, |_| {}, || state.get());
    assert_eq!(notifications.get(), 1);

    observer.clear(&1usize);
    observer.clear(&2usize);
    assert!(!state.as_state().has_subscribers());

    observer.observe_reads(3usize, |_| {}, || state.get());
    assert_eq!(notifications.get(), 2);
}

/// A producer gated on `has_subscribers` (an infinite transition) stops for
/// a consumer that only polls the value from a frame effect, because no
/// snapshot observer ever sees that read. The hold is the polling consumer's
/// way to stay counted; the showcase app's starfield froze on a screen with
/// no draw-observed reader before it existed.
#[test]
fn a_subscription_hold_keeps_an_unobserved_state_subscribed() {
    let runtime = TestRuntime::new();
    let handle = runtime.handle();
    let state = MutableState::with_runtime(0i32, handle.clone());
    assert!(!state.as_state().has_subscribers());

    let hold = state.as_state().subscription_hold();
    assert!(state.as_state().has_subscribers());

    let second = state.as_state().subscription_hold();
    drop(hold);
    assert!(state.as_state().has_subscribers());

    drop(second);
    assert!(!state.as_state().has_subscribers());
}

/// The hold also wakes a producer waiting for its first subscriber, through
/// the same callback channel a scope subscription uses.
#[test]
fn a_subscription_hold_fires_the_subscriber_callback() {
    let runtime = TestRuntime::new();
    let handle = runtime.handle();
    let state = MutableState::with_runtime(0i32, handle.clone());
    let notifications = Rc::new(Cell::new(0));
    let notifications_for_callback = Rc::clone(&notifications);
    let callback: Rc<dyn Fn()> = Rc::new(move || {
        notifications_for_callback.set(notifications_for_callback.get() + 1);
    });
    state.as_state().on_subscriber(callback.clone());

    let hold = state.as_state().subscription_hold();
    assert_eq!(notifications.get(), 1);
    drop(hold);
    drop(callback);
}

#[composable]
fn subscriber_callback_reader(state: MutableState<i32>) {
    let _ = state.get();
}

#[test]
fn state_subscriber_callback_fires_once_for_a_composition_read() {
    let mut composition = test_composition();
    let state = MutableState::with_runtime(0i32, composition.runtime_handle());
    let notifications = Rc::new(Cell::new(0));
    let notifications_for_callback = Rc::clone(&notifications);
    let callback: Rc<dyn Fn()> = Rc::new(move || {
        notifications_for_callback.set(notifications_for_callback.get() + 1);
    });
    state.as_state().on_subscriber(callback.clone());

    composition
        .render(location_key(file!(), line!(), column!()), move || {
            subscriber_callback_reader(state);
        })
        .expect("composition read");
    assert_eq!(notifications.get(), 1);

    state.set(1);
    while composition
        .process_invalid_scopes()
        .expect("process state invalidation")
    {}
    assert_eq!(notifications.get(), 1);
}

#[test]
fn state_subscriber_callback_treats_scope_and_read_observers_as_one_subscription() {
    let runtime = TestRuntime::new();
    let state = MutableState::with_runtime(0i32, runtime.handle());
    let notifications = Rc::new(Cell::new(0));
    let notifications_for_callback = Rc::clone(&notifications);
    let callback: Rc<dyn Fn()> = Rc::new(move || {
        notifications_for_callback.set(notifications_for_callback.get() + 1);
    });
    state.as_state().on_subscriber(callback.clone());

    let observer = SnapshotStateObserver::new(|callback| callback());
    observer.observe_reads(1usize, |_| {}, || state.get());
    let scope = RecomposeScope::new_for_test(runtime.handle());
    state.subscribe_scope_for_test(&scope);
    observer.clear(&1usize);
    observer.observe_reads(2usize, |_| {}, || state.get());

    assert_eq!(notifications.get(), 1);
}

#[test]
fn state_subscriber_callback_does_not_retain_dropped_callback() {
    let runtime = TestRuntime::new();
    let handle = runtime.handle();
    let state = MutableState::with_runtime(0i32, handle.clone());
    let callback = Rc::new(|| {});
    let weak = Rc::downgrade(&callback);
    state.as_state().on_subscriber(callback.clone());
    drop(callback);

    let observer = SnapshotStateObserver::new(|callback| callback());
    observer.observe_reads(1usize, |_| {}, || state.get());
    assert!(weak.upgrade().is_none());
}

#[test]
fn state_subscriber_callback_keeps_reentrant_registration() {
    let runtime = TestRuntime::new();
    let handle = runtime.handle();
    let state = MutableState::with_runtime(0i32, handle.clone());
    let nested_notifications = Rc::new(Cell::new(0));
    let nested_callback: Rc<dyn Fn()> = {
        let nested_notifications = Rc::clone(&nested_notifications);
        Rc::new(move || nested_notifications.set(nested_notifications.get() + 1))
    };
    let nested_lifetime = Rc::new(RefCell::new(Some(Rc::clone(&nested_callback))));
    let registered = Rc::new(Cell::new(false));
    let callback: Rc<dyn Fn()> = {
        let registered = Rc::clone(&registered);
        let nested_lifetime = Rc::clone(&nested_lifetime);
        Rc::new(move || {
            if !registered.replace(true) {
                state.as_state().on_subscriber(
                    nested_lifetime
                        .borrow()
                        .as_ref()
                        .expect("nested callback")
                        .clone(),
                );
            }
        })
    };
    state.as_state().on_subscriber(callback.clone());
    let observer = SnapshotStateObserver::new(|callback| callback());

    observer.observe_reads(1usize, |_| {}, || state.get());
    assert_eq!(nested_notifications.get(), 1);
    observer.clear(&1usize);

    observer.observe_reads(2usize, |_| {}, || state.get());
    assert_eq!(nested_notifications.get(), 2);
}

#[test]
fn state_subscriber_callback_prunes_dead_registrations_while_subscribed() {
    let runtime = TestRuntime::new();
    let handle = runtime.handle();
    let state = MutableState::with_runtime(0i32, handle.clone());
    let observer = SnapshotStateObserver::new(|callback| callback());
    observer.observe_reads(1usize, |_| {}, || state.get());

    for _ in 0..256 {
        state.as_state().on_subscriber(Rc::new(|| {}));
    }

    assert_eq!(state.subscriber_callback_count(), 0);
}

#[test]
fn dropping_scope_unregisters_watchers_without_global_prune() {
    let runtime = TestRuntime::new();
    let handle = runtime.handle();
    let state = MutableState::with_runtime(0i32, handle.clone());

    let scopes: Vec<_> = (0..1024)
        .map(|_| {
            let scope = RecomposeScope::new_for_test(handle.clone());
            state.subscribe_scope_for_test(&scope);
            scope
        })
        .collect();

    assert_eq!(state.watcher_count(), scopes.len());
    assert!(
        state.watcher_capacity() >= scopes.len(),
        "watcher capacity should reflect the retained subscriptions"
    );

    drop(scopes);

    assert_eq!(state.watcher_count(), 0);
    assert!(
        state.watcher_capacity() <= 32,
        "watcher capacity should shrink after scope drop cleanup; got {}",
        state.watcher_capacity()
    );
}

#[test]
fn dropping_scope_after_state_release_is_a_noop() {
    let runtime = TestRuntime::new();
    let handle = runtime.handle();
    let scope = RecomposeScope::new_for_test(handle.clone());
    let state = OwnedMutableState::with_runtime(0i32, handle);

    state.handle().subscribe_scope_for_test(&scope);
    drop(state);
    drop(scope);
}