Skip to content

Priority Queue: Invoke callback when flushing queue#19282

Merged
aduth merged 8 commits intomasterfrom
update/priority-queue-flush
Jan 16, 2020
Merged

Priority Queue: Invoke callback when flushing queue#19282
aduth merged 8 commits intomasterfrom
update/priority-queue-flush

Conversation

@aduth
Copy link
Copy Markdown
Member

@aduth aduth commented Dec 20, 2019

Related: #19199, #19205

This pull request seeks to update the behavior of the priority queue module to invoke a queued member's callback when flushing it from the waiting list. As noted at #13056 (comment), the previous behavior behaved more like a pure deletion than it did a flush, in that a flush would be expected to complete the deferred execution immediately.

Aside from being a desired behavior, it does not seem that this has an immediate benefit on the scenario described in #19199. While the callback is executed, the component still fails to re-render as expected. Interestingly, when stepping through the queue flush using the DevTools debugger, the render does occur. It's unclear to me why this inconsistency occurs.

Testing Instructions:

There are no unit tests for this module. If this is a desired solution, I could seek to include some.

Repeat Steps to Reproduce from #19199

@epiqueras
Copy link
Copy Markdown
Contributor

Looks great to me. Should we add a quick unit test to avoid regressions?

@aduth
Copy link
Copy Markdown
Member Author

aduth commented Jan 7, 2020

There weren't unit tests implemented at all for this module, so would have to build that out. I think it would be good to do, though unsure if it might be difficult based on the nature of how this module operates (i.e. with scheduling). I'll give it a look.

@aduth
Copy link
Copy Markdown
Member Author

aduth commented Jan 9, 2020

Pushed up some basic testing architecture for the package in 4fd5a79. Still need to flesh it out with test cases (unsure if I'll plan to add a full suite of tests, or just for the specific behavior implemented here).

@aduth
Copy link
Copy Markdown
Member Author

aduth commented Jan 15, 2020

Added test cases for new and existing functionality in f5368e6 and 84a7c73 respectively.

Copy link
Copy Markdown
Contributor

@epiqueras epiqueras left a comment

Choose a reason for hiding this comment

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

Great work here!

queue.flush( elementA );

// Only ElementA callback should have been called (synchronously).
expect( callbackElementA ).toHaveBeenCalledTimes( 1 );
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's kind of strange that we call this flush, but it doesn't flush a queue; it runs and removes a specific element from the queue.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's kind of strange that we call this flush, but it doesn't flush a queue; it runs and removes a specific element from the queue.

Yeah, that's a fair concern. In a way I might think it could be explained in that each context (element) from the outside behaves something like a queue of its own (or more like a single-entry stack) where the last-added callback is eventually invoked, but given how the waiting list is actually processed with a global "waiting list", it does seem to defy what one might expect from a flush here.

It makes me wonder whether there's even much value in having this key-by-element behavior, vs. just exposing the signature as add( callback ), and anywhere a distinct queue is needed, that implementation would simply create a new createQueue(). The only problem is that it creates a seperate scheduling for each queue, but I think this is something that would be reasonable to delegate to the default behavior of multiple requestIdleCallback (I expect they'd be processed in the order they're requested, for as long as time allows, in the same way the current implementation behaves).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In a way I might think it could be explained in that each context (element) from the outside behaves something like a queue of its own (or more like a single-entry stack) where the last-added callback is eventually invoked, but given how the waiting list is actually processed with a global "waiting list", it does seem to defy what one might expect from a flush here.

It's a queue of elements and a map of elements to callbacks.

It makes me wonder whether there's even much value in having this key-by-element behavior, vs. just exposing the signature as add( callback ), and anywhere a distinct queue is needed, that implementation would simply create a new createQueue().

That makes more sense.

The only problem is that it creates a seperate scheduling for each queue, but I think this is something that would be reasonable to delegate to the default behavior of multiple requestIdleCallback (I expect they'd be processed in the order they're requested, for as long as time allows, in the same way the current implementation behaves).

Yes, and it'd be easier to understand. This code wasn't entirely clear to me at first glance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Priority Queue /packages/priority-queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants