flummox icon indicating copy to clipboard operation
flummox copied to clipboard

Lower-level actions primitive

Open acdlite opened this issue 9 years ago • 6 comments

At this point I'm really happy with Flummox's store API, its isomorphic capabilities, and its React integration story with FluxComponent. The one area that still feels like it needs some attention is actions.

The auto-dispatching and async stuff is solid. Speaking personally, it's super convenient and really avoids a lot of boilerplate. What I'm not sold on is the Actions class. It really only exists as a way to group actions together — which is convenient, but since one of Flummox's goals is to be highly extensible, I think a lower-level version of the same functionality should be provided.

Flux#dispatch() and Flux.dispatchAsync() already exist (though are undocumented) as a wrapper around the dispatcher. Theoretically this would be enough for a third-party library to build on top of. I think we can do better.

What I have in mind is a method Flux#createAction(actionId, actionCreator). This accepts an action id (constant in Flux parlance) and an action creator function, and returns an action. (Terminology is a bit confusing here... need to clear up somehow.) The action creator works the same way as methods on an Actions subclass currently do: the return value is automatically dispatched, and functions that return promises are treated as async actions.

We could continue providing the Actions class as a convenience and re-implement it using createAction(). This would provide a better way for other modules/projects to build on top of Flummox and add custom behavior.

acdlite avatar Mar 12 '15 03:03 acdlite

I think its nice to be able to specify the actionId but I almost wonder if its possible to just have Flux#createAction(actionCreator) as well.

tappleby avatar Mar 12 '15 04:03 tappleby

How would you retrieve the action id in that case?

acdlite avatar Mar 12 '15 04:03 acdlite

I was picturing the createAction to be similar to what the Actions#_wrapAction method is currently doing where you dont really need the raw (string) action id.

something like:

let action = flux.createAction((arg1, arg2) => { arg1, arg2 });

// Some where in store
this.register(action, this.handleAction);

// Call the action
action('foo', 'bar');

If you need the string actionId, we could have a Flux#getActionId(action) method.


After writing this I realized this method depends on injecting the created action everywhere. not that practical.

tappleby avatar Mar 12 '15 04:03 tappleby

Maybe add a method to actions that returns the id? Basically just exposes _id.

let action = flux.createAction((arg1, arg2) => { arg1, arg2 });
let actionId = action.getId();

acdlite avatar Mar 12 '15 05:03 acdlite

That could work.

only part Im not so sure about is when triggering an action from a component, currently I use something like:

let sessionActions = this.flux.getActions('core.session');
sessionActions.login(email, pass);

Would need a way to lookup the single action, which is where you want a user defined actionId.

tappleby avatar Mar 12 '15 05:03 tappleby

You could just pass the actionId directly to a new method Flux#getAction().

let login = this.flux.getAction('core.session.login'); // or whatever the action id is

But also this is meant to be low-level, so naturally you'll lose some of the convenience of using the Actions class.

acdlite avatar Mar 12 '15 05:03 acdlite