spair 0.0.9

A framework for single-page application 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
use std::cell::{Cell, RefCell};
use std::collections::VecDeque;
use std::rc::{Rc, Weak};
use wasm_bindgen::UnwrapThrowExt;

use crate::dom::{Element, ElementStatus};

mod child_component;

pub use child_component::*;

struct UpdateQueue {
    // A hack to avoid circular borrowing and mutable borrowing between
    // child and parent component. The first component that run when
    // `will_be_executed` == false will have to promise that it will
    // execute UPDATE_QUEUE.
    will_be_executed: Cell<bool>,
    queue: RefCell<VecDeque<Box<dyn FnOnce()>>>,
}

thread_local! {
    static UPDATE_QUEUE: UpdateQueue = UpdateQueue {
        will_be_executed: Cell::new(false),
        queue: RefCell::new(VecDeque::new())
    };
}

fn i_have_to_execute_update_queue() -> bool {
    UPDATE_QUEUE.with(|uq| {
        match uq.will_be_executed.get() {
            false => {
                // It means that there is no component promise executing UPDATE_QUEUE
                // The caller must execute it.
                uq.will_be_executed.set(true);
                true
            }
            true => {
                // There already a component promise to execute UPDATE_QUEUE, so the
                // caller just ignore it.
                false
            }
        }
    })
}

pub fn update_queue_will_be_execute() -> bool {
    UPDATE_QUEUE.with(|uq| uq.will_be_executed.get())
}

pub fn update_component(fn_update: impl FnOnce() + 'static) {
    UPDATE_QUEUE.with(|uq| uq.add(Box::new(fn_update)));
}

fn execute_update_queue(promise: bool) {
    if !promise {
        return;
    }
    UPDATE_QUEUE.with(|uq| uq.execute());
}

impl UpdateQueue {
    fn add(&self, f: Box<dyn FnOnce()>) {
        self.queue.borrow_mut().push_back(f);
    }

    fn take(&self) -> Option<Box<dyn FnOnce()>> {
        self.queue.borrow_mut().pop_front()
    }

    fn execute(&self) {
        while let Some(f) = self.take() {
            f();
        }
        self.will_be_executed.set(false);
    }
}

pub trait Component: 'static + Sized {
    type Routes: crate::routing::Routes;

    // This method will be ran once when the component is created.
    fn init(_: &Comp<Self>) {}

    /// This method allow child-components that wants to receive update when the location
    /// changes to register its callback to the router. The root-component (the component that
    /// implements `spair::Application`) does not have to implement this, but must implement
    /// `spair::Application::init_router`. This method will never be called on the root-component.
    fn register_routing_callback(
        _router: &mut <Self::Routes as crate::routing::Routes>::Router,
        _comp: &Comp<Self>,
    ) {
    }

    fn remove_routing_callback(_router: &mut <Self::Routes as crate::routing::Routes>::Router) {}

    fn default_checklist() -> Checklist<Self> {
        Self::default_should_render().into()
    }

    fn default_should_render() -> ShouldRender {
        ShouldRender::Yes
    }

    /// This method will be called before executing an update method
    fn before_update(&mut self) {}

    fn render(&self, element: crate::Element<Self>);
}

#[must_use = "This value must be returned to the framework. Otherwise, it will be lost and the default value will be used"]
pub enum ShouldRender {
    No,
    Yes,
}

impl<C: Component> From<ShouldRender> for Checklist<C> {
    fn from(should_render: ShouldRender) -> Self {
        Checklist {
            should_render,
            commands: Commands(Vec::new()),
        }
    }
}

pub struct RcComp<C: Component>(Rc<RefCell<CompInstance<C>>>);
pub struct Comp<C: Component>(Weak<RefCell<CompInstance<C>>>);

pub struct CompInstance<C> {
    state: Option<C>,
    root_element: Element,
    mount_status: MountStatus,
    events: Vec<Box<dyn crate::events::Listener>>,
}

pub enum MountStatus {
    // A child component that is attached to the DOM.
    Mounted,
    // A child component that is previously attached to the DOM but
    // has been detached.
    Unmounted,
    // The main component always in this status.
    PermanentlyMounted,
}

#[must_use = "This value must be returned to the framework. Otherwise, it will be lost and the default value will be used"]
pub struct Checklist<C: Component> {
    should_render: ShouldRender,
    commands: Commands<C>,
}

pub(crate) struct Commands<C>(Vec<Box<dyn Command<C>>>);

impl<C: Component> Commands<C> {
    fn execute(&mut self, comp: &Comp<C>, state: &mut C) {
        self.0.iter_mut().for_each(|c| c.execute(comp, state));
    }
}

pub trait Command<C: Component> {
    fn execute(&mut self, comp: &Comp<C>, state: &mut C);
}

impl<C: Component> From<()> for Checklist<C> {
    fn from(_: ()) -> Self {
        C::default_checklist()
    }
}

impl<C: Component> Checklist<C> {
    pub(crate) fn into_parts(self) -> (ShouldRender, Commands<C>) {
        (self.should_render, self.commands)
    }

