Skip to content

Commit

Permalink
implement try/catch approach for dealing with zombie props
Browse files Browse the repository at this point in the history
  • Loading branch information
nerrad committed May 22, 2019
1 parent 0b4e037 commit e4d8a47
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions packages/data/src/components/use-select/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,27 @@ export default function useSelect( mapSelect ) {
const latestMapSelect = useRef();
const latestIsAsync = useRef( isAsync );
const latestMapOutput = useRef();
const latestMapOutputError = useRef();
const isMounted = useRef();

let mapOutput;

if ( latestMapSelect.current !== mapSelect ) {
mapOutput = mapSelect( registry.select, registry );
} else {
mapOutput = latestMapOutput.current;
try {
if ( latestMapSelect.current !== mapSelect || latestMapOutputError.current ) {
mapOutput = mapSelect( registry.select, registry );
} else {
mapOutput = latestMapOutput.current;
}
} catch ( error ) {
let errorMessage = `An error occurred while running 'mapSelect': ${ error.message }`;

if ( latestMapOutputError.current ) {
errorMessage += `\nThe error may be correlated with this previous error:\n`;
errorMessage += `${ latestMapOutputError.current.stack }\n\n`;
errorMessage += 'Original stack trace:';

throw new Error( errorMessage );
}
}

useIsomorphicLayoutEffect( () => {
Expand All @@ -56,17 +69,27 @@ export default function useSelect( mapSelect ) {
renderQueue.flush( queueContext );
}
latestMapOutput.current = mapOutput;
latestMapOutputError.current = undefined;
isMounted.current = true;
} );

useIsomorphicLayoutEffect( () => {
const onStoreChange = () => {
if ( isMounted.current ) {
const newMapOutput = latestMapSelect.current( registry.select, registry );
if ( ! isShallowEqualObjects( latestMapOutput.current, newMapOutput ) ) {
try {
const newMapOutput = latestMapSelect.current(
registry.select,
registry
);
if ( isShallowEqualObjects( latestMapOutput.current, newMapOutput ) ) {
return;
}
latestMapOutput.current = newMapOutput;
forceRender();
} catch ( error ) {
latestMapOutputError.current = error;
}

forceRender( {} );
}
};

Expand Down

0 comments on commit e4d8a47

Please sign in to comment.