Code Monkey home page Code Monkey logo

Comments (8)

evancz avatar evancz commented on May 16, 2024

Yes, totally! I just wrote up an internal document for Prezi that shows "the most general version" of update:

update : UpdateContext -> Action -> Model -> (Model, Effect)

-- whatever other information is owned by a parent module
type alias UpdateContext =
    { userName : String
    , ...
    }

-- request effects from the parent module
type Effect = Print | LoadUserPicture | ...

One cool thing about this is that it makes it super easy to cache and batch HTTP and database requests. If 4 different pieces need the user picture, we can be abstract about requesting it, and at the top create a cache of recently loaded images so we can skip the request.

I'll be writing about this for real as soon as I can!

from elm-architecture-tutorial.

evancz avatar evancz commented on May 16, 2024

The true most general version is probably like this:

update : UpdateContext -> Action -> Model -> (Maybe Model, Effect)

Which would let you delete yourself. I guess that could be exposed through the Effect union type as well though, so maybe this is isomorphic to the most general version :)

Do these comments clarify things? I think the big message is that you can be quite flexible in the return type to cover your particular case. I don't yet know "the one best way to do it" yet.

from elm-architecture-tutorial.

sdegutis avatar sdegutis commented on May 16, 2024

Do these comments clarify things?

Sure, totally.

Which would let you delete yourself.

That only makes sense of the model is itself a Maybe at its root node, which for many things doesn't even make sense. I guess you could try to structure models to be composable, where that might make sense. But I have no idea how you could split models into sub-components using Elm. Absolutely none.

from elm-architecture-tutorial.

sdegutis avatar sdegutis commented on May 16, 2024

But I have no idea how you could split models into sub-components using Elm. Absolutely none.

That was because I hadn't read your tutorial carefully enough. But now I have, and I do get it better. Well done! Nice tutorial!

from elm-architecture-tutorial.

srobertson avatar srobertson commented on May 16, 2024

I'd love to see a working example of using this generalized version of update .

I'm trying to make an Effect that fetches some JSON via a port as a work around until Elm 0.15 comes out. But struggling a bit.

from elm-architecture-tutorial.

srobertson avatar srobertson commented on May 16, 2024

Here's my first attempt

https://gist.github.com/srobertson/3c1f2ad09d50cc054297

from elm-architecture-tutorial.

TheSeamau5 avatar TheSeamau5 commented on May 16, 2024

I have used this archtecture a few times now and I've realized that I often have NoOp in my actions. To distinguish them I give them different names: NoClick, NoChange, NoMove, etc...

So, I kinda switched from dealing with Action to Maybe Action as it is more general and allows me to avoid having all of these NoOp thingies.

So, as such, I think that the general case would be :

update : UpdateContext -> Maybe Action -> Model -> (Model, Effect)

And, I don't know about the deleting oneself. I think that intent should be passed in through the Effect type and the parent is the one who decides. It kinda helps make things more general because you might just have a button and it so happens that this button is shaped like an X and deletes itself but it's just a button and should only communicate that it got clicked in some sense.

from elm-architecture-tutorial.

evancz avatar evancz commented on May 16, 2024

Alright, examples 5 through 8 should give a more comprehensive answer to this question now. If you want to do custom effects that handle GraphQL and the like, you can make a graphql-effects that is based on the elm-effects package as a basic foundation.

from elm-architecture-tutorial.

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.