    pub fn should_render() -> Self {
        Self {
            should_render: ShouldRender::Yes,
            commands: Commands(Vec::new()),
        }
    }

    pub fn skip_render() -> Self {
        Self {
            should_render: ShouldRender::No,
            commands: Commands(Vec::new()),
        }
    }

    pub fn set_should_render(&mut self) {
        self.should_render = ShouldRender::Yes;
    }

    pub fn set_skip_render(&mut self) {
        self.should_render = ShouldRender::No;
    }

    pub fn add_option_command(&mut self, cmd: crate::OptionCommand<C>) {
        if let Some(cmd) = cmd.0 {
            self.commands.0.push(cmd);
        }
    }

    pub fn add_command(&mut self, cmd: crate::Command<C>) {
        self.commands.0.push(cmd.0);
    }
}

impl<C: Component> RcComp<C> {
    pub(crate) fn with_ws_root(root: web_sys::Element) -> Self {
        let root_element = Element::from_ws_element(root);
        let mount_status = MountStatus::PermanentlyMounted;

        Self(Rc::new(RefCell::new(CompInstance {
            state: None,
            root_element,
            mount_status,
            events: Vec::new(),
        })))
    }

    pub(crate) fn with_root(root_element: Element) -> Self {
        Self(Rc::new(RefCell::new(CompInstance {
            state: None,
            root_element,
            mount_status: MountStatus::Mounted,
            events: Vec::new(),
        })))
    }
}

impl<C: Component> RcComp<C> {
    pub(crate) fn set_state(&self, state: C) {
        self.0
            .try_borrow_mut()
            .expect_throw("Why unable to mutably borrow comp instance to set state?")
            .state = Some(state);
    }

    pub(crate) fn first_render(&self) {
        let comp = self.comp();

        C::init(&comp);

        let promise = self::i_have_to_execute_update_queue();

        {
            // This borrow_mut must end before executing self::execute_update_queue()
            let mut instance = self
                .0
                .try_borrow_mut()
                .expect_throw("Expect no borrowing at the first render");

            if instance.root_element.is_empty() {
                // In cases that the router not cause any render yet, such as Routes = ()
                instance.render(&comp);
            }
        }

        self::execute_update_queue(promise);
    }

    pub fn comp(&self) -> Comp<C> {
        Comp(Rc::downgrade(&self.0))
    }
}

impl<C: Component> Clone for Comp<C> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl<C: Component> Comp<C> {
    pub fn window_event(&self, listener: Box<dyn crate::events::Listener>) -> &Self {
        self.0
            .upgrade()
            .expect_throw("Comp::window_event: why the component dropped?")
            .try_borrow_mut()
            .expect_throw("Why unable to mutably borrow comp instance to store event?")
            .events
            .push(listener);
        self
    }

    #[cfg(feature = "queue-render")]
    pub(crate) fn upgrade(&self) -> Rc<RefCell<CompInstance<C>>> {
        // Why wrapping this around an RcComp cause a bug the clear the root element empty?
        self.0
            .upgrade()
            .expect_throw("Comp::upgrade: why the component dropped?")
    }

    fn set_mount_status_to_unmounted(&self) {
        if let Some(instance) = self.0.upgrade() {
            if let Ok(mut instance) = instance.try_borrow_mut() {
                instance.mount_status = MountStatus::Unmounted;
            }
        }
    }

    pub(crate) fn execute_callback<A, Cb>(&self, arg: A, callback: Cb)
    where
        Cb: crate::callback::ExecuteCallback<C, A>,
    {
        let promise = self::i_have_to_execute_update_queue();
        {
            let this = self
                .0
                .upgrade()
                .expect_throw("Expect the component instance alive when updating - update()");
            let mut this = match this.try_borrow_mut() {
                Ok(this) => this,
                Err(_) => {
                    callback.queue(arg);
                    return;
                }
            };

            let state = this
                .state
                .as_mut()
                .expect_throw("Mutable reference to state for updating");
            C::before_update(state);
            let (should_render, commands) = callback.execute(state, arg).into_parts();
            this.extra_update(should_render, commands, self);
        }
        self::execute_update_queue(promise);
        #[cfg(feature = "queue-render")]
        crate::queue_render::execute_render_queue();
    }

    pub fn callback_once_mut<Cl, F>(&self, f: F) -> crate::callback::CallbackFnOnce<C, Cl, F>
    where
        Cl: 'static + Into<Checklist<C>>,
        F: 'static + FnOnce(&mut C) -> Cl,
    {
        crate::callback::CallbackFnOnce {
            comp: self.clone(),
            callback: f,
            phantom: std::marker::PhantomData,
        }
    }

    pub fn callback_once_arg_mut<Cl, A, F>(
        &self,
        f: F,
    ) -> crate::callback::CallbackFnOnceArg<C, A, Cl, F>
    where
        Cl: 'static + Into<Checklist<C>>,
        F: 'static + FnOnce(&mut C, A) -> Cl,
    {
        crate::callback::CallbackFnOnceArg {
            comp: self.clone(),
            callback: f,
            _phantom: std::marker::PhantomData,
        }
    }

