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: set-up auto-generated API docs #14262

Merged
merged 2 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion bin/update-readmes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const packages = [
//'i18n',
//'keycodes',
//'plugins',
//'priority-queue',
'priority-queue',
//'redux-routine',
//'rich-text',
//'shortcode',
Expand Down
24 changes: 21 additions & 3 deletions packages/priority-queue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ Install the module
npm install @wordpress/priority-queue --save
```

_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using [core-js](https://github.com/zloirock/core-js) or [@babel/polyfill](https://babeljs.io/docs/en/next/babel-polyfill) will add support for these methods. Learn more about it in [Babel docs](https://babeljs.io/docs/en/next/caveats).
_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using [core-js](https://github.com/zloirock/core-js) or [@babel/polyfill](https://babeljs.io/docs/en/next/babel-polyfill) will add support for these methods. Learn more about it in [Babel docs](https://babeljs.io/docs/en/next/caveats)._

## Usage
## API

<!-- START TOKEN(Autogenerated API docs) -->

### createQueue

[src/index.js#L25-L72](src/index.js#L25-L72)

Creates a context-aware queue that only executes
the last task of a given context.

**Usage**

```js
import { createQueue } from '@wordpress/priority-queue';
Expand All @@ -21,12 +32,19 @@ const queue = createQueue();

// Context objects.
const ctx1 = {};
const ctx2 = {};
const ctx2 = {};

// For a given context in the queue, only the last callback is executed.
queue.add( ctx1, () => console.log( 'This will be printed first' ) );
queue.add( ctx2, () => console.log( 'This won\'t be printed' ) );
queue.add( ctx2, () => console.log( 'This will be printed second' ) );
```

**Returns**

`Object`: Queue object with `add` and `flush` methods.


<!-- END TOKEN(Autogenerated API docs) -->

<br/><br/><p align="center"><img src="https://s.w.org/style/images/codeispoetry.png?1" alt="Code is Poetry." /></p>
22 changes: 22 additions & 0 deletions packages/priority-queue/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,27 @@
const requestIdleCallback = window.requestIdleCallback ? window.requestIdleCallback : window.requestAnimationFrame;

/**
* Creates a context-aware queue that only executes
* the last task of a given context.
*
* @example
*```js
* import { createQueue } from '@wordpress/priority-queue';
*
* const queue = createQueue();
*
* // Context objects.
* const ctx1 = {};
* const ctx2 = {};
*
* // For a given context in the queue, only the last callback is executed.
* queue.add( ctx1, () => console.log( 'This will be printed first' ) );
* queue.add( ctx2, () => console.log( 'This won\'t be printed' ) );
* queue.add( ctx2, () => console.log( 'This will be printed second' ) );
*```
*
* @return {Object} Queue object with `add` and `flush` methods.
*/
export const createQueue = () => {
const waitingList = [];
const elementsMap = new WeakMap();
Expand Down