Skip to content

Commit

Permalink
Force batching of GET_MANY response updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Aug 22, 2019
1 parent 6217ef6 commit c87f129
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions packages/ra-core/src/dataProvider/useGetMany.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useMemo } from 'react';
import ReactDOM from 'react-dom';
import { useSelector } from 'react-redux';
import { createSelector } from 'reselect';
import debounce from 'lodash/debounce';
Expand Down Expand Up @@ -175,27 +176,32 @@ const callQueries = debounce(() => {
{ ids: accumulatedIds },
{ action: CRUD_GET_MANY }
)
.then(response => {
queries.forEach(({ ids, setState, onSuccess }) => {
setState(prevState => ({
...prevState,
loading: false,
loaded: true,
}));
if (onSuccess) {
const subData = ids.map(id =>
response.data.find(datum => datum.id == id)
);
onSuccess({ data: subData });
}
});
})
.catch(error => {
queries.forEach(({ setState, onFailure }) => {
setState({ error, loading: false, loaded: false });
onFailure && onFailure(error);
});
});
.then(response =>
// Forces batching, see https://stackoverflow.com/questions/48563650/does-react-keep-the-order-for-state-updates/48610973#48610973
ReactDOM.unstable_batchedUpdates(() =>
queries.forEach(({ ids, setState, onSuccess }) => {
setState(prevState => ({
...prevState,
loading: false,
loaded: true,
}));
if (onSuccess) {
const subData = ids.map(id =>
response.data.find(datum => datum.id == id)
);
onSuccess({ data: subData });
}
})
)
)
.catch(error =>
ReactDOM.unstable_batchedUpdates(() =>
queries.forEach(({ setState, onFailure }) => {
setState({ error, loading: false, loaded: false });
onFailure && onFailure(error);
})
)
);
delete queriesToCall[resource];
});
});
Expand Down

0 comments on commit c87f129

Please sign in to comment.