Skip to content

Re-render in async callback #911

@Kelerchian

Description

@Kelerchian

Hi @deniskolodin

I saw this issue #364 and I thought:
Is there any plan to create async callback which in it, component view() can be triggered multiple time

// the Model struct

struct Model {
    link: ComponentLink<Self>,
    pub state: Arc<Mutex<SomeStruct>>
}


// inside fn view()

let state_clone = Arc::clone(&self.state);
let onclick = self.link.async_callback(async move |scope| => {

    // My example of is currently ugly. I think I can do better with macro here
    if let Ok(mut state) = state_clone.lock() {
        (*state).isDoingSomeAsyncThing = true;
    }
    scope.try_refresh().await   // refresh here, imaginarily take current snapshot of Model and queue VNode diffs with the snapshot behind the scene


    // Do some async thing here ...


    if let Ok(mut state) = state_clone.lock() {
        (*state).isDoingSomeAsyncThing = false;
    }
    scope.try_refresh()  // refresh here
})

html! {
    // Render your model here
    <button onclick=onclick>{
        match self.data.lock() {
            Ok(x) => match (*x).isDoingSomeAsyncThing {
                true => "Doing async thing",
                false => "Do async thing",
            },
            _ => "..."
        }

    }</button>
}

This is similar to what we can do in the js world and it'll be an interesting addition to yew

async () => {
  if(this.isRunning) return;
  this.setState({ isDoingSomeAyncThing: true })
  // do async things
  this.setState({ isDoingSomeAyncThing: false })
}

Btw, I'm new to requesting features in OSS projects so let me know if it's not the good way to go

Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions