priority-queue.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // EXPORTS
  40. __webpack_require__.d(__webpack_exports__, {
  41. "createQueue": function() { return /* binding */ createQueue; }
  42. });
  43. ;// CONCATENATED MODULE: ./node_modules/@wordpress/priority-queue/build-module/request-idle-callback.js
  44. /**
  45. * @typedef {( timeOrDeadline: IdleDeadline | number ) => void} Callback
  46. */
  47. /**
  48. * @return {(callback: Callback) => void} RequestIdleCallback
  49. */
  50. function createRequestIdleCallback() {
  51. if (typeof window === 'undefined') {
  52. return callback => {
  53. setTimeout(() => callback(Date.now()), 0);
  54. };
  55. }
  56. return window.requestIdleCallback || window.requestAnimationFrame;
  57. }
  58. /* harmony default export */ var request_idle_callback = (createRequestIdleCallback());
  59. ;// CONCATENATED MODULE: ./node_modules/@wordpress/priority-queue/build-module/index.js
  60. /**
  61. * Internal dependencies
  62. */
  63. /**
  64. * Enqueued callback to invoke once idle time permits.
  65. *
  66. * @typedef {()=>void} WPPriorityQueueCallback
  67. */
  68. /**
  69. * An object used to associate callbacks in a particular context grouping.
  70. *
  71. * @typedef {{}} WPPriorityQueueContext
  72. */
  73. /**
  74. * Function to add callback to priority queue.
  75. *
  76. * @typedef {(element:WPPriorityQueueContext,item:WPPriorityQueueCallback)=>void} WPPriorityQueueAdd
  77. */
  78. /**
  79. * Function to flush callbacks from priority queue.
  80. *
  81. * @typedef {(element:WPPriorityQueueContext)=>boolean} WPPriorityQueueFlush
  82. */
  83. /**
  84. * Reset the queue.
  85. *
  86. * @typedef {()=>void} WPPriorityQueueReset
  87. */
  88. /**
  89. * Priority queue instance.
  90. *
  91. * @typedef {Object} WPPriorityQueue
  92. *
  93. * @property {WPPriorityQueueAdd} add Add callback to queue for context.
  94. * @property {WPPriorityQueueFlush} flush Flush queue for context.
  95. * @property {WPPriorityQueueReset} reset Reset queue.
  96. */
  97. /**
  98. * Creates a context-aware queue that only executes
  99. * the last task of a given context.
  100. *
  101. * @example
  102. *```js
  103. * import { createQueue } from '@wordpress/priority-queue';
  104. *
  105. * const queue = createQueue();
  106. *
  107. * // Context objects.
  108. * const ctx1 = {};
  109. * const ctx2 = {};
  110. *
  111. * // For a given context in the queue, only the last callback is executed.
  112. * queue.add( ctx1, () => console.log( 'This will be printed first' ) );
  113. * queue.add( ctx2, () => console.log( 'This won\'t be printed' ) );
  114. * queue.add( ctx2, () => console.log( 'This will be printed second' ) );
  115. *```
  116. *
  117. * @return {WPPriorityQueue} Queue object with `add`, `flush` and `reset` methods.
  118. */
  119. const createQueue = () => {
  120. /** @type {WPPriorityQueueContext[]} */
  121. let waitingList = [];
  122. /** @type {WeakMap<WPPriorityQueueContext,WPPriorityQueueCallback>} */
  123. let elementsMap = new WeakMap();
  124. let isRunning = false;
  125. /**
  126. * Callback to process as much queue as time permits.
  127. *
  128. * @param {IdleDeadline|number} deadline Idle callback deadline object, or
  129. * animation frame timestamp.
  130. */
  131. const runWaitingList = deadline => {
  132. const hasTimeRemaining = typeof deadline === 'number' ? () => false : () => deadline.timeRemaining() > 0;
  133. do {
  134. if (waitingList.length === 0) {
  135. isRunning = false;
  136. return;
  137. }
  138. const nextElement =
  139. /** @type {WPPriorityQueueContext} */
  140. waitingList.shift();
  141. const callback =
  142. /** @type {WPPriorityQueueCallback} */
  143. elementsMap.get(nextElement); // If errors with undefined callbacks are encountered double check that all of your useSelect calls
  144. // have all dependecies set correctly in second parameter. Missing dependencies can cause unexpected
  145. // loops and race conditions in the queue.
  146. callback();
  147. elementsMap.delete(nextElement);
  148. } while (hasTimeRemaining());
  149. request_idle_callback(runWaitingList);
  150. };
  151. /**
  152. * Add a callback to the queue for a given context.
  153. *
  154. * @type {WPPriorityQueueAdd}
  155. *
  156. * @param {WPPriorityQueueContext} element Context object.
  157. * @param {WPPriorityQueueCallback} item Callback function.
  158. */
  159. const add = (element, item) => {
  160. if (!elementsMap.has(element)) {
  161. waitingList.push(element);
  162. }
  163. elementsMap.set(element, item);
  164. if (!isRunning) {
  165. isRunning = true;
  166. request_idle_callback(runWaitingList);
  167. }
  168. };
  169. /**
  170. * Flushes queue for a given context, returning true if the flush was
  171. * performed, or false if there is no queue for the given context.
  172. *
  173. * @type {WPPriorityQueueFlush}
  174. *
  175. * @param {WPPriorityQueueContext} element Context object.
  176. *
  177. * @return {boolean} Whether flush was performed.
  178. */
  179. const flush = element => {
  180. if (!elementsMap.has(element)) {
  181. return false;
  182. }
  183. const index = waitingList.indexOf(element);
  184. waitingList.splice(index, 1);
  185. const callback =
  186. /** @type {WPPriorityQueueCallback} */
  187. elementsMap.get(element);
  188. elementsMap.delete(element);
  189. callback();
  190. return true;
  191. };
  192. /**
  193. * Reset the queue without running the pending callbacks.
  194. *
  195. * @type {WPPriorityQueueReset}
  196. */
  197. const reset = () => {
  198. waitingList = [];
  199. elementsMap = new WeakMap();
  200. isRunning = false;
  201. };
  202. return {
  203. add,
  204. flush,
  205. reset
  206. };
  207. };
  208. (window.wp = window.wp || {}).priorityQueue = __webpack_exports__;
  209. /******/ })()
  210. ;