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: try once setting #60460

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/priority-queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ queue.add( ctx2, () => console.log( "This won't be printed" ) );
queue.add( ctx2, () => console.log( 'This will be printed second' ) );
```

_Parameters_

- _options_ `Object`:
- _options.once_ `boolean`: Execute only a callback once per idle callback.

_Returns_

- `WPPriorityQueue`: Queue object with `add`, `flush` and `reset` methods.
Expand Down
6 changes: 5 additions & 1 deletion packages/priority-queue/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ import requestIdleCallback from './request-idle-callback';
* Creates a context-aware queue that only executes
* the last task of a given context.
*
* @param {Object} options
* @param {boolean} options.once Execute only a callback once per idle callback.
*
* @example
*```js
* import { createQueue } from '@wordpress/priority-queue';
Expand All @@ -66,7 +69,7 @@ import requestIdleCallback from './request-idle-callback';
*
* @return {WPPriorityQueue} Queue object with `add`, `flush` and `reset` methods.
*/
export const createQueue = () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

@youknowriad Do you agree with the naming? Should we call it something else? Or make it private?

Copy link
Contributor

Choose a reason for hiding this comment

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

The naming is fine by me and also public is fine. Should we pass the option as true for the "Async" component queue?

But I do wonder if it should be a global option like here or an option per callback (on the add function).

Copy link
Member Author

Choose a reason for hiding this comment

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

Whoops, reverted a bit too much in 9190404. Added it back.

I think a "global" option makes sense. I can't think of a case where you'd want to use it for some callbacks and not others? If you really need that separation, you could make two queues?

export const createQueue = ( { once } = { once: false } ) => {
/** @type {Map<WPPriorityQueueContext, WPPriorityQueueCallback>} */
const waitingList = new Map();
let isRunning = false;
Expand All @@ -93,6 +96,7 @@ export const createQueue = () => {
callback();

if (
once ||
'number' === typeof deadline ||
deadline.timeRemaining() <= 0
) {
Expand Down
Loading