🕸️ Capturing parts of the View

Views often contain nodes that are required in the logic loop. When a view node is needed in a logic loop we can capture it using a sink, or better yet, Captured.

Using the capture:view attribute

To capture a view after it is built you can use the capture:view attribute with an impl Sink<T>, where T is your domain view type, and then await the first message on the receiver:

        use mogwai_dom::core::channel::broadcast;
        use mogwai_dom::prelude::*;

        futures::executor::block_on(async {
            println!("using channels");
            let (tx, mut rx) = broadcast::bounded::<Dom>(1.try_into().unwrap());

            let builder = rsx! {
                div(){
                    button(capture:view = tx) { "Click" }
                }
            };

            let div = Dom::try_from(builder).unwrap();

            div.run_while(async move {
                let _button: Dom = rx.next().await.unwrap();
            })
            .await
            .unwrap();
        });

Using the Captured type

To make capturing a view easier you can use the Captured type, which encapsulates the ends of a channel with a nice API: