redux-data-structures icon indicating copy to clipboard operation
redux-data-structures copied to clipboard

Feature: make getters generator functions

Open Romms opened this issue 7 years ago • 2 comments

What about an idea to make getter methods be a generator function? It can allow us to do this for example:

const todos = map({
  addActionTypes: ['ADD_FEW_TODOS'],
  *keyGetter(action) {
    yield* action.payload.map(todo => todo.id);
  },
  *itemGetter(action) {
    yield* action.payload;
  },
});

I think it is an easy way to solve the problem described here https://github.com/adrienjt/redux-data-structures/issues/5#issuecomment-362779807

Romms avatar Mar 25 '18 14:03 Romms

This idea will be better if keep in mind this issue https://github.com/adrienjt/redux-data-structures/issues/8 what allow us to describe getters only for one action type. Than we can write like that

const todos = map({
  addActionTypes: [
    {
      todo: 'ADD_ONE_TODO',
      keyGetter: action => action.payload.uuid,
    },
    {
      todo: 'ADD_FEW_TODOS',
      * keyGetter(action) { yield* action.payload.map(todo => todo.uuid); },
      * itemGetter(action) { yield* action.payload; },
    },
  ],
});

Romms avatar Mar 25 '18 14:03 Romms

Those are great suggestions @Romms ! (this and #8) Getters would not only be specific to action types, but also to "sub-reducers" (add/change/remove). That would make the design more flexible, and we wouldn't have to add confusing parameters like manyItemsGetter in #6. However, simple use cases might become more verbose. I'll definitely consider your suggestions for v1. Thank you.

adrienjt avatar Mar 28 '18 19:03 adrienjt