Redux: Retrieved Data for Items in a Collection

Here is a simplified version of my state tree:

{
    "radius": 8,
    "nodes": [
        { "id": 1, "x": 10, "y": 10 },
        { "id": 2, "x": 15, "y": 10 },
        { "id": 3, "x": 20, "y": 10 }
    ]
}

Essentially, I have a list of nodes, each of which has x and a y. I also have a radius number, which is used to calculate other nodes within a node radius, i.e. Close neighbors.

I need my condition to look like this:

{
    "radius": 8,
    "nodes": [
        { "id": 1, "x": 10, "y": 10, "neighbors": [2] },
        { "id": 2, "x": 15, "y": 10, "neighbors": [1, 3] },
        { "id": 3, "x": 20, "y": 10, "neighbors": [2] }
    ]
}

Calculation for neighbors is quite expensive, so I really want to calculate this if one of the node positions changes.

, , . node, . , , - node . , x y node. , , x y.

, , . ?

, . , ?

- ?

* EDIT

, . , /memoize.

, , , , . ( , memoized ...) , , .

?

* EDIT2 :

nodes: {
    nodesById: {
        "1": { "id": 1, "color": "blue", ... },
        "2": { "id": 2, "color": "red", ... },
        ...
    },
    positionsById: {
        "1": { "id": 1, "x": 0, "y": 10 },
        "2": { "id": 2, "x": 10, "y": 10 },
        ...
    }
}

, , :

export const getNeighborsById = createSelector(
    (positionsById, radius) => positionsById,
    (positionsById, radius) => radius,
    (positionsById, radius) => {
        // calculate neighbors
    }
);

, positionsById, nodesById ( ).

, , , , ...

+4
2

Redux:

  • JS. Wrong
  • , .

1.

Array, -, . , , , , node, neighbors. , - , .

-, (, ). DS node , .

2.

, , .

quardtree, .

+1

, memoized , . , Reselect.createSelector , , .

0

All Articles