rpominov / fluce

[DEPRECATED] Flux with immutable data and pure action handlers
MIT License
76 stars 1 forks source link

Should we remove any API for action creators? #7

Closed rpominov closed 9 years ago

rpominov commented 9 years ago

Currently we have following API:

// Define:
let myActionCreator = (fluce) => {
  return (some, args) => {
    fluce.dispatch('actionName', payload);
  }
};

// Register:
const fluce = createFluce();
fluce.addActionCreator('actionCreatorName', myActionCreator);

// Call:
fluce.actions.actionCreatorName(some, args);

Wouldn't it be better if we simply didn't have any API:

// Define:
function myActionCreator(fluce, some, args) {
  fluce.dispatch('actionName', payload);
}

// Call:
myActionCreator(fluce, some, args);

The only disadvantage of removing the API is that with API programmer can stash some action related state in the closure, for instance:

let refreshFoo = (fluce) => {
  let latestRequest;
  return () => {
    fluce.dispatch('refreshFooStart');
    if (latestRequest !== undefined) {
      latestRequest.cancel();
    }
    latestRequest = myApi.getLatestFoo((foo) => {
      fluce.dispatch('refreshFooSuccess', foo);
      latestRequest = undefined;
    })
  }
};

This is a rare use case, and programmer will have following options:

// foo-actions.js

let latestRefreshRequest;
export refreshFoo(fluce) { /*...*/ }

Related: #6