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
use std::collections::*;
use std::any::*;
use std::marker::*;
use std::cell::*;
use std::rc::*;
use either::{Either, Either::*};

use crate::backend::browser;
use crate::backend::browser::{NodeApi, ElementApi, CallbackSettings, QueueCallback, VoidCallback};
use crate::reactive_sys::*;
use crate::view_sys::dsl::{self as dsl, Dsl, View};
use crate::view_sys::shared::*;
use crate::view_sys::dom::*;


///////////////////////////////////////////////////////////////////////////////
// THUNK
///////////////////////////////////////////////////////////////////////////////

#[derive(Debug)]
pub(crate) struct DomThunk<Msg>(Box<RefCell<ThunkState<Msg>>>);

// INTERNAL
#[derive(Debug)]
enum ThunkState<Msg> {
    Borrowed,
    View(View<Msg>),
    Dom(Dom<Msg>),
}

pub(crate) struct EvalDomThunk<New, Update> {
    pub new: New,
    pub update: Update,
}

impl<Msg> DomThunk<Msg> {
    pub(crate) fn into_inner(self) -> Either<View<Msg>, Dom<Msg>> {
        match self.0.into_inner() {
            ThunkState::View(view) => Left(view),
            ThunkState::Dom(dom) => Right(dom),
            ThunkState::Borrowed => {panic!()}
        }
    }
    pub(crate) fn new(x: Dom<Msg>) -> Self {
        DomThunk(Box::new(RefCell::new(ThunkState::Dom(x))))
    }
    pub(crate) fn from_view(x: View<Msg>) -> Self {
        DomThunk(Box::new(RefCell::new(ThunkState::View(x))))
    }
    pub(crate) fn inspect(&self, f: &mut FnMut(&Dom<Msg>)) {
        // let mut f = Box::new(f);
        let inner: &ThunkState<Msg> = &self.0.borrow();
        match inner {
            ThunkState::Dom(dom) => {f(dom)}
            ThunkState::View(_) => {}
            ThunkState::Borrowed => {panic!()}
        }
    }
    pub(crate) fn eval(&self, mut f: EvalDomThunk<impl FnMut(View<Msg>)->Dom<Msg>, impl FnMut(&mut Dom<Msg>)>) {
        match self.0.replace(ThunkState::Borrowed) {
            ThunkState::View(view) => {
                self.0.replace(ThunkState::Dom((f.new)(view)));
            }
            ThunkState::Dom(mut x) => {
                (f.update)(&mut x);
                self.0.replace(ThunkState::Dom(x));
            }
            ThunkState::Borrowed => panic!()
        }
    }
}