Comments (6)
Hmm. I've usually found it's best to keep async operations outside of store handlers and move them into either actions or controller-views instead. In the example you've given, for instance, maybe your controller-view (or whatever module is triggering the data loading) can check if the store has the data already before firing the action.
However, I realize that there will always be exceptions, and I certainly can't say it's always a bad idea to put async stuff inside your stores. Perhaps Flummox should have the option to disable warning messages entirely? With an environment variable, perhaps. Does that sound like a good solution?
from flummox.
The warning is probably useful 99% of the time, but in this case I don't feel that I'm doing anything "bad" per se. I think it's definitely a good thing to be able to suppress the warning. An environment variable is fine. It would also be nice if there was a more "supported" means of doing async in store.
I did look over registerAsync to see if that would be helpful, but I couldn't drive the promise from the store side, so that didn't work out for me. One thing that might've been workable is if the promise had been passed to the begin handler, just like it is to the success and fail handlers. I guess that would be a breaking change, but it would open up a new pattern for me.
With that promise I think I could use the begin handler to initiate my async operations. Then when they complete I could resolve the promise with the correct data. Then my start handler would be called and I could set state there, and I'd be fine.
from flummox.
I'm conflicted. I don't want to get in the way of people who know what they're doing, but I also don't want to encourage bad habits, and in my view, doing async inside of stores is almost always a bad idea. Even in your example, I'm unconvinced that it wouldn't be better to handle caching before firing any actions at all.
Regarding this part of your comment:
One thing that might've been workable is if the promise had been passed to the start handler, just like it is to the success and fail handlers
Success and fail handlers don't receive a promise; they receive the resolved or rejected value that is the result of a promise. We could pass the promise to the begin handler, but I'm not sure how that would help in your situation. If the request is already in progress, how does having access to the promise help the store do caching logic?
Back to the original issue, maybe one way to offer "support" for async in stores is with something like setStateAsync()
, which accepts a promise that resolves to a state object. Again, though, I'm hesitant to officially support something like that out of concern it will be abused. Not opposed, but hesitant.
from flummox.
Or I guess we wouldn't need a separate method. We could overload setState()
to detect if a promise is passed, much how actions work.
Same concerns apply, though.
from flummox.
I could get behind a setState that understands promises.
Maybe you could expand a bit further on why async in stores is a bad idea. I'm open to being convinced.
Otherwise I'd see this as a natural extension to asynchronous actions, just moving the work over into the store.
I'll spend a little time considering how my app my work if I asked my components to check cache first.
from flummox.
I started reading on the net about async data loads, and it seems you are definitely in the majority in your views. So, I'll refactor my code to make sure that async happens outside the store.
http://fluxxor.com/guides/async-data.html
"The short answer is: in order to ensure that all the stores in your application have a chance to respond to the successful (or unsuccessful) loading of asynchronous data, you should fire additional actions from your asynchronous handlers to indicate when loading fails or succeeds."
http://www.code-experience.com/async-requests-with-react-js-and-flux-revisited/
"Asynchronously executed callbacks should not leak into Stores. The consequences of it are just to hard to fully foresee. This leads to elusive bugs. Stores should only execute synchronous code. Otherwise they are too hard to understand."
https://news.ycombinator.com/item?id=7721292
Gent from facebook (wingspan) gives a bit of a contradictory answer, but seems to indicate that data should be async pulled in actions.
etc. etc.
from flummox.
Related Issues (20)
- how to get a child component input value HOT 1
- Missing getActionsAsObject from Actions HOT 5
- Incorrect params passed to async begin handler HOT 1
- More topic guides needed in doc HOT 1
- Context differs problem
- Do not pass flux prop to components wrapped with `connect` HOT 6
- Use Redux HOT 6
- Explanation in README as to why a switch to Redux should be made HOT 5
- Import flummox/component fails with 4.0.0-rc1 HOT 1
- unable to make flummox work with react-router 1.0.0-beta3 HOT 2
- Actions.js grabs all console logs HOT 4
- Wrong link in why-flux-component-is-better-than-flux-mixin.md
- Considering the numbers HOT 1
- Support for React 0.14 HOT 17
- How to only use custom stateGetter for subset of subscribed stores HOT 1
- Maintain module? HOT 4
- FluxComponent does not work on IE10 HOT 24
- Flummox 3.6 is incompatible with IE8 HOT 3
- TestUtils removed from NPM HOT 2
- Deprecation warning with React 15.5 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flummox.