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
Conversation
Size Change: +79.7 kB (+6%) 🔍 Total Size: 1.37 MB
ℹ️ View Unchanged
|
packages/priority-queue/src/index.js
Outdated
@@ -96,7 +96,11 @@ export const createQueue = () => { | |||
const callback = /** @type {WPPriorityQueueCallback} */ ( elementsMap.get( | |||
nextElement | |||
) ); | |||
callback(); | |||
|
|||
if ( typeof callback === 'function' ) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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.
@talldan, @ellatrix do you have any thoughts on why this queue might be getting an undefined callback in the gallery refactor, and if adding this defensive check is a good approach or not? Seems like even if we track down what is causing the undefined callback that it would be a good idea to prevent the queue dying in these instances. |
@glendaviesnz I don't know much about the priority queue package. Looking at the history, @youknowriad has authored some commits. |
I feel we should try to find the original issue and not hide it. The main usage of this package is for the async mode. The idea of the async mode is that useSelect callbacks are not batched for unselected blocks. |
@youknowriad I agree that it would be good to track down the reason why it ends up as undefined ... but I wasn't able to successfully debug it as I don't have a good enough understanding of how this queue is working. If you check out the Gallery Refactor PR add a gallery with some images, and then move a image within the gallery the error will be thrown ... any help you can provide in debugging this would be greatly appreciated as it is one of the last remaining bugs to sort on this refactor. |
…n't been added back to the waitlist before deleting from the element map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, LGTM
Description
In the Gallery block refactor, in some instances a callback is not defined in the waitingList method, eg. when moving or deleting blocks. This currently causes an exception which kills the queue and prevents any further actions that are dependent on it.
It turned out that this was due to a missing dependency on a useSelect call, which was not easy to track down, so this PR just adds a hint at the point of failure to help if people encounter this in the future.
Types of changes
Just adds a comment to help people with debugging if they encounter a similar error