Platform: RFC store: ActionCreator, ability to add context dynamically

Created on 29 Feb 2020  路  1Comment  路  Source: ngrx/platform

According to #1734 there is no consistent way to reuse actions without some "extra job" and compromises.

Possible solution

Add an extra field to Action interface, something like baseType.
For empty and props ActionCreators add an ability to pass an optional argument.
As a result of creator's function call, we should have an object like this:

{
   type: context + type,
   baseType: type
}

For compare incoming actions on and ofType should use baseType at first and a type as fallback

Example of expected result

const foo = createAction('FOO');
const bar = createAction('BAR', props<{foo: number}>());

...
createReducer(
  initialState,
  on(foo, bar, (state) => ({...state, modifyed: true}))
);
....
createEffect(() =>
  this.actions$.pipe(
    ofType(foo, bar),
    switchMap(_ => this.foobar())
  ),
  { dispatch: false }
);
...
this.store.dispatch(foo());
this.store.dispatch(foo('FOO CONTEXT'));
this.store.dispatch(bar({foo: 33}));
this.store.dispacth(bar({foo: 22}, 'BAR CONTEXT'));

I expect that reducer and effect will trigger for every action and action log looks like

[FOO CONTEXT]FOO
BAR
[BAR CONTEXT]BAR

If accepted, I would be willing to submit a PR for this feature

[x] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No

Most helpful comment

@vmotornyi thanks for the issue. We are encouraging not to reuse actions. It is very easy to create them.
Also, we do not plan to split the type and will continue to _log_ (in redux-devtools) and _handle_ actions (in effects/reducers) by the same type property.

Personally, I feel it would be very very hard to track the actions with the split.

>All comments

@vmotornyi thanks for the issue. We are encouraging not to reuse actions. It is very easy to create them.
Also, we do not plan to split the type and will continue to _log_ (in redux-devtools) and _handle_ actions (in effects/reducers) by the same type property.

Personally, I feel it would be very very hard to track the actions with the split.

Was this page helpful?
0 / 5 - 0 ratings