Seed: Question: App::update vs seed::update

Created on 20 Jun 2019  路  7Comments  路  Source: seed-rs/seed

As far as I can see there are two ways to trigger an update event:

  1. via the update method of an App instance without (De)Serialization
  2. via the global seed::update function with (De)Serialization

I wonder what's exactly the difference and which way do you recommend?
Consider this context:

let app = seed::App::build(Model::default(), update, view::app)
        .finish()
        .run();
    let a = app.clone();
    let on_message = Closure::wrap(Box::new(move |ev: MessageEvent| {
        let txt = ev.data().as_string().unwrap();
        let notification: NotificationPayload = serde_json::from_str(&txt).unwrap();
        a.update(Msg::ServerMsg(notification));
    }) as Box<FnMut(MessageEvent)>);

I could get rid of app.clone() and use seed::update instead:

```diff

  • let a = app.clone();
    let on_message = Closure::wrap(Box::new(move |ev: MessageEvent| {
    let txt = ev.data().as_string().unwrap();
    let notification: NotificationPayload = serde_json::from_str(&txt).unwrap();
  • a.update(Msg::ServerMsg(notification));
  • seed::update(Msg::ServerMsg(notification));
    }) as Box ```

What do you think? Is the clone operation faster than serializing and deserializing the message + triggering an extra browser event?
Or what other aspects do I have to take into account?

question

All 7 comments

I would use seed::update where using app is problematic - e.g. https://github.com/David-OConnor/seed/blob/master/examples/animation_frame/src/lib.rs#L81 => we don't have access to App instance here.

And app.update for other cases - I think that clone should be cheap, because all App's fields are Rc (https://github.com/David-OConnor/seed/blob/master/src/vdom.rs#L200).

I agree with MartinKavik. seed::update should only be used when app.update and events can't be used.

Thank you!
I added a small note to the docs (#157).

I still wonder why seed::update is used here:

https://github.com/David-OConnor/seed/blob/ce4d56932c05217dd9496d3b69715b678464e2ac/examples/websocket/src/client.rs#L157

I still wonder why seed::update is used here:

If I remember correctly

  • I wanted to test seed::update somewhere
  • So we don't need to pass app through function parameters

If you think that it's confusing we can add comment to it or rewrite to app.

If you think that it's confusing we can add comment to it or rewrite to app.

I think adding a comment would be enough :)

seed::update is deprecated and will be removed. I suggest to close the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sbditto85 picture sbditto85  路  3Comments

songmaotian picture songmaotian  路  4Comments

MartinKavik picture MartinKavik  路  4Comments

dashed picture dashed  路  5Comments

TuntematonSotilas picture TuntematonSotilas  路  3Comments