dead-code.rs 2 KB
async fn logic( mut rx_logic: broadcast::Receiver<AppLogic>
              , tx_view: broadcast::Sender<AppView>
              , mut rx_dom: broadcast::Receiver<Dom> ) {
    let mut clicks = 0u32;
    let dom = rx_dom.next().await.unwrap();

    while let Some(msg) = rx_logic.next().await {
        if let Either::Left(dom_js) = dom.inner_read() {
            /*
             * Here we can get the whole dom element with all sub elements and
             * text data.
             * Needed to get the contenteditable dom element content.
             */
            let text = dom_js
                     . to_owned()
                     . dyn_into::<Node>().unwrap()
                     . first_child().unwrap()
                     . first_child().unwrap();

            console::log_1(text.as_ref());
        }

        match msg {
            AppLogic::Click => {
                clicks += 1;
                tx_view.broadcast(AppView::Clicked(clicks)).await.unwrap();
            },
            _ => ()
        }
    }
}

fn view( tx_logic: broadcast::Sender<AppLogic>
       , rx_view: broadcast::Receiver<AppView>
       , tx_dom: broadcast::Sender<Dom>
) -> ViewBuilder<Dom> {
    builder! {
        <div style:float="left"
             style:padding="1em"
             style:border_radius=".5em"
             style:border="1px solid #ddd"
             style:background="#f7f7f7"
             style:cursor="pointer"
             on:click=tx_logic.sink().contra_map(|_| AppLogic::Click)
             capture:view=tx_dom.sink()>
            <p>
                {(
                    "Hello from mogwai!",
                    rx_view.to_owned().map(|msg| {
                        match msg {
                            AppView::Clicked(1) =>
                                format!("Caught 1 click, click again 😀"),
                            AppView::Clicked(n) =>
                                format!("Caught {} clicks", n),
                        }
                    })
                )}
            </p>
        </div>
    }
}