7 ms·
The Elm Architecture
- wa1987 8y agoIn what ways does the Elm architecture differ from MVC?
- m90 8y agoNot the Elm Architecture, but I found this a very good article on how MVC differs from unidirectional approaches: https://www.futurice.com/blog/reactive-mvc-and-the-virtual-dom/ https://www.futurice.com/blog/reactive-mvc-and-the-virtual-d...
- mpweiher 8y agoAs usual, that article gets MVC wrong. In MVC, the model-view communication is not "interactive" as defined by the article (source pushes to sink). Instead, the view pulls data from the model. There is only a general #changed notification to let the view know that the model has changed. So "unidirectional" approaches fix the problems you get when you don't understand MVC. ¯\_(ツ)_/¯
- RobertRoberts 8y agoHow would you compare React to Vue in regards to the linked article? I have not used React, but in Vue I have never run into things like "setState, forceUpdate, setProps, render". It would seem that React runs quite differently than Vue deep under the hood, am I missing something? I appreciated the alternative view from the article, as I like to know where things are going, and his criticisms of React seemed valid, but I am new enough to this environment to need clarifications on complex claims.
- aikah 8y agoDon't know why you are getting downvoted, MVC is the combination of the observer and strategy patterns, no more, no less. All the pieces have a single responsibility, the view reacts to model changes and redraw itself, the controller executes the business logic on UI events and the model to broadcast events when its state has/was changed by the controller. Data flows only in one direction.
- ricardobeat 8y agoThe problems lie in the way change notifications are managed. Any reasonably complex client application will have multiple data sources feeding into a myriad of components, with a very entangled dependency graph. In MVC, you’ll start sharing models and notifying changes selectively, with a lot of “action at a distance” from component interactions triggering updates. This is usually hard to visualize and debug (worst case, recursive update loops). In Flux/redux the unidirectional nature comes from all updates targeting and emanating from a single source (store), and being routed to views accordingly. An interaction from one component will never result in a disconnect where you failed to update the correct model(s) or to notify the right components of a change (hi Backbone.js).
- mpweiher 8y ago> Any reasonably complex client application will > have multiple data sources feeding into a myriad of components, Why? Certainly my MVC apps have a single "store" that's the source of truth, and I would say that what you describe is not following the MVC pattern.
- ricardobeat 8y agoYou're gonna have to provide a bit more context for that. What part of the MVC pattern?
- mpweiher 8y agoVarious parts. First, the M->V communication should only notify, the View decides if and when to refresh itself, and the action it takes is only to refresh itself. "In this metaphor, views deal with everything graphical; they request data from their model, and display the data."[1] Also, the View should only make a note that it needs to be refreshed (Cocoa for example has the convenient "needsDisplay" boolean property on Views), not actually do the refresh on notification. I see the latter a lot, so you have the model effectively calling refresh on the view, so pushing updates. That ain't MVC, in MVC the View pulls from the model. "The standard interaction cycle in the Model-View-Controller metaphor, then, is that the user takes some input action and the active controller notifies the model to change itself accordingly. The model carries out the prescribed operations, possibly changing its state, and broadcasts to its dependents (views and controllers) that it has changed, possibly telling them the nature of the change. Views can then inquire of the model about its new state, and update their display if necessary."[1] Once you're down with pushing updates, you tend to also see "optimizations" that also push the data to the view (very much what the linked article describes). Again, not MVC, View pulls, model doesn't push. If the view only refreshes itself and pulls from the model when notified, it's hard to get an entangled dependency graph, and downright impossible to get loops. So if you get loops, you're definitely not doing MVC. As to the "multiple sources" feeding into a "myriad of components", that also doesn't quite match. In MVC, all sources feed into the model. When the model changes, it sends its change notification, and it doesn't really matter where the original change came from, the action afterward is the same. So if "multiple sources" are causing you problems, that is a strong indicator that you are not doing MVC, and "myriad of components" also indicates that you don't have a strongly developed model. [1] https://web.archive.org/web/20100921030808/http://www.itu.dk/courses/VOP/E2005/VOP2005E/8_mvc_krasner_and_pope.pdf https://web.archive.org/web/20100921030808/http://www.itu.dk...
- thangngoc89 8y agoIf you want to use TEA but with easier interop with JavaScript libraries, you can use ReasonML/OCaml with bucklescript-tea[1] instead [1]: https://github.com/OvermindDL1/bucklescript-tea https://github.com/OvermindDL1/bucklescript-tea
- napsterbr 8y agoJust pulled an all-nighter deciding what to use instead of Elm. We have a 45kloc codebase in Elm and, despite having some big pros, the closed source development it goes through, along with the 0.19 debacle, just pushed us over the edge. The team is very motivated to ditch elm over something else. So, Bucklescript seems great (and it's wonderful how OvermindDL1 explains in the open his design decisions. An openness that I've missed in Elm), and IMO superior than Typescript, and comparable with Fable (in that case being mostly a matter of personal taste: F# vs ocaml). But, as long as we are trying new stuff, I've decided to use Clojurescript with Reagent and re-frame. The tooling around it seems very mature (even more so than Elm, but I may be wrong), with a good community and macros! For anyone in a similar position, several good options out there, but the best two that I've found, in my opinion, are Bucklescript (ReasonML/Ocaml) and Clojurescript. Cheers!
- sixstringbudha 8y ago> We have a 45kloc codebase in Elm and, despite having some big pros, the closed source development it goes through, along with the 0.19 debacle, just pushed us over the edge. Can you clarify in concrete terms what the closed source development and 0.19 thing had cost you?
- napsterbr 8y agoI guess I've replied on the sister thread. 0.19 was just the final push. Closed development plus complete blocking of contrary feedback just does not feel like open source. The lack of transparency removed all the trust we had in the project, and although our app is relatively big, it is still viable to change to something else - that we trust.
- Touche 8y agoSo what ties together the model, update, and view? It looks to me like it is this Browser module, is that right? Not knowing Elm or Haskell syntax, I'm guessing that Browser.sandbox is a function you call, passing in the model, update, and view functions. Assuming that's all correct, how does Browser.sandbox, work exactly? In particular how does it know when to call Update? At first glance it looks like it might massage the view and detect event listeners such as onClick in the button example, is that correct?
- hawkice 8y agoTurning Html Msg types into actual Dom nodes means hooking up the event handlers so that they call update with the Msg the event handler is given. Browser.sandbox is building the Dom nodes and has the update function.
- Touche 8y agoYeah, I got that far. How does it know what it should turn into an Update handler though?
- hawkice 8y agoYou pass it the update function, the one function handles all updates. Typically, you'll have a case statement in that function to dispatch it out to something more specific.
- neoberg 8y agoBrowser.sandbox, Browser.application etc. are your app's runtimes. (Or they are the doors to the runtime) Subscriptions or DOM elements can generate Msgs and every Msg causes an update cycle.
- rtfeldman 8y agoGreat question! You're correct, it's the Browser module. In Elm, the entire application state is a single immutable value, which Browser owns and only changes when it calls `update` and a new value gets returned. Browser calls `update` in response to these 3 situations: 1. An event handler from view (e.g. `onClick Foo` means Browser will call `update`, passing `Foo` as the message, whenever that element gets clicked) 2. A subscription fires (this is kinda like #1 except for global events instead of per-element handlers) 3. A command completes, and its resulting message needs to get sent to `update` Every interaction in Elm is modeled in terms of one of these!
- avolcano 8y agoThe bit I love about the Elm Architecture that I really wish existed in other frameworks is the "Command" system, by which the "Update" step can dispatch further changes. Basically, imagine if a Redux reducer could dispatch further actions. I've tried building Redux apps with bidirectional real-time communication (re: networked video games), and found myself having to pull almost all of the logic up to the action layer, as I often needed to dispatch further side effects based on changes that happened as the result of an action. It made my reducer into nothing more than a dumb setter, and caused me to have to write complex mocked tests for my action layer instead of my reducer layer. I know there's been some attempts at bringing this to Redux (such as https://github.com/redux-loop/redux-loop https://github.com/redux-loop/redux-loop), but from what I've seen it isn't quite as easy to use. OTOH, I will say, as an outside observer, it sounds like Elm has run into problems with this approach (see: the removal of WebSockets from this guide, pending a rewrite), so I am curious to know more about what issues arose.
- bcherny 8y agoCheck out Undux effects - they're exactly that. https://undux.org https://undux.org
- kiliancs 8y agoredux-loop is harder to use, but so is Redux itself. They are both having to fight the language (JS) to do what is so simple in elm.
- rtfeldman 8y agoTo be fair, the websocket package's design problems are unrelated to the Elm Architecture - just plain old run-of-the-mill API challenges. :)
- mudetroit 8y agoredux-observable can give you something rather akin to that type of behavior. It sees each action after it has already been applied to update state. This also separates out the reducer from those side-effects, which you really want anyhowto keep your reducers as pure functions.
- 8y ago
- zoul 8y agoI like the Elm architecture so much we have tried to do something similar in Swift on iOS. It’s a shame it’s not possible to go all the way in (it’s quite hard to find an analogy for the DOM diffing view part), but after a year of work or so it looks like the principled state management + message-based state changes + principled side effect modelling brought us about 70–80 % of the advantages. It feels _so_ good so see the state of the app change using well-defined messages and not by ad-hoc state changes on some entangled forest of singletons. I look forward to experimenting with the architecture further. I can’t recommend it enough.
- dom96 8y agoHuh, I must say I thought there was more to Elm's architecture (I've seen it mentioned in the recent negative article about Elm). I've written an SPA[1] recently in a framework that is quite flexible and also ended up using a very similar architecture. 1 - https://github.com/nim-lang/nimforum https://github.com/nim-lang/nimforum
- andrejewski 8y agoIf anyone wants the Elm architecture in JavaScript there is Raj: https://jew.ski/raj/ https://jew.ski/raj/ I spent over a year building the framework and a set of libraries around it so that the architecture feels natural to JavaScript developers. There's only a few of us building apps with it right now, but the stories for doing most anything are covered and as of 2 months ago the framework hit 1.0. It is a safe foundation to build on as I don't plan to ever change it.
- deleted 8y ago[deleted]
- sridca 8y agoYesterday's submission on Elm with interesting discussion in comments: https://news.ycombinator.com/item?id=17842400 https://news.ycombinator.com/item?id=17842400
- fruitfruit 8y agoWhy would anyone care about this? Web development, as a profession, is going away. Look at Squarespace. Look at how low web-developer salaries are.
- vmchale 8y agoThis is just model-view-controller except Evan is taking credit for inventing it.