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
//! A `Gizmo` turns implementors of [`Component`] into something useful.
//!
//! Converting an implementor of [`Component`] into a `Gizmo` wires up
//! a set of [`Transmitter`]s and [`Receiver`]s into the `Gizmo`'s
//! [Component::update] function.
//!
//! For more info see [the Component module documentation][crate::component].
use std::{
    cell::{Ref, RefCell},
    rc::Rc,
};
pub use wasm_bindgen::{JsCast, JsValue, UnwrapThrowExt};
use web_sys::Node;
pub use web_sys::{Element, Event, EventTarget};

use crate::{
    prelude::{txrx, Component, IsDomNode, Receiver, Subscriber, Transmitter, ViewBuilder},
    utils,
};

/// A user interface component that can spawn views.
pub struct Gizmo<T: Component> {
    /// This gizmo's [`Component::ModelMsg`] transmitter.
    /// Sending on this [`Transmitter`] causes its [`Component::update`]
    /// function to run.
    pub trns: Transmitter<T::ModelMsg>,
    /// This gizmo's [`Component::ViewMsg`] receiver.
    /// Clones of this receiver are owned by all of this gizmo's views.
    pub recv: Receiver<T::ViewMsg>,
    /// This gizmo's internal state.
    pub state: Rc<RefCell<T>>,
}

impl<T> Gizmo<T>
where
    T: Component + 'static,
    T::ViewMsg: Clone,
    T::DomNode: JsCast + AsRef<Node> + Clone,
{
    /// Create a new [`Gizmo`] from an initial state using
    /// a view and the [`Transmitter`] + [`Receiver`] used to
    /// create that view.
    pub fn from_parts(
        init: T,
        tx_in: Transmitter<T::ModelMsg>,
        rx_out: Receiver<T::ViewMsg>,
    ) -> Self {
        let subscriber = Subscriber::new(&tx_in);
        init.bind(&subscriber);
        let state = Rc::new(RefCell::new(init));
        let tx_out = rx_out.new_trns();
        let rx_in = tx_in.spawn_recv();

        let (tx_view, rx_view) = txrx();
        rx_in.respond_shared(state.clone(), move |t: &mut T, msg: &T::ModelMsg| {
            t.update(msg, &tx_view, &subscriber);
        });

        rx_view.respond(move |msg: &T::ViewMsg| {
            let tx_out = tx_out.clone();
            let msg = msg.clone();
            utils::set_immediate(move || tx_out.send(&msg));
        });

        Gizmo {
            trns: tx_in,
            recv: rx_out,
            state,
        }
    }

    /// Create a new [`Gizmo`] from a stateful [`Component`].
    /// This will create a 'fresh' view.
    pub fn new(init: T) -> Gizmo<T> {
        let tx_in = Transmitter::new();
        let rx_out = Receiver::new();

        Gizmo::from_parts(init, tx_in, rx_out)
    }

    /// Use the Gizmo to spawn a [`ViewBuilder<T::DomNode>`].
    /// This allows you to send the builder (or subsequent view) somewhere else while still
    /// maintaining the ability to update the view from afar.
    pub fn view_builder(&self) -> ViewBuilder<T::DomNode> {
        self.state.as_ref().borrow().view(&self.trns, &self.recv)
    }

    /// Update the component with the given message.
    /// This how a parent component communicates down to its child components.
    pub fn send(&self, msg: &T::ModelMsg) {
        self.trns.send(msg);
    }

    /// Access the underlying state.
    pub fn with_state<F, N>(&self, f: F) -> N
    where
        F: Fn(&T) -> N,
    {
        let t = self.state.borrow();
        f(&t)
    }

    /// Set this gizmo's state.
    ///
    /// This silently updates the state and doesn't trigger any messages
    /// and does *not* update the view.
    pub fn set_state(&mut self, t: T) {
        *self.state.borrow_mut() = t;
    }

    /// Borrow a reference to the inner state.
    pub fn state_ref(&self) -> Ref<T> {
        self.state.borrow()
    }
}

impl<T: Component> From<T> for Gizmo<T> {
    fn from(component: T) -> Gizmo<T> {
        Gizmo::new(component)
    }
}

/// The type of function that uses a txrx pair and returns a View.
pub type BuilderFn<T, D> = dyn Fn(&Transmitter<T>, &Receiver<T>) -> ViewBuilder<D>;

/// A simple component made from a [`BuilderFn`].
///
/// Any function that takes a transmitter and receiver of the same type and
/// returns a [`ViewBuilder`][crate::view::builder::ViewBuilder] can be made
/// into a component that holds no internal state. It forwards all of its
/// incoming messages to its view.
///
/// ```rust,no_run
/// extern crate mogwai;
/// use mogwai::prelude::*;
///
/// let component = Gizmo::from(SimpleComponent::new(
///     |tx: &Transmitter<()>, rx: &Receiver<()>| -> ViewBuilder<HtmlElement> {
///         builder!{
///             <button style="pointer" on:click=tx.contra_map(|_| ())>
///                 {("Click me", rx.branch_map(|()| "Clicked!".to_string()))}
///             </button>
///         }
///     }
/// ));
/// ```
pub struct SimpleComponent<T, D: IsDomNode>(Box<BuilderFn<T, D>>);

impl<T, D: IsDomNode> SimpleComponent<T, D> {
    /// Create a new SimpleCopmonent form a static Fn closure.
    pub fn new<F>(f: F) -> Self
    where
        F: Fn(&Transmitter<T>, &Receiver<T>) -> ViewBuilder<D> + 'static,
    {
        SimpleComponent(Box::new(f))
    }
}

impl<T, D> Component for SimpleComponent<T, D>
where
    T: Clone + 'static,
    D: JsCast + AsRef<Node> + Clone + 'static,
{
    type ModelMsg = T;
    type ViewMsg = T;
    type DomNode = D;

    fn update(&mut self, msg: &T, tx_view: &Transmitter<T>, _sub: &Subscriber<T>) {
        tx_view.send(msg);
    }

    fn view(&self, tx: &Transmitter<T>, rx: &Receiver<T>) -> ViewBuilder<D> {
        self.0(tx, rx)
    }
}