fluxible-action-utils icon indicating copy to clipboard operation
fluxible-action-utils copied to clipboard

dynamic parameters for actions executed by executeMultiple

Open geekyme opened this issue 9 years ago • 2 comments

In the docs, I see this:


loadStuffForUser: [
            'loadUser',
            {
                // will be executed after 'loadUser' task completes successfully 
                action: require('../../actions/loadStuffForUser'),
                params: context.getStore(UserStore).getGUID()
            }
        ]

getGUID is ran at runtime when executeMultiple is called, so wouldn't the user not be loaded by the time the action "loadStuffForUser" is called?

geekyme avatar Sep 28 '15 14:09 geekyme

for instance right now i have a piece of application code like this:

actionUtils.executeMultiple(context, {
      user: {
        action: getUser
      },
      location: {
        action: getLocation
      },
      groups: [
        "location",
        {
          action: getAll,
          params: {
            countryCode: context.getStore("ApplicationStore").getLocation().get("country_code")
          }
        }
      ],

....

I realized that the getAll action creator is getting called with countryCode = undefined because ApplicationStore is still not hydrated with the location data that is provided only after the action creator getLocation has been run.

As a result I will have to place the code fragment context.getStore("ApplicationStore").getLocation().get("country_code") into the getAll action creator to make this work, however, this makes the action creator difficult to test because it is now getting data from else where other than its arguments.

geekyme avatar Sep 28 '15 14:09 geekyme

@koulmomo

At the moment I attempt to solve this by wrapping around an action:

export default function provideActionParams(c, func, provider) {
  return function(context, params, done){
    const otherParams = provider(c);
    return func(context, { ...params, ...otherParams }, done);
  };
}

This can be used together with action.executeMultiple like this:

actionUtils.executeMultiple(context, {
      groups: [
        {
          action: provideActionParams(context, getGroups, function(context){
            return {
              countryCode: context.getStore("ApplicationStore").getLocation().get("country_code")
            };
          })
        }
      ]
});

geekyme avatar Sep 29 '15 08:09 geekyme