Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note to indicate why priority queue callbacks may be undefined #28337

Merged
merged 6 commits into from
Feb 10, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/priority-queue/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export const createQueue = () => {
const callback = /** @type {WPPriorityQueueCallback} */ ( elementsMap.get(
nextElement
) );
callback();

if ( typeof callback === 'function' ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another option would be a try/catch here instead, with a warning for isDev on catch so at least there is an indication that something is going through the queue without a callback

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in which case the callback won't be a valid function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youknowriad It turns out that in some cases if the callBack in runWaitingList takes a while to complete, the element can be added back to the waitList before elementsMap.delete is called. It then gets called before the new element in the waitingList goes through runWaitingList, so when it does there is no matching element in the elementsMap and the exception happens.

Screen Shot 2021-02-10 at 2 53 34 PM

I added some extra debugging in this PR, and you can see above callback being called for waitingList item 82, but then item 83 being added before the delete from element map happens for 82.

I have changed this PR to handle this situation by check that an element has not been added back to the waitList before deleting it from the element map, and this seems to fix the issue in the gallery refactor PR, but not sure if there are any other implications to handling this way.

callback();
}

elementsMap.delete( nextElement );
} while ( hasTimeRemaining() );

Expand Down