    fn cb<Cl: 'static>(&self, f: impl Fn(&C) -> Cl + 'static) -> crate::callback::CallbackFn<C, ()>
    where
        Cl: Into<Checklist<C>>,
    {
        crate::callback::CallbackFn {
            comp: self.clone(),
            callback: Rc::new(crate::callback::Cb(f)),
        }
    }

    fn cb_mut<Cl: 'static>(
        &self,
        f: impl Fn(&mut C) -> Cl + 'static,
    ) -> crate::callback::CallbackFn<C, ()>
    where
        Cl: Into<Checklist<C>>,
    {
        crate::callback::CallbackFn {
            comp: self.clone(),
            callback: Rc::new(crate::callback::CbMut(f)),
        }
    }

    fn cb_dropped_arg<Cl: 'static, A>(
        &self,
        f: impl Fn(&C) -> Cl + 'static,
    ) -> crate::callback::CallbackFn<C, A>
    where
        Cl: Into<Checklist<C>>,
    {
        crate::callback::CallbackFn {
            comp: self.clone(),
            callback: Rc::new(crate::callback::CbDroppedArg(f)),
        }
    }

    fn cb_dropped_arg_mut<Cl: 'static, A>(
        &self,
        f: impl Fn(&mut C) -> Cl + 'static,
    ) -> crate::callback::CallbackFn<C, A>
    where
        Cl: Into<Checklist<C>>,
    {
        crate::callback::CallbackFn {
            comp: self.clone(),
            callback: Rc::new(crate::callback::CbDroppedArgMut(f)),
        }
    }

    fn cb_arg_mut<Cl: 'static, A>(
        &self,
        f: impl Fn(&mut C, A) -> Cl + 'static,
    ) -> crate::callback::CallbackFn<C, A>
    where
        Cl: Into<Checklist<C>>,
    {
        crate::callback::CallbackFn {
            comp: self.clone(),
            callback: Rc::new(crate::callback::CbArgMut(f)),
        }
    }

    pub fn callback<Cl: 'static>(&self, f: impl Fn(&C) -> Cl + 'static) -> crate::Callback
    where
        Cl: Into<Checklist<C>>,
    {
        Box::new(self.cb(f))
    }

    pub fn callback_mut<Cl: 'static>(&self, f: impl Fn(&mut C) -> Cl + 'static) -> crate::Callback
    where
        Cl: Into<Checklist<C>>,
    {
        Box::new(self.cb_mut(f))
    }

    pub fn callback_arg_mut<Cl: 'static, A: 'static>(
        &self,
        f: impl Fn(&mut C, A) -> Cl + 'static,
    ) -> crate::CallbackArg<A>
    where
        Cl: Into<Checklist<C>>,
    {
        Box::new(self.cb_arg_mut(f))
    }

    pub fn handler<Cl: 'static, A: 'static>(
        &self,
        f: impl Fn(&C) -> Cl + 'static,
    ) -> impl crate::callback::CallbackArg<A>
    where
        Cl: Into<Checklist<C>>,
    {
        self.cb_dropped_arg(f)
    }

    pub fn handler_mut<Cl: 'static, A: 'static>(
        &self,
        f: impl Fn(&mut C) -> Cl + 'static,
    ) -> impl crate::callback::CallbackArg<A>
    where
        Cl: Into<Checklist<C>>,
    {
        self.cb_dropped_arg_mut(f)
    }

    pub fn handler_arg_mut<Cl: 'static, A: 'static>(
        &self,
        f: impl Fn(&mut C, A) -> Cl + 'static,
    ) -> impl crate::callback::CallbackArg<A>
    where
        Cl: Into<Checklist<C>>,
    {
        self.cb_arg_mut(f)
    }
}

impl<C: Component> CompInstance<C> {
    pub(crate) fn render(&mut self, comp: &Comp<C>) {
        let state = self
            .state
            .as_ref()
            .expect_throw("A immutable reference for rendering component");
        let status = if self.root_element.is_empty() {
            ElementStatus::JustCreated
        } else {
            ElementStatus::Existing
        };
        let er =
            crate::render::base::ElementUpdater::new(comp, state, &mut self.root_element, status);
        state.render(er.into());
    }

    fn extra_update(
        &mut self,
        should_render: ShouldRender,
        mut commands: Commands<C>,
        comp: &Comp<C>,
    ) {
        if let ShouldRender::Yes = should_render {
            self.render(comp);
        }
        commands.execute(
            comp,
            self.state
                .as_mut()
                .expect_throw("A mutable reference for executing commands"),
        );
    }

    pub fn state(&self) -> &C {
        self.state
            .as_ref()
            .expect_throw("Immutably borrow the state from CompInstance::state()")
    }

    pub(crate) fn is_mounted(&self) -> bool {
        matches!(self.mount_status, MountStatus::Mounted)
    }

    pub(crate) fn root_element(&self) -> &Element {
        &self.root_element
    }
}