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
#[macro_use]
pub mod effect;
pub mod spec;
pub mod instances;
pub mod shell;

use std::collections::*;
use std::rc::*;
use std::any::*;
use std::cell::*;
use wasm_bindgen::prelude::*;

use crate::backend::browser;
use crate::backend::browser::{NodeApi, ElementApi, CallbackSettings, QueueCallback, VoidCallback};
use crate::view_sys::dsl::View;
use crate::program_sys::spec::*;
use crate::program_sys::instances::*;
use crate::view_sys::shared::*;
use crate::program_sys::shell::*;
pub use crate::program_sys::effect::nav::{Url};



///////////////////////////////////////////////////////////////////////////////
// ROOT-PROCESS TICK - EXTERNAL API
///////////////////////////////////////////////////////////////////////////////

#[wasm_bindgen]
pub fn on_request_animation_frame() {
    ROOT_PROCESS.with(|cell| {
        let inner: Option<Box<ProgramImpl>> = cell.replace(None);
        if let Some(mut process) = inner {
            process.tick();
            assert!(cell.replace(Some(process)).is_none());
        }
    });
}

///////////////////////////////////////////////////////////////////////////////
// NAVIGATION
///////////////////////////////////////////////////////////////////////////////

thread_local! {
    pub(crate) static CURRENT_URL: RefCell<Option<Url>> = {
        RefCell::new(None)
    };
}

fn init_url_callback() {
    let callback_settings = browser::CallbackSettings{
        prevent_default: true,
        ..browser::CallbackSettings::default()
    };
    let callback = browser::VoidCallback::new_with_fn_unset(callback_settings, move |_| {
        CURRENT_URL.with(|cell| {
            let new_url: Url = Url::get_current(&browser::window());
            cell.replace(Some(new_url));
        });
    });
    browser::window().add_event_listener("popstate", &callback);
    std::mem::forget(callback);
}

///////////////////////////////////////////////////////////////////////////////
// ROOT-PROCESS TICK - INTERNAL
///////////////////////////////////////////////////////////////////////////////

thread_local! {
    pub(crate) static ROOT_PROCESS: RefCell<Option<Box<ProgramImpl>>> = {
        RefCell::new(None)
    };
}

pub(crate) trait ProgramImpl {
    fn tick(&mut self);
}


impl ProgramImpl for Program {
    fn tick(&mut self) {
        let url_state: Option<Url> = CURRENT_URL.with(|url| {
            url.borrow().clone()
        });
        let url_state: Url = if let Some(url_state) = url_state {
            url_state
        } else {
            self.url.clone()
        };
        let url_unchanged = &self.url == &url_state;
        let mut messages: Vec<SystemMessage> = GLOABL_MESSAGE_REGISTRY.with(|reg| {
            reg.borrow_mut().drain(..).collect::<Vec<SystemMessage>>()
        });
        if !url_unchanged {
            CURRENT_URL.with(|cell| {
                let new_url = Url::get_current(&browser::window());
                cell.replace(Some(new_url));
            });
            messages.push({
                let value: Rc<UrlChanged> = Rc::new(UrlChanged(url_state.clone()));
                SystemMessage::Public {
                    from_name: String::from(""),
                    from_tid: TypeId::of::<Program>(),
                    value,
                }
            });
            self.url = url_state;
        };
        self.process.0.tick(&messages);
    }
}

///////////////////////////////////////////////////////////////////////////////
// PROGRAM
///////////////////////////////////////////////////////////////////////////////

pub struct Program {
    url: Url,
    process: SubProcess,
}

impl Program {
    pub fn run_basic<Model, Msg>(
        init: impl Fn(Option<Model>)-> Model + 'static,
        update: impl Fn(&mut Model, Msg) + 'static,
        view: impl Fn(&Model) -> View<Msg> + 'static,
    ) where
        Model: 'static,
        Msg: 'static,
    {
        Program::run_spec(SimpleApp {
            init: Rc::new(init),
            update: Rc::new(update),
            view: Rc::new(view),
        })
    }
    pub fn run_spec(spec: impl Spec + 'static) {
        let window = browser::window();
        let root_component = Component {
            name: String::from("Root Component"),
            spec,
        };
        let process = root_component.build_impl(true);
        browser::window().document.body.append_child(&process.dom_ref());
        let process = SubProcess(Box::new(process));
        let program: Program = Program{
            url: Url::get_current(&window),
            process,
        };
        ROOT_PROCESS.with(move |cell| {
            let old = cell.replace(Some(Box::new(program)));
            assert!(old.is_none());
        });
    }
}





///////////////////////////////////////////////////////////////////////////////
// IMMEDIATE-MODE-APP
///////////////////////////////////////////////////////////////////////////////

pub(crate) struct SimpleApp<Model, Msg> {
    init: Rc<Fn(Option<Model>)-> Model>,
    update: Rc<Fn(&mut Model, Msg)>,
    view: Rc<Fn(&Model) -> View<Msg>>,
}

impl<Model, Msg> Spec for SimpleApp<Model, Msg> {
    type Msg = Msg;
    type Model = Model;
    
    fn init(&self, sh: &Shell<Self>) -> Init<Self> {
        let inner = (self.init)(None);
        Init {
            model: inner,
            subs: Subscriptions::default(),
        }
    }
    fn update(&self, model: &mut Self::Model, msg: Self::Msg, sys: &mut Shell<Self>) {
        (self.update)(model, msg);
    }
    fn view(&self, model: &Self::Model) -> View<Self::Msg> {
        (self.view)(model)
    }
}

impl<Model, Msg> Clone for SimpleApp<Model, Msg> {
    fn clone(&self) -> Self {
        let init = self.init.clone();
        let update = self.update.clone();
        let view = self.view.clone();
        SimpleApp{init,update,view}
    }
}