cranpose-core 0.1.0

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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// Observer callbacks use Arc for shared ownership but may capture non-Send types.
// This is safe because callbacks are always invoked on the UI thread where they were created.
#![allow(clippy::arc_with_non_send_sync)]
// Complex types are inherent to the observer pattern with nested callbacks and state tracking
#![allow(clippy::type_complexity)]

use crate::collections::map::HashSet;
use crate::snapshot_v2::{register_apply_observer, ReadObserver, StateObjectId};
use crate::state::StateObject;
use std::any::Any;
use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};
use std::sync::Arc;

/// Executes a callback once changes are delivered.
type Executor = dyn Fn(Box<dyn FnOnce() + 'static>) + 'static;

/// Observer that records state object reads performed inside a given scope and
/// notifies the caller when any of the observed objects change.
///
/// This is a pragmatic Rust translation of Jetpack Compose's
/// `SnapshotStateObserver`. The implementation focuses on the core behaviour
/// needed by the Cranpose runtime:
/// - Tracking state object reads per logical scope.
/// - Reacting to snapshot apply notifications.
/// - Scheduling invalidation callbacks via the supplied executor.
///
/// Advanced features from the Kotlin version (derived state tracking, change
/// coalescing, queue minimisation) are deferred
#[derive(Clone)]
pub struct SnapshotStateObserver {
    inner: Rc<SnapshotStateObserverInner>,
}

impl SnapshotStateObserver {
    /// Create a new observer that schedules callbacks using `on_changed_executor`.
    pub fn new(on_changed_executor: impl Fn(Box<dyn FnOnce() + 'static>) + 'static) -> Self {
        let inner = Rc::new(SnapshotStateObserverInner::new(on_changed_executor));
        inner.set_self(Rc::downgrade(&inner));
        Self { inner }
    }

    /// Observe state object reads performed while executing `block`.
    ///
    /// Subsequent calls to `observe_reads` replace any previously recorded
    /// observations for the provided `scope`. When one of the observed objects
    /// mutates, `on_value_changed_for_scope` will be invoked on the executor.
    pub fn observe_reads<T, R>(
        &self,
        scope: T,
        on_value_changed_for_scope: impl Fn(&T) + 'static,
        block: impl FnOnce() -> R,
    ) -> R
    where
        T: Any + Clone + PartialEq + 'static,
    {
        self.inner
            .observe_reads(scope, on_value_changed_for_scope, block)
    }

    /// Notify the observer that a new composition frame is starting.
    pub fn begin_frame(&self) {
        self.inner.begin_frame();
    }

    /// Temporarily pause read observation while executing `block`.
    pub fn with_no_observations<R>(&self, block: impl FnOnce() -> R) -> R {
        self.inner.with_no_observations(block)
    }

    /// Remove any recorded reads for `scope`.
    pub fn clear<T>(&self, scope: &T)
    where
        T: Any + PartialEq + 'static,
    {
        self.inner.clear(scope);
    }

    /// Remove recorded reads for scopes that satisfy `predicate`.
    pub fn clear_if(&self, predicate: impl Fn(&dyn Any) -> bool) {
        self.inner.clear_if(predicate);
    }

    /// Remove all recorded observations.
    pub fn clear_all(&self) {
        self.inner.clear_all();
    }

    /// Begin listening for snapshot apply notifications.
    pub fn start(&self) {
        let weak = Rc::downgrade(&self.inner);
        self.inner.start(weak);
    }

    /// Stop listening for snapshot apply notifications.
    pub fn stop(&self) {
        self.inner.stop();
    }

    /// Test-only helper to simulate snapshot changes.
    #[cfg(test)]
    pub fn notify_changes(&self, modified: &[Arc<dyn StateObject>]) {
        self.inner.handle_apply(modified);
    }
}

struct SnapshotStateObserverInner {
    executor: Rc<Executor>,
    scopes: RefCell<Vec<Rc<RefCell<ScopeEntry>>>>,
    fast_scopes: RefCell<Vec<Option<Rc<RefCell<ScopeEntry>>>>>,
    pause_count: Rc<Cell<usize>>,
    apply_handle: RefCell<Option<crate::snapshot_v2::ObserverHandle>>,
    weak_self: RefCell<Weak<SnapshotStateObserverInner>>,
    frame_version: Cell<u64>,
}

impl SnapshotStateObserverInner {
    fn new(on_changed_executor: impl Fn(Box<dyn FnOnce() + 'static>) + 'static) -> Self {
        Self {
            executor: Rc::new(on_changed_executor),
            scopes: RefCell::new(Vec::new()),
            fast_scopes: RefCell::new(Vec::new()),
            pause_count: Rc::new(Cell::new(0)),
            apply_handle: RefCell::new(None),
            weak_self: RefCell::new(Weak::new()),
            frame_version: Cell::new(0),
        }
    }

    fn set_self(&self, weak: Weak<SnapshotStateObserverInner>) {
        self.weak_self.replace(weak);
    }

    fn begin_frame(&self) {
        let next = self.frame_version.get().wrapping_add(1);
        self.frame_version.set(next);
    }

    fn observe_reads<T, R>(
        &self,
        scope: T,
        on_value_changed_for_scope: impl Fn(&T) + 'static,
        block: impl FnOnce() -> R,
    ) -> R
    where
        T: Any + Clone + PartialEq + 'static,
    {
        let frame_version = self.frame_version.get();
        let has_frame_version = frame_version != 0;

        let on_changed: Rc<dyn Fn(&dyn Any)> = {
            let callback = Rc::new(on_value_changed_for_scope);
            Rc::new(move |scope_any: &dyn Any| {
                if let Some(typed) = scope_any.downcast_ref::<T>() {
                    callback(typed);
                }
            })
        };

        let entry = self.get_scope_entry(scope.clone(), on_changed.clone());

        let pause_count = self.pause_count.clone();

        let read_observer: ReadObserver = {
            let mut entry_mut = entry.borrow_mut();
            entry_mut.update(scope, on_changed);

            let already_observed =
                has_frame_version && entry_mut.last_seen_version == frame_version;
            if already_observed || entry_mut.is_stateless {
                drop(entry_mut);
                return block();
            }

            entry_mut.observed.clear();
            entry_mut.last_seen_version = if has_frame_version {
                frame_version
            } else {
                u64::MAX
            };
            entry_mut.is_stateless = false;

            if let Some(observer) = entry_mut.read_observer.clone() {
                observer
            } else {
                let entry_for_observer = entry.clone();
                let pause_count = pause_count.clone();

                let observer: ReadObserver = Arc::new(move |state| {
                    if pause_count.get() > 0 {
                        return;
                    }
                    let mut entry_ref = entry_for_observer.borrow_mut();
                    let id = state.object_id().as_usize();
                    entry_ref.observed.insert(id);
                    entry_ref.is_stateless = false;
                });

                entry_mut.read_observer = Some(observer.clone());
                observer
            }
        };

        let result = self.run_with_read_observer(read_observer, block);

        {
            let mut entry_mut = entry.borrow_mut();
            if entry_mut.observed.is_empty() {
                entry_mut.is_stateless = true;
            }
        }

        result
    }

    fn with_no_observations<R>(&self, block: impl FnOnce() -> R) -> R {
        self.pause_count.set(self.pause_count.get() + 1);
        let result = block();
        self.pause_count
            .set(self.pause_count.get().saturating_sub(1));
        result
    }

    fn clear<T>(&self, scope: &T)
    where
        T: Any + PartialEq + 'static,
    {
        // Clear from fast_scopes if it's a RecomposeScope
        if let Some(rc_scope) = (scope as &dyn Any).downcast_ref::<RecomposeScope>() {
            let id = rc_scope.id();
            let mut fast = self.fast_scopes.borrow_mut();
            if id < fast.len() {
                fast[id] = None;
            }
        }

        // Clear from scopes
        self.scopes
            .borrow_mut()
            .retain(|entry| !entry.borrow().matches_scope(scope));
    }

    fn clear_if(&self, predicate: impl Fn(&dyn Any) -> bool) {
        // Clear from fast_scopes for any RecomposeScope entries that match predicate
        let mut fast = self.fast_scopes.borrow_mut();
        for slot in fast.iter_mut() {
            if let Some(entry) = slot {
                let should_clear = {
                    let entry_ref = entry.borrow();
                    predicate(entry_ref.scope())
                };
                if should_clear {
                    *slot = None;
                }
            }
        }
        drop(fast);

        // Clear from scopes
        self.scopes.borrow_mut().retain(|entry| {
            let entry_ref = entry.borrow();
            !predicate(entry_ref.scope())
        });
    }

    fn clear_all(&self) {
        self.fast_scopes.borrow_mut().clear();
        self.scopes.borrow_mut().clear();
    }

    // Arc-wrapped closure captures Weak which may not be Send/Sync. This is safe because
    // the observer callback is only invoked on the UI thread where it was registered.
    #[allow(clippy::arc_with_non_send_sync)]
    fn start(&self, weak_self: Weak<SnapshotStateObserverInner>) {
        if self.apply_handle.borrow().is_some() {
            return;
        }

        let handle = register_apply_observer(Arc::new(move |modified, _snapshot_id| {
            if let Some(inner) = weak_self.upgrade() {
                inner.handle_apply(modified);
            }
        }));
        self.apply_handle.replace(Some(handle));
    }

    fn stop(&self) {
        if let Some(handle) = self.apply_handle.borrow_mut().take() {
            drop(handle);
        }
    }

    fn get_scope_entry(
        &self,
        scope: impl Any + Clone + PartialEq + 'static,
        on_changed: Rc<dyn Fn(&dyn Any)>,
    ) -> Rc<RefCell<ScopeEntry>> {
        // ---------- FAST PATH: real compose scope ----------
        if let Some(rc_scope) = (&scope as &dyn Any).downcast_ref::<RecomposeScope>() {
            let id: usize = rc_scope.id(); // or `.0` or similar

            let mut fast = self.fast_scopes.borrow_mut();

            if id >= fast.len() {
                fast.resize_with(id + 1, || None);
            }

            if let Some(existing) = &fast[id] {
                return existing.clone();
            }

            let entry = Rc::new(RefCell::new(ScopeEntry::new(scope, on_changed)));
            fast[id] = Some(entry.clone());
            // CRITICAL: Also add to scopes Vec so handle_apply and clear* methods work correctly
            drop(fast);
            self.scopes.borrow_mut().push(entry.clone());
            return entry;
        }

        // ---------- SLOW / GENERIC PATH ----------
        let mut scopes = self.scopes.borrow_mut();

        if let Some(existing) = scopes
            .iter()
            .find(|entry| entry.borrow().matches_scope(&scope))
        {
            return existing.clone();
        }

        let entry = Rc::new(RefCell::new(ScopeEntry::new(scope, on_changed)));
        scopes.push(entry.clone());
        entry
    }

    fn run_with_read_observer<R>(
        &self,
        read_observer: ReadObserver,
        block: impl FnOnce() -> R,
    ) -> R {
        // Kotlin uses Snapshot.observeInternal which creates a TransparentObserverMutableSnapshot,
        // not a readonly snapshot. This allows writes to happen during observation (composition).
        use crate::snapshot_v2::take_transparent_observer_mutable_snapshot;

        // Create a transparent mutable snapshot (not readonly!) for observation
        // This matches Kotlin's Snapshot.observeInternal behavior
        let snapshot = take_transparent_observer_mutable_snapshot(Some(read_observer), None);
        let result = snapshot.enter(block);
        snapshot.dispose();
        result
    }

    fn handle_apply(&self, modified: &[Arc<dyn StateObject>]) {
        if modified.is_empty() {
            return;
        }

        let mut modified_ids: SmallVec<[usize; MAX_OBSERVED_STATES]> = SmallVec::new();
        for state in modified {
            modified_ids.push(state.object_id().as_usize());
        }

        let scopes = self.scopes.borrow();
        let mut to_notify: Vec<Rc<RefCell<ScopeEntry>>> = Vec::new();
        let mut seen: HashSet<usize> = HashSet::default();

        for entry in scopes.iter() {
            let entry_ref = entry.borrow();
            if entry_ref
                .observed
                .iter()
                .any(|id| modified_ids.contains(id))
            {
                let ptr = Rc::as_ptr(entry) as usize;
                if seen.insert(ptr) {
                    to_notify.push(entry.clone());
                }
            }
        }
        drop(scopes);

        {
            let fast_scopes = self.fast_scopes.borrow();
            for entry in fast_scopes.iter().flatten() {
                let entry_ref = entry.borrow();
                if entry_ref
                    .observed
                    .iter()
                    .any(|id| modified_ids.contains(id))
                {
                    let ptr = Rc::as_ptr(entry) as usize;
                    if seen.insert(ptr) {
                        to_notify.push(entry.clone());
                    }
                }
            }
        }

        if to_notify.is_empty() {
            return;
        }

        for entry in to_notify {
            let executor = self.executor.clone();
            executor(Box::new(move || {
                if let Ok(entry) = entry.try_borrow() {
                    entry.notify();
                }
            }));
        }
    }
}

use cranpose_core::RecomposeScope;
use smallvec::SmallVec;

enum ObservedIds {
    Small(SmallVec<[StateObjectId; MAX_OBSERVED_STATES]>),
    Large(HashSet<StateObjectId>),
}

impl ObservedIds {
    fn new() -> Self {
        ObservedIds::Small(SmallVec::new())
    }

    fn insert(&mut self, id: StateObjectId) {
        match self {
            ObservedIds::Small(small) => {
                if small.contains(&id) {
                    return;
                }
                if small.len() < MAX_OBSERVED_STATES {
                    small.push(id);
                } else {
                    let mut large =
                        HashSet::with_capacity_and_hasher(small.len() + 1, Default::default());
                    for existing in small.iter() {
                        large.insert(*existing);
                    }
                    large.insert(id);
                    *self = ObservedIds::Large(large);
                }
            }
            ObservedIds::Large(large) => {
                large.insert(id);
            }
        }
    }

    fn is_empty(&self) -> bool {
        match self {
            ObservedIds::Small(small) => small.is_empty(),
            ObservedIds::Large(large) => large.is_empty(),
        }
    }

    fn clear(&mut self) {
        match self {
            ObservedIds::Small(small) => small.clear(),
            ObservedIds::Large(large) => large.clear(),
        }
    }

    fn iter(&self) -> Box<dyn Iterator<Item = &StateObjectId> + '_> {
        match self {
            ObservedIds::Small(small) => Box::new(small.iter()),
            ObservedIds::Large(large) => Box::new(large.iter()),
        }
    }
}

const MAX_OBSERVED_STATES: usize = 8;
struct ScopeEntry {
    scope: Box<dyn Any>,
    on_changed: Rc<dyn Fn(&dyn Any)>,
    observed: ObservedIds,
    read_observer: Option<ReadObserver>,
    is_stateless: bool,
    last_seen_version: u64,
}

impl ScopeEntry {
    fn new<T>(scope: T, on_changed: Rc<dyn Fn(&dyn Any)>) -> Self
    where
        T: Any + 'static,
    {
        Self {
            scope: Box::new(scope),
            on_changed,
            observed: ObservedIds::new(),
            read_observer: None,
            is_stateless: false,
            last_seen_version: u64::MAX,
        }
    }

    fn update<T>(&mut self, new_scope: T, on_changed: Rc<dyn Fn(&dyn Any)>)
    where
        T: Any + 'static,
    {
        self.scope = Box::new(new_scope);
        self.on_changed = on_changed;
    }

    fn matches_scope<T>(&self, scope: &T) -> bool
    where
        T: Any + PartialEq + 'static,
    {
        self.scope
            .downcast_ref::<T>()
            .map(|stored| stored == scope)
            .unwrap_or(false)
    }

    fn scope(&self) -> &dyn Any {
        &*self.scope
    }

    fn notify(&self) {
        (self.on_changed)(self.scope());
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::snapshot_v2::take_mutable_snapshot;
    use crate::snapshot_v2::{reset_runtime_for_tests, TestRuntimeGuard};
    use crate::state::{NeverEqual, SnapshotMutableState};
    use std::cell::Cell;

    fn reset_runtime() -> TestRuntimeGuard {
        reset_runtime_for_tests()
    }

    #[derive(Clone, PartialEq)]
    struct TestScope(&'static str);

    #[test]
    fn notifies_scope_when_state_changes() {
        let _guard = reset_runtime();

        let state = SnapshotMutableState::new_in_arc(0, Arc::new(NeverEqual));
        let triggered = Rc::new(Cell::new(0));
        let observer_trigger = triggered.clone();

        let observer = SnapshotStateObserver::new(|callback| callback());
        observer.start();

        let scope = TestScope("scope");
        observer.observe_reads(
            scope.clone(),
            move |_| {
                observer_trigger.set(observer_trigger.get() + 1);
            },
            || {
                let _ = state.get();
            },
        );

        let snapshot = take_mutable_snapshot(None, None);
        snapshot.enter(|| {
            state.set(1);
        });
        snapshot.apply().check();

        assert_eq!(triggered.get(), 1);
        observer.stop();
    }

    #[test]
    fn clear_removes_scope_observation() {
        let _guard = reset_runtime();

        let state = SnapshotMutableState::new_in_arc(0, Arc::new(NeverEqual));
        let triggered = Rc::new(Cell::new(0));
        let observer_trigger = triggered.clone();

        let observer = SnapshotStateObserver::new(|callback| callback());
        observer.start();

        let scope = TestScope("scope");
        observer.observe_reads(
            scope.clone(),
            move |_| {
                observer_trigger.set(observer_trigger.get() + 1);
            },
            || {
                let _ = state.get();
            },
        );

        observer.clear(&scope);

        let snapshot = take_mutable_snapshot(None, None);
        snapshot.enter(|| {
            state.set(1);
        });
        snapshot.apply().check();

        assert_eq!(triggered.get(), 0);
        observer.stop();
    }

    #[test]
    fn with_no_observations_skips_reads() {
        let _guard = reset_runtime();

        let state = SnapshotMutableState::new_in_arc(0, Arc::new(NeverEqual));
        let triggered = Rc::new(Cell::new(0));
        let observer_trigger = triggered.clone();

        let observer = SnapshotStateObserver::new(|callback| callback());
        observer.start();

        let scope = TestScope("scope");
        observer.observe_reads(
            scope.clone(),
            move |_| {
                observer_trigger.set(observer_trigger.get() + 1);
            },
            || {
                observer.with_no_observations(|| {
                    let _ = state.get();
                });
            },
        );

        let snapshot = take_mutable_snapshot(None, None);
        snapshot.enter(|| {
            state.set(1);
        });
        snapshot.apply().check();

        assert_eq!(triggered.get(), 0);
        observer.stop();
    }
}