flux icon indicating copy to clipboard operation
flux copied to clipboard

Docs don't adequately explain some concepts

Open vcarl opened this issue 11 years ago • 12 comments

People new to the concept of Flux seem to consistently have questions on a few areas:

  • when actions should be fired, and what they should represent
  • how to introduce data from a server

This suggests that the documentation for these areas is pretty consistently failing people. Additionally, people very often attempt to use actions imperatively, which--to my understanding--is a poor design decision that undercuts what Flux is supposed to encourage.

I propose that documentation should be made that demonstrates what an application's actions should look like. I would love to make a PR for this, but I don't feel confident in my knowledge of how Flux is intended to be used. If this issue gets some discussion that helps my understanding, I'd be happy to write a first pass.

vcarl avatar May 22 '15 20:05 vcarl

@vcarl Thanks for raising this question once again. To my understanding, actions should really be called events in Flux architecture, the docs say that we should call actions like 'something has happened' (which is a notification about an event), not 'do something' (which is an imperative command).

sompylasar avatar May 23 '15 14:05 sompylasar

@sompylasar Yeah, I definitely agree with you. The problem is that most people refer to actions as Commands in CQRS terms, therefore something what should happen rather than what has actually happened. Action in my opinion is definitely an interaction with the system (be that a user interaction or API callback) and should never expose implementation detail. An example might be LOAD_USER vs USER_LOADING_REQUESTED.

tomkis avatar May 23 '15 18:05 tomkis

Discussion like this is why I want there to be more clarification around actions. I don't think simply changing what actions are named is enough to change a system of imperative to declarative, but there isn't really good information about how to plan a declarative system. I'd love to avoid writing multiple applications in order to figure out seemingly simple questions like this.

I'd also like to say that the reason I think this needs attention is because the bar for documentation has been set so high by React. Those docs were so fantastic that I felt really let down that the Flux docs are only good, not amazing.

vcarl avatar May 24 '15 10:05 vcarl

The Flux docs will get a new page soon that will focus on async programming, and I will attempt to revise some existing pages soon after that.

Note: there is no Flux team at Facebook, and there is no Flux framework at Facebook. So comparing the documentation there to the docs here is a little bit unfair, in my opinion. I know they could be better, and like I said, I'm working on it.

fisherwebdev avatar May 26 '15 18:05 fisherwebdev

@vcarl Could you please explain why 'imperatively' is bad in Flux architecture? I'm new to Flux. I fail to understand why it is bad. Looking at the TodoActions.js from the Todo example, the actions look almost exactly same as commands in CQRS system. They simply act as DTOs. What's wrong with them?

moon0326 avatar May 26 '15 22:05 moon0326

@moon0326 In simple cases the imperative way is probably not a big deal. Like in the example, it's pretty straightforward. However, when it comes to more complex use cases you might want to treat your actions like events instead of commands.

In my opinion, it's always a bad practice to call more actions at once, especially when those actions are fired only when certain condition is met (meaning it depends on application state, meaning you are pulling the business logic from stores - where it should actually be). Eventually, you will have to do it when using commands.

Let's say you have two stores in your application: BreadcrumbsStore and ProductDetailStore. Once your application loads detail data, it will get name of product and you would like the name to display in breadcrumbs and product detail. Treating them as commands would result in firing two actions UPDATE_BREADCRUMBS and UPDATE_DETAIL not very descriptive though and very specifically tied to the business logic. However, if it was an event PRODUCT_DETAIL_LOADED the application could handle the action (event) in both stores.

Action represents interaction with system. Mostly an action initiated by user (click), but it may also be a callback from API call. And this interaction has definitely happened in the past and therefore it's an event. On other hand, command is something that should happen. Just ask yourself, what is the click on the button? Is that an event or command?

Treating actions as commands is leading to exposing your business logic outside the store.

tomkis avatar May 27 '15 13:05 tomkis

@fisherwebdev Ah, I wasn't aware of that. That explains a lot of the differences then, I was under the impression it was larger.

I began writing a blog post attempting to explain some of the fundamentals of Flux (directly in response to the consistent questions I was seeing), but after a while started to feel like I didn't understand what I was explaining. I'd love some feedback or contributions. The bottom is the roughest part. https://gist.github.com/vcarl/a5b362abef48767255cb

@tomkis1 That's precisely what I was talking about, I love your example. Imperative actions aren't bad at a small scale (which I think is part of their danger), but when you have a massive isomorphic application with 20 people working on it, having them be glorified function calls doesn't have any benefit.

The difficulty I've had is figuring out how to have actions be decoupled from the business logic. Should there be a 'BUTTON_CLICKED' with a payload that allows stores to figure out if they should respond? Should each button click have a distinct action? I feel like the answer is somewhere in the middle, but I'm not sure what that would look like, or how it would be implemented.

vcarl avatar May 27 '15 16:05 vcarl

@tomkis1 Ah I see. So there's no command in Flux system. I was still thinking in CQRS where we usually command something then emit events. So..in this frontend case, where we have users....I guess user interactions are command and Actions in flux should be event.

Thank you for the answer.

moon0326 avatar May 27 '15 23:05 moon0326

@vcarl Me personally always choose naming that reflects the interaction with the system. IMO firing BUTTON_CLICKED with payload is not optimal as you hide your business logic in action's payload. It's better to use something like USER_LOADING_REQUESTED

@moon0326 well the difference here is that in standard CQRS, command is responsible for emitting corresponding events based on business logic. Handling emitted events serves for creating a projection (though it's most likely combination of EventSourcing and CQRS). In Flux action handler / event handler (which is store) is responsible for business logic, Store reduces events into application state.

tomkis avatar May 28 '15 15:05 tomkis

@tomkis1 Thank you for the write up. That explains a lot.

I should get my hands dirty on Flux more, but it sounds like to me that even Facebook uses imperatively action names (https://news.ycombinator.com/item?id=7719957) and past tense event names at the same time.

moon0326 avatar May 28 '15 17:05 moon0326

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

ghost avatar Aug 05 '15 18:08 ghost

I've completely rewritten the examples, including a complex example dealing with server data and async actions. Hopefully it will provide enough guidance on dealing with async actions.

https://github.com/facebook/flux/tree/master/examples

kyldvs avatar Dec 16 '16 02:12 kyldvs