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

Priority Queue: Invoke callback when flushing queue #19282

Merged
merged 8 commits into from
Jan 16, 2020
12 changes: 8 additions & 4 deletions packages/priority-queue/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Internal dependencies
*/
import requestIdleCallback from './request-idle-callback';

/**
* Enqueued callback to invoke once idle time permits.
*
Expand Down Expand Up @@ -31,9 +36,6 @@
* @property {WPPriorityQueueFlush} flush Flush queue for context.
*/

/** @type {typeof window.requestIdleCallback|typeof window.requestAnimationFrame} */
const requestIdleCallback = window.requestIdleCallback ? window.requestIdleCallback : window.requestAnimationFrame;

/**
* Creates a context-aware queue that only executes
* the last task of a given context.
Expand Down Expand Up @@ -127,9 +129,11 @@ export const createQueue = () => {
return false;
}

elementsMap.delete( element );
const index = waitingList.indexOf( element );
waitingList.splice( index, 1 );
const callback = /** @type {WPPriorityQueueCallback} */ ( elementsMap.get( element ) );
elementsMap.delete( element );
callback();

return true;
};
Expand Down
14 changes: 14 additions & 0 deletions packages/priority-queue/src/request-idle-callback.js
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();
96 changes: 96 additions & 0 deletions packages/priority-queue/src/test/index.js
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 );
Copy link
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
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
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.

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 );
} );
} );
} );