Skip to content

Commit

Permalink
Merge pull request #3275 from slecoustre/fix-performance-getFetchedAt
Browse files Browse the repository at this point in the history
optimize getFetchedAt.ts
  • Loading branch information
fzaninotto committed May 24, 2019
2 parents f45bf90 + cb717ba commit 71de47a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
20 changes: 7 additions & 13 deletions packages/ra-core/src/reducer/admin/resource/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,17 @@ export const addRecords = (
newRecords: Record[] = [],
oldRecords: RecordSetWithDate
): RecordSetWithDate => {
const newRecordsById = newRecords.reduce(
(acc, record) => ({
...acc,
[record.id]: record,
}),
{}
);
const newRecordsById = {};
newRecords.forEach(record => (newRecordsById[record.id] = record));

const newFetchedAt = getFetchedAt(
newRecords.map(({ id }) => id),
oldRecords.fetchedAt
);
const records = Object.keys(newFetchedAt).reduce(
(acc, id) => ({
...acc,
[id]: newRecordsById[id] || oldRecords[id],
}),
{ fetchedAt: newFetchedAt }

const records = { fetchedAt: newFetchedAt };
Object.keys(newFetchedAt).forEach(
id => (records[id] = newRecordsById[id] || oldRecords[id])
);

return hideFetchedAt(records);
Expand Down
10 changes: 3 additions & 7 deletions packages/ra-core/src/util/getFetchedAt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ const getFetchedAt = (
cacheDuration = defaultCacheDuration
): FetchedOutDates => {
// prepare new records and timestamp them
const newFetchedAt = newRecordIds.reduce(
(prev, recordId) => ({
...prev,
[recordId]: now,
}),
{}
);
const newFetchedAt = {};
newRecordIds.forEach(recordId => (newFetchedAt[recordId] = now));

// remove outdated entry
const latestValidDate = new Date();
latestValidDate.setTime(latestValidDate.getTime() - cacheDuration);
Expand Down

0 comments on commit 71de47a

Please sign in to comment.