-
Notifications
You must be signed in to change notification settings - Fork 781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parameterization of actions in V2 #751
Comments
I think I lean towards the tuple version since it approaches Elm syntax more, but don't care that much. The object version is certainly more "normal" JS. From the library code perspective, the current V2 code which handles the object-style actions is pretty clean & intuitive. } else if (typeof obj.action === "function") {
dispatch(obj.action(state, obj, data)) The array syntax would be a little more complex and uglier. (unless you skip the isArray check) } else if (Array.isArray(obj) && typeof obj[0] === "function") {
dispatch(obj[0](state, obj[1], data)
} |
@SkaterDad Thanks for your feedback, Mike. The implementation doesn't bother me personally. I'm just interested in reaching a consensus here. 🙏
Neither approach is too wildly different from the other, and that is what makes this decision hard. I don't take issue with the verbosity of the approach using objects, but I haven't been writing V2 apps for a long time either. Now, there is an actual problem when dealing with parameterized actions in effects and subscriptions. How do you pass a value to this action? MyEffect({ props, action: MyAction }) This works, but it's quite unpleasant. MyEffect({ props, action: { action: MyAction, value: 42 } }) To avoid the redundancy and mitigate the issue, we can use a different property name, but the result is not very satisfying either. MyEffect({ props, onComplete: { action: MyAction, value: 42 } }) I could live with that, but it means more to remember — was it onComplete or onSuccess? It would be nicer if effects treated the action property consistently. I think the tuples win in this case MyEffect({ props, action: [MyAction, { value: 42 }] }) But is this a real use case? I never had to do something like this. |
If parameterizing actions passed to FX is a valid use case, then personally I prefer the 2-tuple syntax. With that said, it's tough for me to see what that use case would be. Since the effect can already be parameterized, and FX have full control over the data they |
@okwolf It should be: when would you need to parameterize the actions dispatched at the time the effect is back with a result. I can't think of a scenario off the top of my head either. Is this the only reason to like the tuple syntax? |
For me, the tuple is the clear winner. The idea of keeping the action function and its args separated (rather than all in the same object) is a pro that you forgot to mention imo.
From initial response on here and in twitter. It doesn't seem like people are finding the tuple syntax too hard to digest and comprehend. |
Like everyone so far (?) I also prefer the tuple-variant. It makes intuitive sense. I also like the idea of props being allowed to be values or objects, and I don't like reserving words if it can be avoided. |
I agreed with the object syntax at the first point. But now I think that tuple syntax is better. Because object syntax has some problems as @jorgebucaran and others have mentioned. It is true that newcomers are difficult to understand, but if the main problem is only that one, it is primarily the better to choose a tuple. |
Please add your reaction to this comment 👍 if your vote for Tuple syntax |
Another vote for tuple syntax, would be good to have multiple argument support too (I believe it would be more intuitive for newbies, helps avoid having to name things, eases refactoring, eases typings etc etc) |
Tuple looks shorter and requires no reserved word, +1 for me |
That's a great idea. It is annoying sometimes to be forced into an object just to pass 2 arguments. [TheAction, arg1, arg2, ...] |
I'm against using positional parameters when there are multiple. With destructuring well-supported today, objects give named parameters, which I prefer. |
Yes, exactly. I've also got some idea of how to implement it elegantly in
Agreed, however, my suggestion is more to address the issues I mentioned: newbies, intuition (ie arguments being accepted just like a regular function does), refactoring, typings etc. |
The vote only includes So keep in mind that somethings are not solved best via pure democracy. For example, even in America, you don't generally get to vote on if you think "Free Speech" is a good idea. Frankly, my vote would be best represented by |
My comment was not intended to be disparaging of any prior comments, but merely a reaction of recent things at work where a prior voting process led to a "short term win" - and now a "long term frustration". The lesson learned was that the direction of a vote, and the conviction of that vote are nice to have together. |
@ericdmoore I agree that there could be a |
I like your style! You sir, @ddmng, are what we need more of in this world :) |
I'd like to learn from that lesson Eric. Anything else you can share with us? I felt this decision deserved an issue for two reasons: (1) it is a key aspect of the API and (2) I want to document the decision making process. This issue is our official record showing how we arrived at this decision and I estimate will be very useful in the future. |
@jorgebucaran - At the risk of taking your statement too literally :)
Absolutely, and I think the discourse here is civil and productive, and the public rationale and record keeping are laudable too. As far as lessons learned, There are two other things at play: "disagree and commit" and "the level of consideration" Disagree and commitNumerous well run companies benefit from a "disagree and commit" ethos. Where someone can strongly disagree, make their points, and then even if the final decision goes the against their point of view, they are able to muster up the strength to trudge through "the mud" that the may have even predicted. This is largely lacking in American politics, but some companies are still a strong hold of this very productive mentality. And I just wonder in the world of Consideration GivenWhat i alluded to in the comment above was that I have a preference, but it's based on a small amount of consideration.
Separate But Concrete Example If this was a "work setting" someone would likely spend a few days and attempt to simulate what a sufficiently complex system looks under both of the options. Where the goal is to let the voters have a more tangible, a more visceral, or a at least a more informed opinion on the ramifications of their opinion. So that each vote cast could be more enlightened than the one formed by minor considerations ConclusionThis thread and countless others serve as great milestones of how a community engages the member, and I find most of the HA community members more thoughtful about programming patterns than my usual comrades in a commercial setting. Perhaps this type of thing is less needed with people able to mentally visualize these things. In short "disagree and commit" generally allows for more insightful decision making. And you can get their by really appreciating the nuance of a decision (high consideration) or by assuming the "voice of the crowd" is seeing something I am not (low consideration) Action ItemsSeeking "High Consideration" - we could include more complex "Code Samples" (( Next week, intend to start working on a complex Code Sample assuming object destructuring && I will post it here as I am able to get to it. )) Sorry for the pseudo philosophy rant - especially if your |
👋 @ericdmoore. Fascinating. I really enjoyed reading this. And no, my question wasn't sarcasm in the slightest. I'm sorry you doubted that for a second. It was pure curiosity. Thank you for taking the time to write this! I think I could've added more than those 3 arbitrary examples, so I've added a new, more realistic example to the list above.
I tried in #672. However I still have more to improve in my dealings with the community. For example, I use slack too much. I feel that's only acceptable if it's met with an equally active operation on GitHub through issues which leave a (more or less) permanent record and document the history of the project. FWIW I've already started this by revamping the labels used in the project and retroactively searching old issues, summarizing their resolution and preemptively locking them in some instances. Anyhow, I'm really glad to hear you are on board with V2. 🎉 |
@ericdmoore I've added a new, more realistic example to the list above. My first impression is that the object syntax gets a bit annoying with the |
Tuples win. I'll update #749 to clarify the details. TL;DR<button onClick={[MyAction, { foo, bar, baz }]}>Do Something FooBarBazy</button> |
The goal of this issue is documenting and formalizing how to use parameterized actions. If you are not familiar with the new V2 Action API, please see #749.
In V2, the runtime (Hyperapp) calls our actions, not us. So, how do we pass a value to an action?
There have been two approaches on the table since the original V2 announcement was made in #672. Using a two-element array (henceforth referred to as tuple) or using an object.
Your mission, should you choose to accept it, consists of choosing the best between these two options. Good luck!
Options
Tuple Syntax
Pros
Cons
Object Syntax
Pros
Cons
action
(you can't have a prop namedaction
in your state)Examples
One-liner
Tuple-syntax
Object-syntax
Multi-line
Tuple-syntax
Object-syntax
Nested objects
Tuple-syntax
Object-syntax
Todo item component
Tuple-syntax
Object-syntax
Abandoned Options
A third option consists of currying the actions. Unfortunately, it has several disadvantages: poor debugability, non-declarative, invalidation of strict equality checks when diffing props from events, effects and subscriptions.
The text was updated successfully, but these errors were encountered: