actuate 0.21.0

A reactive user-interface framework
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
use crate::{
    composer::{ComposePtr, Node, Runtime},
    data::Data,
    use_context, use_ref, Scope, ScopeData, ScopeState,
};
use alloc::borrow::Cow;
use alloc::rc::Rc;
use core::{
    any::TypeId,
    cell::{Cell, RefCell, UnsafeCell},
    fmt, mem,
};
use slotmap::{DefaultKey, SlotMap};

mod catch;
pub use self::catch::{catch, Catch};

mod dyn_compose;
pub use self::dyn_compose::{dyn_compose, DynCompose};

mod from_fn;
pub use self::from_fn::{from_fn, FromFn};

mod from_iter;
pub use self::from_iter::{from_iter, FromIter};

mod memo;
pub use self::memo::{memo, Memo};

/// A composable function.
///
/// For a dynamically-typed composable, see [`DynCompose`].
///
/// Composables are the building blocks of reactivity in Actuate.
/// A composable is essentially a function that is re-run whenever its state (or its parent state) is changed.
/// Composables may return one or more children, that run after their parent.
///
/// When a composable is re-run, we call that "recomposition".
/// For example, on the initial composition, hooks may initialize their state.
/// Then on recomposition, hooks update their state from the last set value.
///
/// Triggering a state update will recompose each parent, and then each child,
/// until either a [`Memo`] is reached or the composition is complete.
///
/// [`Memo`] is special in that it will only recompose in two cases:
/// 1. It's provided dependencies have changed (see [`memo()`] for more)
/// 2. Its own state has changed, which will then trigger the above parent-to-child process for its children.
#[must_use = "Composables do nothing unless composed or returned from other composables."]
pub trait Compose: Data {
    /// Compose this function.
    fn compose(cx: Scope<Self>) -> impl Compose;

    #[doc(hidden)]
    fn name() -> Option<Cow<'static, str>> {
        let name = core::any::type_name::<Self>();
        Some(
            name.split('<')
                .next()
                .unwrap_or(name)
                .split("::")
                .last()
                .unwrap_or(name)
                .into(),
        )
    }
}

impl Compose for () {
    fn compose(cx: Scope<Self>) -> impl Compose {
        let _ = cx;
    }
}

impl<C: Compose> Compose for Option<C> {
    fn compose(cx: Scope<Self>) -> impl Compose {
        let child_key = use_ref(&cx, || Cell::new(None));

        let rt = Runtime::current();
        let mut nodes = rt.nodes.borrow_mut();

        if let Some(content) = &*cx.me() {
            if let Some(key) = child_key.get() {
                let last = nodes.get_mut(key).unwrap();

                let ptr = content as *const dyn AnyCompose;
                let ptr: *const dyn AnyCompose = unsafe { mem::transmute(ptr) };

                *last.compose.borrow_mut() = ComposePtr::Ptr(ptr);

                drop(nodes);

                rt.queue(key);
            } else {
                let ptr: *const dyn AnyCompose =
                    unsafe { mem::transmute(content as *const dyn AnyCompose) };
                let key = nodes.insert(Rc::new(Node {
                    compose: RefCell::new(crate::composer::ComposePtr::Ptr(ptr)),
                    scope: ScopeData::default(),
                    parent: Some(rt.current_key.get()),
                    children: RefCell::new(Vec::new()),
                    child_idx: 0,
                }));
                child_key.set(Some(key));

                nodes
                    .get(rt.current_key.get())
                    .unwrap()
                    .children
                    .borrow_mut()
                    .push(key);

                let child_state = &nodes[key].scope;

                *child_state.contexts.borrow_mut() = cx.contexts.borrow().clone();
                child_state
                    .contexts
                    .borrow_mut()
                    .values
                    .extend(cx.child_contexts.borrow().values.clone());

                drop(nodes);

                rt.queue(key);
            }
        } else if let Some(key) = child_key.get() {
            child_key.set(None);

            drop_node(&mut nodes, key);
        }
    }
}

// TODO replace with non-recursive algorithm.
fn drop_node(nodes: &mut SlotMap<DefaultKey, Rc<Node>>, key: DefaultKey) {
    let node = nodes[key].clone();
    if let Some(parent) = node.parent {
        let parent = nodes.get_mut(parent).unwrap();
        parent.children.borrow_mut().retain(|&x| x != key);
    }

    let children = node.children.borrow().clone();
    for key in children {
        drop_node(nodes, key)
    }

    nodes.remove(key);
}

/// Composable error.
///
/// This can be handled by a parent composable with [`Catch`].
#[derive(Data, thiserror::Error)]
#[actuate(path = "crate")]
pub struct Error {
    make_error: Box<dyn Fn() -> Box<dyn core::error::Error>>,
}

