Skip to content

Commit a3daea7

Browse files
committed
Core Data: Optimize getRawEntityRecord selector
1 parent f1a7bbf commit a3daea7

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

‎packages/core-data/src/selectors.ts‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -523,25 +523,26 @@ export const getRawEntityRecord = createSelector(
523523
name,
524524
key
525525
);
526-
return (
527-
record &&
528-
Object.keys( record ).reduce( ( accumulator, _key ) => {
529-
if (
530-
isRawAttribute( getEntityConfig( state, kind, name ), _key )
531-
) {
532-
// Because edits are the "raw" attribute values,
533-
// we return those from record selectors to make rendering,
534-
// comparisons, and joins with edits easier.
535-
accumulator[ _key ] =
536-
record[ _key ]?.raw !== undefined
537-
? record[ _key ]?.raw
538-
: record[ _key ];
539-
} else {
540-
accumulator[ _key ] = record[ _key ];
526+
const config = getEntityConfig( state, kind, name );
527+
if ( ! record || ! config?.rawAttributes?.length ) {
528+
return record;
529+
}
530+
531+
// Because edits are the "raw" attribute values,
532+
// we return those from record selectors to make rendering,
533+
// comparisons, and joins with edits easier.
534+
return Object.fromEntries(
535+
Object.keys( record ).map( ( _key ) => {
536+
if ( isRawAttribute( config, _key ) ) {
537+
const rawValue = record[ _key ]?.raw;
538+
return [
539+
_key,
540+
rawValue !== undefined ? rawValue : record[ _key ],
541+
];
541542
}
542-
return accumulator;
543-
}, {} as any )
544-
);
543+
return [ _key, record[ _key ] ];
544+
} )
545+
) as EntityRecord;
545546
},
546547
(
547548
state: State,

0 commit comments

Comments
 (0)