-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Priority Queue: Invoke callback when flushing queue #19282
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
78af8f3
Priority Queue: Invoke callback when flushing queue
aduth 4fd5a79
Priority Queue: Implement basic testing architecture
aduth 338ea72
Priority Queue: Update tests to self-contain mocked requestIdleCallback
aduth 2c02ba4
Priority Queue: Assert callback not called before tick
aduth f5368e6
Priority Queue: Add test case to verify flush callback invoked
aduth 84a7c73
Priority Queue: Verify callback call order of added items
aduth 0931177
Priority Queue: Add test case for idle deadline implementation
aduth a044b60
Priority Queue: Add CHANGELOG entry for flush callback invoked
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* @return {typeof window.requestIdleCallback|typeof window.requestAnimationFrame|((callback:(timestamp:number)=>void)=>void)} | ||
*/ | ||
export function createRequestIdleCallback() { | ||
if ( typeof 'window' === undefined ) { | ||
return ( callback ) => { | ||
setTimeout( () => callback( Date.now() ), 0 ); | ||
}; | ||
} | ||
|
||
return window.requestIdleCallback || window.requestAnimationFrame; | ||
} | ||
|
||
export default createRequestIdleCallback(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { createQueue } from '../'; | ||
import requestIdleCallback from '../request-idle-callback'; | ||
|
||
jest.mock( '../request-idle-callback', () => { | ||
const emitter = new ( require.requireActual( 'events' ).EventEmitter )(); | ||
|
||
return Object.assign( | ||
( callback ) => emitter.once( 'tick', () => callback( Date.now() ) ), | ||
{ tick: () => emitter.emit( 'tick' ) }, | ||
); | ||
} ); | ||
|
||
describe( 'createQueue', () => { | ||
let queue; | ||
beforeEach( () => { | ||
queue = createQueue(); | ||
} ); | ||
|
||
describe( 'add', () => { | ||
it( 'runs callback after processing waiting queue', () => { | ||
const callback = jest.fn(); | ||
|
||
queue.add( {}, callback ); | ||
|
||
expect( callback ).not.toHaveBeenCalled(); | ||
requestIdleCallback.tick(); | ||
expect( callback ).toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'runs callbacks in order by distinct added element', () => { | ||
const elementA = {}; | ||
const elementB = {}; | ||
const callbackElementA = jest.fn(); | ||
const callbackElementB = jest.fn(); | ||
queue.add( elementA, callbackElementA ); | ||
queue.add( elementB, callbackElementB ); | ||
|
||
expect( callbackElementA ).not.toHaveBeenCalled(); | ||
expect( callbackElementB ).not.toHaveBeenCalled(); | ||
|
||
// ElementA was added first, and should be called first after tick. | ||
requestIdleCallback.tick(); | ||
expect( callbackElementA ).toHaveBeenCalledTimes( 1 ); | ||
expect( callbackElementB ).not.toHaveBeenCalled(); | ||
|
||
// ElementB will be be processed after second tick. | ||
requestIdleCallback.tick(); | ||
expect( callbackElementA ).toHaveBeenCalledTimes( 1 ); | ||
expect( callbackElementB ).toHaveBeenCalledTimes( 1 ); | ||
} ); | ||
|
||
it( 'calls most recently added callback if added for same element', () => { | ||
const element = {}; | ||
const callbackOne = jest.fn(); | ||
const callbackTwo = jest.fn(); | ||
queue.add( element, callbackOne ); | ||
queue.add( element, callbackTwo ); | ||
|
||
expect( callbackOne ).not.toHaveBeenCalled(); | ||
expect( callbackTwo ).not.toHaveBeenCalled(); | ||
|
||
requestIdleCallback.tick(); | ||
expect( callbackOne ).not.toHaveBeenCalled(); | ||
expect( callbackTwo ).toHaveBeenCalledTimes( 1 ); | ||
} ); | ||
} ); | ||
|
||
describe( 'flush', () => { | ||
it( 'invokes all callbacks associated with element', () => { | ||
const elementA = {}; | ||
const elementB = {}; | ||
const callbackElementA = jest.fn(); | ||
const callbackElementB = jest.fn(); | ||
queue.add( elementA, callbackElementA ); | ||
queue.add( elementB, callbackElementB ); | ||
|
||
expect( callbackElementA ).not.toHaveBeenCalled(); | ||
expect( callbackElementB ).not.toHaveBeenCalled(); | ||
|
||
queue.flush( elementA ); | ||
|
||
// Only ElementA callback should have been called (synchronously). | ||
expect( callbackElementA ).toHaveBeenCalledTimes( 1 ); | ||
expect( callbackElementB ).not.toHaveBeenCalled(); | ||
|
||
// Verify that callback still called only once after tick (verify | ||
// removal). | ||
requestIdleCallback.tick(); | ||
expect( callbackElementA ).toHaveBeenCalledTimes( 1 ); | ||
expect( callbackElementB ).toHaveBeenCalledTimes( 1 ); | ||
} ); | ||
} ); | ||
} ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.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.
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 asadd( callback )
, and anywhere a distinct queue is needed, that implementation would simply create a newcreateQueue()
. 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 multiplerequestIdleCallback
(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).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.
It's a queue of elements and a map of elements to callbacks.
That makes more sense.
Yes, and it'd be easier to understand. This code wasn't entirely clear to me at first glance.