Coder Social home page Coder Social logo

Comments (5)

adrienjt avatar adrienjt commented on May 13, 2024 1

Let's create an itemsModifier (note the plural) with your proposed signature for now, and I'll merge it with itemModifier in 1.0.0.

from redux-data-structures.

adrienjt avatar adrienjt commented on May 13, 2024

I see your point. Like you said, it's possible to add hundreds of items to a map at once by dispatching many actions. That's exactly what I do. I know it is not the most efficient way, but the performance impact has been negligible in my case, and a little for-loop in the client code isn't a big deal for me. It's kept the map API simple.

Now, I could see a situation where you already have an action with several items, which is reduced by another reducer, and you'd like to reduce that same action into a map, without having to dispatch new actions. Are you in that situation?

I can't promise that I'll implement this soon, so feel free to submit a PR. It would be greatly appreciated. I'd call it addMany... rather than addAll..., and I would also include a changeMany... and removeMany.... Also, how would you get the keys of the collection items?

Thank you for using redux-data-structures!

from redux-data-structures.

bsonntag avatar bsonntag commented on May 13, 2024

Well, I'm using redux-promise-middleware for API requests. I get a list of entities on the fulfilled action that I want to add to a map. I still want to dispatch the fulfilled action because of some request state reducers (isFetching and stuff like that) and it would be nice if that would be enough to add all entities to the map.

Also, how would you get the keys of the collection items?

I was thinking that keyGetter would help with that, but its default is action => action.payload.id, so that won't work. We'd need collectionKeyGetter (item => item.id by default) and maybe even collectionItemGetter (item => ({ ...item }) by default).

Example:

const users = map({
  addActionTypes: ['CREATE_USER_FULFILLED'],
  addManyActionTypes: ['FETCH_USERS_FULFILLED'],
  changeActionTypes: ['UPDATE_USER_FULFILLED'],
  removeActionTypes: ['DELETE_USER_FULFILLED'],
  collectionGetter: action => [...action.payload], // default
  collectionItemGetter: item => ({ ...item }), // default
  collectionKeyGetter: item => item.id, // default
  itemGetter: action => ({ ...action.payload }), // default
  itemModifier: (item, action) => ({ ...item, ...action.payload }), // default
  keyGetter: action => action.payload.id, // default
});

I'll submit a PR for this when I have time.

from redux-data-structures.

bsonntag avatar bsonntag commented on May 13, 2024

@adrienjt changeMany... isn't as easy as I first thought, because itemModifier takes the action as it's second argument. I'll have to add another option for modifying items from a collection.

What do you think about changing itemModifier to receive the result of itemGetter instead of action? Like this: itemModifier = (item, newItem) => ({ ...item, ...newItem }). This way I could easily implement changeMany... without adding another option. This would be a breaking change, though...

from redux-data-structures.

rsayn avatar rsayn commented on May 13, 2024

I had the same issue: I solved it by using the reduce-reducers library to create a flat-combined reducer with the map reducer and a custom reducer for multiple add/remove/replace handling.

Code

import { map } from "redux-data-structures";
import reduceReducers from "reduce-reducers";

const reducer = reduceReducers(map({...}), replaceAllReducer, addAllReducer, removeAllReducer);

By doing this, the "all" reducers inherit the state shape from map, obtaining the visibility they need to operate on byId and allIds.

It's a workaround, but it works for the time being.

from redux-data-structures.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.