impl Error {
    /// Create a new composable error.
    pub fn new(error: impl core::error::Error + Clone + 'static) -> Self {
        Self {
            make_error: Box::new(move || Box::new(error.clone())),
        }
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (self.make_error)().fmt(f)
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        (self.make_error)().fmt(f)
    }
}

impl<C: Compose> Compose for Result<C, Error> {
    fn compose(cx: Scope<Self>) -> impl Compose {
        let catch_cx = use_context::<CatchContext>(&cx).unwrap();

        let child_key = use_ref(&cx, || Cell::new(None));

        let rt = Runtime::current();

        match &*cx.me() {
            Ok(content) => {
                if let Some(key) = child_key.get() {
                    let mut nodes = rt.nodes.borrow_mut();
                    let last = nodes.get_mut(key).unwrap();

                    let ptr = content as *const dyn AnyCompose;
                    let ptr: *const dyn AnyCompose = unsafe { mem::transmute(ptr) };

                    *last.compose.borrow_mut() = ComposePtr::Ptr(ptr);

                    drop(nodes);

                    rt.queue(key);
                } else {
                    let mut nodes = rt.nodes.borrow_mut();
                    let ptr: *const dyn AnyCompose =
                        unsafe { mem::transmute(content as *const dyn AnyCompose) };
                    let key = nodes.insert(Rc::new(Node {
                        compose: RefCell::new(crate::composer::ComposePtr::Ptr(ptr)),
                        scope: ScopeData::default(),
                        parent: Some(rt.current_key.get()),
                        children: RefCell::new(Vec::new()),
                        child_idx: 0,
                    }));
                    child_key.set(Some(key));

                    nodes
                        .get(rt.current_key.get())
                        .unwrap()
                        .children
                        .borrow_mut()
                        .push(key);

                    let child_state = &nodes[key].scope;

                    *child_state.contexts.borrow_mut() = cx.contexts.borrow().clone();
                    child_state
                        .contexts
                        .borrow_mut()
                        .values
                        .extend(cx.child_contexts.borrow().values.clone());

                    drop(nodes);

                    rt.queue(key);
                }
            }
            Err(error) => {
                let mut nodes = rt.nodes.borrow_mut();

                if let Some(key) = child_key.get() {
                    drop_node(&mut nodes, key);
                }

                (catch_cx.f)((error.make_error)())
            }
        }
    }
}

pub(crate) struct CatchContext {
    f: Rc<dyn Fn(Box<dyn core::error::Error>)>,
}

impl CatchContext {
    pub(crate) fn new(f: impl Fn(Box<dyn core::error::Error>) + 'static) -> Self {
        Self { f: Rc::new(f) }
    }
}

macro_rules! impl_tuples {
    ($($t:tt : $idx:tt),*) => {
        unsafe impl<$($t: Data),*> Data for ($($t,)*) {}

        impl<$($t: Compose),*> Compose for ($($t,)*) {
            fn compose(cx: Scope<Self>) -> impl Compose {
                $({
                    let ptr: *const dyn AnyCompose = unsafe { mem::transmute(&cx.me().$idx as *const dyn AnyCompose) };
                    let (key, _) = use_node(&cx, ComposePtr::Ptr(ptr), $idx);

                    let rt = Runtime::current();
                    rt.queue(key)
                })*
            }

            fn name() -> Option<Cow<'static, str>> {
                None
            }
        }
    };
}

impl_tuples!(T1:0);
impl_tuples!(T1:0, T2:1);
impl_tuples!(T1:0, T2:1, T3:2);
impl_tuples!(T1:0, T2:1, T3:2, T4:3);
impl_tuples!(T1:0, T2:1, T3:2, T4:3, T5:4);
impl_tuples!(T1:0, T2:1, T3:2, T4:3, T5:4, T6:5);
impl_tuples!(T1:0, T2:1, T3:2, T4:3, T5:4, T6:5, T7:6);
impl_tuples!(T1:0, T2:1, T3:2, T4:3, T5:4, T6:5, T7:6, T8:7);

impl<C> Compose for Vec<C>
where
    C: Compose,
{
    fn compose(cx: Scope<Self>) -> impl Compose {
        for (idx, item) in cx.me().iter().enumerate() {
            let ptr: *const dyn AnyCompose =
                unsafe { mem::transmute(item as *const dyn AnyCompose) };
            let (key, _) = use_node(&cx, ComposePtr::Ptr(ptr), idx);

            let rt = Runtime::current();
            rt.queue(key);
        }
    }
}

