119 points · by tosh · 8y ago · 79 comments
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), 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.
You can achieve the same by using redux-saga[0], then writing a generator function ("saga") that inspects dispatched actions, and potentially triggers a side effect, dispatches another action, waits for another action to get dispatched in a certain time, etc.
Extra bytes are not a trivial issue for frontend web development, like they are for many server projects.
There are also more fully-baked middlewares like Redux Saga, but I find that those are a bit too heavy-handed and result in too much “lock-in” for implementing your actions and action creators.
Some pedants online continually claim this - or something like it - is an "anti-pattern" but I'm trying to get stuff done, and this is the way to do it.
Since you're dealing with them from within an observable stream you have access to all the rxjs operators, so you can do things like waiting for multiple actions to be dispatched before doing something, etc.
The result feels very Elm-like, albeit sans the assurances of the typing.
You can have sagas subscribing to actions and processing them in arbitrary ways, including dispatching new actions.
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!
We've waited for this fix a long time (over a year), but we can't upgrade because the only (AFAIK) library that connects to Elixir's Phoenix Channels stopped working. In fact the language (0.19) currently has no websocket support.
During this whole year of development, we could not compile the 0.19-dev. It was somewhere closed. We couldn't even see the commits. Pretty sure there's a nice and long post somewhere explaining how "outsiders" would slow down the progress by giving, how dare them, feedback.
We could wait a little bit more, but, even though I was deeply in love with elm, the language that made me not dread frontend development anymore, I am now totally uninterested at it, driven away by 1) lack of transparency and 2) total blocking of genuine and constructive user feedback, which feels like censorship - or simply a closed-source, proprietary software.
The language is good though. Has some minor flaws, but I believe over time they will get fixed. Though it lacks some powerful features that will never be implemented. But ovoverall, we were able to tackle complexity quite nicely. I guess we - me and my team - just got tired of how it is handled.
Oh, and Elm is way too verbose. Pretty sure that 45kloc codebase will be about 15-20kloc in cljs!
Edit: Using a throwaway so I don't get banned from Elm Discourse and /r/elm. I still have production applications using Elm.
I can't blame them - I worked on an SPA where the company used an older version of a javascript framework. After that version's docs went offline completely (they were actually for a slightly newer version with some extra functionality we couldn't use), the only existing docs were a PDF and the source code. Not a great position to be in.
Can you clarify in concrete terms what the closed source development and 0.19 thing had cost you?
http://docs.reflex-frp.org/en/latest/
"React and Elm do not offer the same concept of FRP as Reflex. Reflex offers something based on Conal Elliott's push-pull paper. Conal Elliott is the father of FRP (at least of the theory of FRP), and the semantic model he envisioned is similar to Reflex, but quite different from React/Elm. I believe his semantic model is pretty superior, and Reflex implements it quite nicely." https://www.reddit.com/r/haskell/comments/5aksxa/ghcjs_frame...
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?
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!
Subscriptions or DOM elements can generate Msgs and every Msg causes an update cycle.
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. ¯\_(ツ)_/¯
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).
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.
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.
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.
I've written an SPA[1] recently in a framework that is quite flexible and also ended up using a very similar architecture.