Coder Social home page Coder Social logo

Comments (7)

rsaccon avatar rsaccon commented on May 8, 2024

One more thought on this: My suggestion to let update return an Option<Model> will cause trouble when calling update recursively.

So maybe update could return a tuple like: (Model, shouldRenderBool)

Of course this will break everything. But I think, sooner or later, a way to skip render at update needs to be provided.

from seed.

David-OConnor avatar David-OConnor commented on May 8, 2024

Agree - will make breaking/significant changes if needed to get this working.

from seed.

David-OConnor avatar David-OConnor commented on May 8, 2024

The latest commit has your Tuple idea built in to the lib, and to the counter example. I don't like how it adds verbosity to the API, but I don't have a better approach. Perhaps there's a way to improve this using proc_macros, so that your update func could return just the model for a render, or a (Model, false) tuple for not rendering.

from seed.

rsaccon avatar rsaccon commented on May 8, 2024

I also don't like it. Another possibility: returning an enum:

enum ModelUpdate {
  ShouldRender(Model),
  SkipRender(Model)
}

from seed.

David-OConnor avatar David-OConnor commented on May 8, 2024

I like that more than tuple; got it working on my setup. Need to think on this; the enum approach isn't bad, but still adds code to every update branch for a minority use case. Putting the enum and its variants in the prelude to minimize extra code.

Counter:

fn update(msg: Msg, model: Model) -> ModelUpdate<Model> {
    match msg {
        Msg::Increment => Render(Model {count: model.count + 1, ..model}),
        Msg::Decrement => Skip(Model {count: model.count - 1, ..model}),
        Msg::ChangeWWC(what_we_count) => Render(Model {what_we_count, ..model }),
    }
}

If the update func's set up in a mutable way, there will be less extra, especially if all branches are Render (or Skip) Eg, with all paths render:

fn update(msg: Msg, model: Model) -> ModelUpdate<Model> {
    let mut model = model;
    match msg {
        Msg::Increment => model.count += 1,
        Msg::Decrement => model.count -= 1,
        Msg::ChangeWWC(what_we_count) =>model.what_we_count = what_we_count,
    }
    Render(model)
}

from seed.

rsaccon avatar rsaccon commented on May 8, 2024

If you go with the enum approach, then maybe shortening the enum name also saves some extra

enum Update {
  Render(Model),
  Skip(Model)
}

from seed.

David-OConnor avatar David-OConnor commented on May 8, 2024

Included in v0.2.4 release, using your last suggestion. I think the extra verbosity is worth the flexibility.

from seed.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.