fn use_node(
    cx: ScopeState<'_>,
    compose_ptr: ComposePtr,
    child_idx: usize,
) -> (DefaultKey, &Rc<Node>) {
    let mut compose_ptr_cell = Some(compose_ptr);

    let (key, node) = use_ref(cx, || {
        let rt = Runtime::current();
        let mut nodes = rt.nodes.borrow_mut();

        let key = nodes.insert(Rc::new(Node {
            compose: RefCell::new(compose_ptr_cell.take().unwrap()),
            scope: ScopeData::default(),
            parent: Some(rt.current_key.get()),
            children: RefCell::new(Vec::new()),
            child_idx,
        }));

        nodes
            .get(rt.current_key.get())
            .unwrap()
            .children
            .borrow_mut()
            .push(key);

        let child_state = &nodes[key].scope;
        *child_state.contexts.borrow_mut() = cx.contexts.borrow().clone();
        child_state
            .contexts
            .borrow_mut()
            .values
            .extend(cx.child_contexts.borrow().values.clone());

        (key, nodes[key].clone())
    });

    // Reborrow the pointer to the node's composable.
    if let Some(compose_ptr) = compose_ptr_cell.take() {
        *node.compose.borrow_mut() = compose_ptr;
    }

    (*key, node)
}

pub(crate) trait AnyCompose {
    fn data_id(&self) -> TypeId;

    fn as_ptr_mut(&mut self) -> *mut ();

    unsafe fn reborrow(&mut self, ptr: *mut ());

    /// Safety: The caller must ensure `&self` is valid for the lifetime of `state`.
    unsafe fn any_compose(&self, state: &ScopeData);

    fn name(&self) -> Option<Cow<'static, str>>;
}

impl<C> AnyCompose for C
where
    C: Compose + Data,
{
    fn data_id(&self) -> TypeId {
        typeid::of::<C>()
    }

    fn as_ptr_mut(&mut self) -> *mut () {
        self as *mut Self as *mut ()
    }

    unsafe fn reborrow(&mut self, ptr: *mut ()) {
        core::ptr::swap(self, ptr as _);
    }

    unsafe fn any_compose(&self, state: &ScopeData) {
        // Reset the hook index.
        state.hook_idx.set(0);

        // Increment the scope's current generation.
        state.generation.set(state.generation.get() + 1);

        // Transmute the lifetime of `&Self`, `&ScopeData`, and the `Scope` containing both to the same`'a`.
        // Safety: `self` and `state` are guranteed to have the same lifetime..
        let state: ScopeState = unsafe { mem::transmute(state) };
        let cx: Scope<'_, C> = Scope { me: self, state };
        let cx: Scope<'_, C> = unsafe { mem::transmute(cx) };

        // Cell for the Box used to re-allocate this composable.
        let cell: &UnsafeCell<Option<Box<dyn AnyCompose>>> = use_ref(&cx, || UnsafeCell::new(None));
        // Safety: This cell is only accessed by this composable.
        let cell = unsafe { &mut *cell.get() };

        let child_key_cell = use_ref(&cx, || Cell::new(None));

        let rt = Runtime::current();

        if cell.is_none() {
            #[cfg(feature = "tracing")]
            if let Some(name) = C::name() {
                tracing::trace!("Compose: {}", name);
            }

            let child = C::compose(cx);

            if child.data_id() == typeid::of::<()>() {
                return;
            }

            let child: Box<dyn AnyCompose> = Box::new(child);
            let mut child: Box<dyn AnyCompose> = unsafe { mem::transmute(child) };

            let mut nodes = rt.nodes.borrow_mut();

            unsafe {
                if let Some(key) = child_key_cell.get() {
                    let last = nodes.get_mut(key).unwrap();
                    child.reborrow(last.compose.borrow_mut().as_ptr_mut());
                } else {
                    let child_key = nodes.insert(Rc::new(Node {
                        compose: RefCell::new(crate::composer::ComposePtr::Boxed(child)),
                        scope: ScopeData::default(),
                        parent: Some(rt.current_key.get()),
                        children: RefCell::new(Vec::new()),
                        child_idx: 0,
                    }));
                    child_key_cell.set(Some(child_key));

                    nodes
                        .get(rt.current_key.get())
                        .unwrap()
                        .children
                        .borrow_mut()
                        .push(child_key);

                    let child_state = &nodes[child_key].scope;

                    *child_state.contexts.borrow_mut() = cx.contexts.borrow().clone();
                    child_state
                        .contexts
                        .borrow_mut()
                        .values
                        .extend(cx.child_contexts.borrow().values.clone());
                }
            }
        }

        if let Some(key) = child_key_cell.get() {
            rt.queue(key)
        }
    }

    fn name(&self) -> Option<Cow<'static, str>> {
        C::name()
    }
}