Skip to content

make map_message generic #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spersson opened this issue Jul 2, 2019 · 6 comments
Closed

make map_message generic #169

spersson opened this issue Jul 2, 2019 · 6 comments

Comments

@spersson
Copy link
Contributor

spersson commented Jul 2, 2019

I encoutered a problem when trying to grow my app into having a sub - (view/update/Msg/Model).

fn view(model: &Model) -> Vec<El<Msg>> {
    let sidebar = div![
        class!["sidebar"],
    ];
    let mut result = vec![sidebar];
    for (prop_id, prop) in &model.properties {
        result.push(property::view(prop).map_message(|m| Msg::Property(prop_id, m)));
    }
    result
}

This will not compile because map_message is not accepting closures, needs to be generic to do that.
In my case it has be a closure since it is dynamically adding an unique identifier to each message.

@MartinKavik
Copy link
Member

Yeah, I encountered it a few times, too. In map_message and I think also in event handlers.

I wanted to change it, but it's not a super simple, because there are many dependencies.
So my "workaround" looks like this:

impl<Ms: 'static, GMs> Effect<Ms, GMs> {
    pub fn map_message_with_fn_once<OtherMs: 'static>(
        self,
        f: impl FnOnce(Ms) -> OtherMs + 'static + Clone,
    ) -> Effect<OtherMs, GMs> {
        match self {
            Effect::Msg(msg) => Effect::Msg(f(msg)),
            Effect::Cmd(cmd) => Effect::Cmd(Box::new(cmd.map(f.clone()).map_err(f))),
            Effect::GMsg(g_msg) => Effect::GMsg(g_msg),
            Effect::GCmd(g_cmd) => Effect::GCmd(g_cmd),
        }
    }
}

@jgrund
Copy link
Contributor

jgrund commented Jul 8, 2019

I'm hitting this quite a bit as well. Would be very useful to have this closure support.

@MartinKavik
Copy link
Member

MartinKavik commented Jul 16, 2019

also in event handlers.

Like this one:

form![
    raw_ev(Ev::Submit, |event| {
        event.prevent_default();
        Msg::FormSubmitted(article_slug) // fails because of article_slug
    }),
...

I'll try to implement support for closures for map_message and event handlers.
@David-OConnor Do you agree?

@David-OConnor
Copy link
Member

Yep

@MartinKavik
Copy link
Member

All handlers and mappers should be able to receive FnOnce now. So I think we can close it or wait to next release so @spersson (@jgrund ) will be able to test it.

@David-OConnor
Copy link
Member

Closing, due to @MartinKavik 's fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants