adrienjt / redux-data-structures

Reducer factory functions for common data structures: counters, maps, lists (queues, stacks), sets, etc.
https://redux-data-structures.js.org
MIT License
157 stars 4 forks source link

Feature: make getters generator functions #7

Open Romms opened 6 years ago

Romms commented 6 years ago

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 commented 6 years ago

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; },
    },
  ],
});
adrienjt commented 6 years ago

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.