kollapsminoriteten/wp-includes/js/dist/priority-queue.js

240 lines
6.6 KiB
JavaScript
Raw Normal View History

2022-06-16 14:03:35 +02:00
/******/ (function() { // webpackBootstrap
/******/ "use strict";
/******/ // The require scope
/******/ var __webpack_require__ = {};
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ !function() {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = function(exports, definition) {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ }();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ !function() {
/******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }
/******/ }();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ !function() {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
2019-11-02 10:38:58 +01:00
/******/ };
2022-06-16 14:03:35 +02:00
/******/ }();
/******/
2019-11-02 10:38:58 +01:00
/************************************************************************/
2022-06-16 14:03:35 +02:00
var __webpack_exports__ = {};
2020-09-15 14:29:22 +02:00
// ESM COMPAT FLAG
2019-11-02 10:38:58 +01:00
__webpack_require__.r(__webpack_exports__);
2020-05-06 17:23:38 +02:00
2020-09-15 14:29:22 +02:00
// EXPORTS
2022-06-16 14:03:35 +02:00
__webpack_require__.d(__webpack_exports__, {
"createQueue": function() { return /* binding */ createQueue; }
});
2020-09-15 14:29:22 +02:00
2022-06-16 14:03:35 +02:00
;// CONCATENATED MODULE: ./node_modules/@wordpress/priority-queue/build-module/request-idle-callback.js
2020-05-06 17:23:38 +02:00
/**
2020-09-15 14:29:22 +02:00
* @typedef {( timeOrDeadline: IdleDeadline | number ) => void} Callback
*/
/**
* @return {(callback: Callback) => void} RequestIdleCallback
2020-05-06 17:23:38 +02:00
*/
function createRequestIdleCallback() {
if (typeof window === 'undefined') {
2021-07-23 11:58:50 +02:00
return callback => {
setTimeout(() => callback(Date.now()), 0);
2020-05-06 17:23:38 +02:00
};
}
return window.requestIdleCallback || window.requestAnimationFrame;
}
/* harmony default export */ var request_idle_callback = (createRequestIdleCallback());
2022-06-16 14:03:35 +02:00
;// CONCATENATED MODULE: ./node_modules/@wordpress/priority-queue/build-module/index.js
2020-05-06 17:23:38 +02:00
/**
* Internal dependencies
*/
/**
* Enqueued callback to invoke once idle time permits.
*
* @typedef {()=>void} WPPriorityQueueCallback
*/
/**
* An object used to associate callbacks in a particular context grouping.
*
* @typedef {{}} WPPriorityQueueContext
*/
/**
* Function to add callback to priority queue.
*
* @typedef {(element:WPPriorityQueueContext,item:WPPriorityQueueCallback)=>void} WPPriorityQueueAdd
*/
/**
* Function to flush callbacks from priority queue.
*
* @typedef {(element:WPPriorityQueueContext)=>boolean} WPPriorityQueueFlush
*/
2020-09-15 14:29:22 +02:00
/**
* Reset the queue.
*
* @typedef {()=>void} WPPriorityQueueReset
*/
2020-05-06 17:23:38 +02:00
/**
* Priority queue instance.
*
* @typedef {Object} WPPriorityQueue
*
* @property {WPPriorityQueueAdd} add Add callback to queue for context.
* @property {WPPriorityQueueFlush} flush Flush queue for context.
2020-09-15 14:29:22 +02:00
* @property {WPPriorityQueueReset} reset Reset queue.
2020-05-06 17:23:38 +02:00
*/
2019-11-02 10:38:58 +01:00
/**
* 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' ) );
*```
*
2020-09-15 14:29:22 +02:00
* @return {WPPriorityQueue} Queue object with `add`, `flush` and `reset` methods.
2019-11-02 10:38:58 +01:00
*/
2021-07-23 11:58:50 +02:00
const createQueue = () => {
2020-05-06 17:23:38 +02:00
/** @type {WPPriorityQueueContext[]} */
2021-07-23 11:58:50 +02:00
let waitingList = [];
2020-05-06 17:23:38 +02:00
/** @type {WeakMap<WPPriorityQueueContext,WPPriorityQueueCallback>} */
2021-07-23 11:58:50 +02:00
let elementsMap = new WeakMap();
let isRunning = false;
2020-05-06 17:23:38 +02:00
/**
* Callback to process as much queue as time permits.
*
* @param {IdleDeadline|number} deadline Idle callback deadline object, or
* animation frame timestamp.
*/
2019-11-02 10:38:58 +01:00
2021-07-23 11:58:50 +02:00
const runWaitingList = deadline => {
const hasTimeRemaining = typeof deadline === 'number' ? () => false : () => deadline.timeRemaining() > 0;
2020-05-06 17:23:38 +02:00
2019-11-02 10:38:58 +01:00
do {
if (waitingList.length === 0) {
isRunning = false;
return;
}
2021-07-23 11:58:50 +02:00
const nextElement =
2020-05-06 17:23:38 +02:00
/** @type {WPPriorityQueueContext} */
waitingList.shift();
2021-07-23 11:58:50 +02:00
const callback =
2020-05-06 17:23:38 +02:00
/** @type {WPPriorityQueueCallback} */
2021-07-23 11:58:50 +02:00
elementsMap.get(nextElement); // If errors with undefined callbacks are encountered double check that all of your useSelect calls
// have all dependecies set correctly in second parameter. Missing dependencies can cause unexpected
// loops and race conditions in the queue.
2020-05-06 17:23:38 +02:00
callback();
2019-11-02 10:38:58 +01:00
elementsMap.delete(nextElement);
2020-05-06 17:23:38 +02:00
} while (hasTimeRemaining());
2019-11-02 10:38:58 +01:00
2020-05-06 17:23:38 +02:00
request_idle_callback(runWaitingList);
2019-11-02 10:38:58 +01:00
};
2020-05-06 17:23:38 +02:00
/**
* Add a callback to the queue for a given context.
*
* @type {WPPriorityQueueAdd}
*
* @param {WPPriorityQueueContext} element Context object.
* @param {WPPriorityQueueCallback} item Callback function.
*/
2019-11-02 10:38:58 +01:00
2021-07-23 11:58:50 +02:00
const add = (element, item) => {
2019-11-02 10:38:58 +01:00
if (!elementsMap.has(element)) {
waitingList.push(element);
}
elementsMap.set(element, item);
if (!isRunning) {
isRunning = true;
2020-05-06 17:23:38 +02:00
request_idle_callback(runWaitingList);
2019-11-02 10:38:58 +01:00
}
};
2020-05-06 17:23:38 +02:00
/**
* Flushes queue for a given context, returning true if the flush was
* performed, or false if there is no queue for the given context.
*
* @type {WPPriorityQueueFlush}
*
* @param {WPPriorityQueueContext} element Context object.
*
* @return {boolean} Whether flush was performed.
*/
2019-11-02 10:38:58 +01:00
2021-07-23 11:58:50 +02:00
const flush = element => {
2019-11-02 10:38:58 +01:00
if (!elementsMap.has(element)) {
return false;
}
2021-07-23 11:58:50 +02:00
const index = waitingList.indexOf(element);
2019-11-02 10:38:58 +01:00
waitingList.splice(index, 1);
2021-07-23 11:58:50 +02:00
const callback =
2020-05-06 17:23:38 +02:00
/** @type {WPPriorityQueueCallback} */
elementsMap.get(element);
elementsMap.delete(element);
callback();
2019-11-02 10:38:58 +01:00
return true;
};
2020-09-15 14:29:22 +02:00
/**
* Reset the queue without running the pending callbacks.
*
* @type {WPPriorityQueueReset}
*/
2021-07-23 11:58:50 +02:00
const reset = () => {
2020-09-15 14:29:22 +02:00
waitingList = [];
elementsMap = new WeakMap();
isRunning = false;
};
2019-11-02 10:38:58 +01:00
return {
2021-07-23 11:58:50 +02:00
add,
flush,
reset
2019-11-02 10:38:58 +01:00
};
};
2022-06-16 14:03:35 +02:00
(window.wp = window.wp || {}).priorityQueue = __webpack_exports__;
/******/ })()
;