| @@ -1 +1,240 @@ | ||
| 1 | -this.wp=this.wp||{},this.wp.priorityQueue=function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=382)}({31:function(e,t,n){"use strict";function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}n.d(t,"a",(function(){return r}))},382:function(e,t,n){"use strict";n.r(t);var r=n(31);var o=void 0===Object(r.a)("window")?function(e){setTimeout((function(){return e(Date.now())}),0)}:window.requestIdleCallback||window.requestAnimationFrame;n.d(t,"createQueue",(function(){return u}));var u=function(){var e=[],t=new WeakMap,n=!1,r=function r(u){var i="number"==typeof u?function(){return!1}:function(){return u.timeRemaining()>0};do{if(0===e.length)return void(n=!1);var f=e.shift();t.get(f)(),t.delete(f)}while(i());o(r)};return{add:function(u,i){t.has(u)||e.push(u),t.set(u,i),n||(n=!0,o(r))},flush:function(n){if(!t.has(n))return!1;var r=e.indexOf(n);e.splice(r,1);var o=t.get(n);return t.delete(n),o(),!0}}}}}); | |
| 1 | +/******/ (function() { // webpackBootstrap | |
| 2 | +/******/ "use strict"; | |
| 3 | +/******/ // The require scope | |
| 4 | +/******/ var __webpack_require__ = {}; | |
| 5 | +/******/ | |
| 6 | +/************************************************************************/ | |
| 7 | +/******/ /* webpack/runtime/define property getters */ | |
| 8 | +/******/ !function() { | |
| 9 | +/******/ // define getter functions for harmony exports | |
| 10 | +/******/ __webpack_require__.d = function(exports, definition) { | |
| 11 | +/******/ for(var key in definition) { | |
| 12 | +/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { | |
| 13 | +/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); | |
| 14 | +/******/ } | |
| 15 | +/******/ } | |
| 16 | +/******/ }; | |
| 17 | +/******/ }(); | |
| 18 | +/******/ | |
| 19 | +/******/ /* webpack/runtime/hasOwnProperty shorthand */ | |
| 20 | +/******/ !function() { | |
| 21 | +/******/ __webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); } | |
| 22 | +/******/ }(); | |
| 23 | +/******/ | |
| 24 | +/******/ /* webpack/runtime/make namespace object */ | |
| 25 | +/******/ !function() { | |
| 26 | +/******/ // define __esModule on exports | |
| 27 | +/******/ __webpack_require__.r = function(exports) { | |
| 28 | +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { | |
| 29 | +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); | |
| 30 | +/******/ } | |
| 31 | +/******/ Object.defineProperty(exports, '__esModule', { value: true }); | |
| 32 | +/******/ }; | |
| 33 | +/******/ }(); | |
| 34 | +/******/ | |
| 35 | +/************************************************************************/ | |
| 36 | +var __webpack_exports__ = {}; | |
| 37 | +// ESM COMPAT FLAG | |
| 38 | +__webpack_require__.r(__webpack_exports__); | |
| 39 | + | |
| 40 | +// EXPORTS | |
| 41 | +__webpack_require__.d(__webpack_exports__, { | |
| 42 | + "createQueue": function() { return /* binding */ createQueue; } | |
| 43 | +}); | |
| 44 | + | |
| 45 | +;// CONCATENATED MODULE: ./packages/priority-queue/build-module/request-idle-callback.js | |
| 46 | +/** | |
| 47 | + * @typedef {( timeOrDeadline: IdleDeadline | number ) => void} Callback | |
| 48 | + */ | |
| 49 | + | |
| 50 | +/** | |
| 51 | + * @return {(callback: Callback) => void} RequestIdleCallback | |
| 52 | + */ | |
| 53 | +function createRequestIdleCallback() { | |
| 54 | + if (typeof window === 'undefined') { | |
| 55 | + return callback => { | |
| 56 | + setTimeout(() => callback(Date.now()), 0); | |
| 57 | + }; | |
| 58 | + } | |
| 59 | + | |
| 60 | + return window.requestIdleCallback || window.requestAnimationFrame; | |
| 61 | +} | |
| 62 | +/* harmony default export */ var request_idle_callback = (createRequestIdleCallback()); | |
| 63 | +//# sourceMappingURL=request-idle-callback.js.map | |
| 64 | +;// CONCATENATED MODULE: ./packages/priority-queue/build-module/index.js | |
| 65 | +/** | |
| 66 | + * Internal dependencies | |
| 67 | + */ | |
| 68 | + | |
| 69 | +/** | |
| 70 | + * Enqueued callback to invoke once idle time permits. | |
| 71 | + * | |
| 72 | + * @typedef {()=>void} WPPriorityQueueCallback | |
| 73 | + */ | |
| 74 | + | |
| 75 | +/** | |
| 76 | + * An object used to associate callbacks in a particular context grouping. | |
| 77 | + * | |
| 78 | + * @typedef {{}} WPPriorityQueueContext | |
| 79 | + */ | |
| 80 | + | |
| 81 | +/** | |
| 82 | + * Function to add callback to priority queue. | |
| 83 | + * | |
| 84 | + * @typedef {(element:WPPriorityQueueContext,item:WPPriorityQueueCallback)=>void} WPPriorityQueueAdd | |
| 85 | + */ | |
| 86 | + | |
| 87 | +/** | |
| 88 | + * Function to flush callbacks from priority queue. | |
| 89 | + * | |
| 90 | + * @typedef {(element:WPPriorityQueueContext)=>boolean} WPPriorityQueueFlush | |
| 91 | + */ | |
| 92 | + | |
| 93 | +/** | |
| 94 | + * Reset the queue. | |
| 95 | + * | |
| 96 | + * @typedef {()=>void} WPPriorityQueueReset | |
| 97 | + */ | |
| 98 | + | |
| 99 | +/** | |
| 100 | + * Priority queue instance. | |
| 101 | + * | |
| 102 | + * @typedef {Object} WPPriorityQueue | |
| 103 | + * | |
| 104 | + * @property {WPPriorityQueueAdd} add Add callback to queue for context. | |
| 105 | + * @property {WPPriorityQueueFlush} flush Flush queue for context. | |
| 106 | + * @property {WPPriorityQueueReset} reset Reset queue. | |
| 107 | + */ | |
| 108 | + | |
| 109 | +/** | |
| 110 | + * Creates a context-aware queue that only executes | |
| 111 | + * the last task of a given context. | |
| 112 | + * | |
| 113 | + * @example | |
| 114 | + *```js | |
| 115 | + * import { createQueue } from '@wordpress/priority-queue'; | |
| 116 | + * | |
| 117 | + * const queue = createQueue(); | |
| 118 | + * | |
| 119 | + * // Context objects. | |
| 120 | + * const ctx1 = {}; | |
| 121 | + * const ctx2 = {}; | |
| 122 | + * | |
| 123 | + * // For a given context in the queue, only the last callback is executed. | |
| 124 | + * queue.add( ctx1, () => console.log( 'This will be printed first' ) ); | |
| 125 | + * queue.add( ctx2, () => console.log( 'This won\'t be printed' ) ); | |
| 126 | + * queue.add( ctx2, () => console.log( 'This will be printed second' ) ); | |
| 127 | + *``` | |
| 128 | + * | |
| 129 | + * @return {WPPriorityQueue} Queue object with `add`, `flush` and `reset` methods. | |
| 130 | + */ | |
| 131 | + | |
| 132 | +const createQueue = () => { | |
| 133 | + /** @type {WPPriorityQueueContext[]} */ | |
| 134 | + let waitingList = []; | |
| 135 | + /** @type {WeakMap<WPPriorityQueueContext,WPPriorityQueueCallback>} */ | |
| 136 | + | |
| 137 | + let elementsMap = new WeakMap(); | |
| 138 | + let isRunning = false; | |
| 139 | + /** | |
| 140 | + * Callback to process as much queue as time permits. | |
| 141 | + * | |
| 142 | + * @param {IdleDeadline|number} deadline Idle callback deadline object, or | |
| 143 | + * animation frame timestamp. | |
| 144 | + */ | |
| 145 | + | |
| 146 | + const runWaitingList = deadline => { | |
| 147 | + const hasTimeRemaining = typeof deadline === 'number' ? () => false : () => deadline.timeRemaining() > 0; | |
| 148 | + | |
| 149 | + do { | |
| 150 | + if (waitingList.length === 0) { | |
| 151 | + isRunning = false; | |
| 152 | + return; | |
| 153 | + } | |
| 154 | + | |
| 155 | + const nextElement = | |
| 156 | + /** @type {WPPriorityQueueContext} */ | |
| 157 | + waitingList.shift(); | |
| 158 | + const callback = | |
| 159 | + /** @type {WPPriorityQueueCallback} */ | |
| 160 | + elementsMap.get(nextElement); // If errors with undefined callbacks are encountered double check that all of your useSelect calls | |
| 161 | + // have all dependecies set correctly in second parameter. Missing dependencies can cause unexpected | |
| 162 | + // loops and race conditions in the queue. | |
| 163 | + | |
| 164 | + callback(); | |
| 165 | + elementsMap.delete(nextElement); | |
| 166 | + } while (hasTimeRemaining()); | |
| 167 | + | |
| 168 | + request_idle_callback(runWaitingList); | |
| 169 | + }; | |
| 170 | + /** | |
| 171 | + * Add a callback to the queue for a given context. | |
| 172 | + * | |
| 173 | + * @type {WPPriorityQueueAdd} | |
| 174 | + * | |
| 175 | + * @param {WPPriorityQueueContext} element Context object. | |
| 176 | + * @param {WPPriorityQueueCallback} item Callback function. | |
| 177 | + */ | |
| 178 | + | |
| 179 | + | |
| 180 | + const add = (element, item) => { | |
| 181 | + if (!elementsMap.has(element)) { | |
| 182 | + waitingList.push(element); | |
| 183 | + } | |
| 184 | + | |
| 185 | + elementsMap.set(element, item); | |
| 186 | + | |
| 187 | + if (!isRunning) { | |
| 188 | + isRunning = true; | |
| 189 | + request_idle_callback(runWaitingList); | |
| 190 | + } | |
| 191 | + }; | |
| 192 | + /** | |
| 193 | + * Flushes queue for a given context, returning true if the flush was | |
| 194 | + * performed, or false if there is no queue for the given context. | |
| 195 | + * | |
| 196 | + * @type {WPPriorityQueueFlush} | |
| 197 | + * | |
| 198 | + * @param {WPPriorityQueueContext} element Context object. | |
| 199 | + * | |
| 200 | + * @return {boolean} Whether flush was performed. | |
| 201 | + */ | |
| 202 | + | |
| 203 | + | |
| 204 | + const flush = element => { | |
| 205 | + if (!elementsMap.has(element)) { | |
| 206 | + return false; | |
| 207 | + } | |
| 208 | + | |
| 209 | + const index = waitingList.indexOf(element); | |
| 210 | + waitingList.splice(index, 1); | |
| 211 | + const callback = | |
| 212 | + /** @type {WPPriorityQueueCallback} */ | |
| 213 | + elementsMap.get(element); | |
| 214 | + elementsMap.delete(element); | |
| 215 | + callback(); | |
| 216 | + return true; | |
| 217 | + }; | |
| 218 | + /** | |
| 219 | + * Reset the queue without running the pending callbacks. | |
| 220 | + * | |
| 221 | + * @type {WPPriorityQueueReset} | |
| 222 | + */ | |
| 223 | + | |
| 224 | + | |
| 225 | + const reset = () => { | |
| 226 | + waitingList = []; | |
| 227 | + elementsMap = new WeakMap(); | |
| 228 | + isRunning = false; | |
| 229 | + }; | |
| 230 | + | |
| 231 | + return { | |
| 232 | + add, | |
| 233 | + flush, | |
| 234 | + reset | |
| 235 | + }; | |
| 236 | +}; | |
| 237 | +//# sourceMappingURL=index.js.map | |
| 238 | +(window.wp = window.wp || {}).priorityQueue = __webpack_exports__; | |
| 239 | +/******/ })() | |
| 240 | +; | |