markdalgleish / redial

Universal data fetching and route lifecycle management for React etc.
1.1k stars 42 forks source link

Feature/waterfall #32

Closed meriadec closed 7 years ago

meriadec commented 8 years ago

Hi, thanks for this awesome tiny tool :D

I've been stuck into a problem in a big web project recently, where I had some nested routes with hooks both on parent and children views (react-router). The children needed the parent data to be resolved before him, but it can't be guaranteed with a Promise.all call.

So, I would find it very useful to have the ability to chain hooks instead of resolving them concurrently. This PR introduce the waterfall function, which is basically a trigger, but with chained hooks (I trust components to appear in the right order : "outside" components first).

I didn't achieve to write the correct test for it. Do you have time to help?

coveralls commented 8 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling a69299b4cccc80fc0c093544cb6802ba143b096c on meriadec:feature/waterfall into 5b8f7f8086219d6e285b1df62dbd879bc9428fc3 on markdalgleish:master.

jaredpalmer commented 8 years ago

Maybe I'm not understanding the issue, but why can't you just write another action creator that dispatches the actions in the sequence you need them?

markdalgleish commented 8 years ago

To be honest, making your hooks depend on each other doesn't sound like a good idea, but I'm not against providing an escape hatch if needed.

How about I provide a way to get an array of hook thunks, then you can trivially implement waterfall yourself, among other things. The API could look like this, for example (using promise-map-series):

import { triggerThunks } from 'redial';
import mapSeries from 'promise-map-series';

...

const fetchThunks = triggerThunks('fetch', components, locals);

mapSeries(fetchThunks, fn => fn())
  .then(() => console.log('Done!'));
idangozlan commented 7 years ago

Actually waterfall is super needed on big projects. It's hard to use provideHooks the route that i'm using is depending on other things, for ex:

I have profile page which there is nested route of profile/:id/comments

The comments has to be loaded only after profile has been loaded since it's use some of the profile responses to retrieve relevant comments.

In this example, you can see that waterfall is a must, and I can implement it by myself but then all the concept of provideHooks not working as expected

Exomnius commented 6 years ago

Is there a trigger function available yet for triggering the fetch hooks in sync (waterfall)? Of is there an alternative approach for this?