[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/dist/ -> priority-queue.js (source)

   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: ./node_modules/@wordpress/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  
  64  ;// CONCATENATED MODULE: ./node_modules/@wordpress/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  
 238  (window.wp = window.wp || {}).priorityQueue = __webpack_exports__;
 239  /******/ })()
 240  ;


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1