PluginProbe
Gutenberg / 13.2.2
Gutenberg v13.2.2
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / priority-queue / index.js

index.js in Gutenberg 13.2.2, at build/priority-queue/index.js

265 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
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 {WPPriorityQueueFlush} cancel Clear queue for context.
107 * @property {WPPriorityQueueReset} reset Reset queue.
108 */
109
110 /**
111 * Creates a context-aware queue that only executes
112 * the last task of a given context.
113 *
114 * @example
115 *```js
116 * import { createQueue } from '@wordpress/priority-queue';
117 *
118 * const queue = createQueue();
119 *
120 * // Context objects.
121 * const ctx1 = {};
122 * const ctx2 = {};
123 *
124 * // For a given context in the queue, only the last callback is executed.
125 * queue.add( ctx1, () => console.log( 'This will be printed first' ) );
126 * queue.add( ctx2, () => console.log( 'This won\'t be printed' ) );
127 * queue.add( ctx2, () => console.log( 'This will be printed second' ) );
128 *```
129 *
130 * @return {WPPriorityQueue} Queue object with `add`, `flush` and `reset` methods.
131 */
132
133 const createQueue = () => {
134 /** @type {WPPriorityQueueContext[]} */
135 let waitingList = [];
136 /** @type {WeakMap<WPPriorityQueueContext,WPPriorityQueueCallback>} */
137
138 let elementsMap = new WeakMap();
139 let isRunning = false;
140 /**
141 * Callback to process as much queue as time permits.
142 *
143 * @param {IdleDeadline|number} deadline Idle callback deadline object, or
144 * animation frame timestamp.
145 */
146
147 const runWaitingList = deadline => {
148 const hasTimeRemaining = typeof deadline === 'number' ? () => false : () => deadline.timeRemaining() > 0;
149
150 do {
151 if (waitingList.length === 0) {
152 isRunning = false;
153 return;
154 }
155
156 const nextElement =
157 /** @type {WPPriorityQueueContext} */
158 waitingList.shift();
159 const callback =
160 /** @type {WPPriorityQueueCallback} */
161 elementsMap.get(nextElement); // If errors with undefined callbacks are encountered double check that all of your useSelect calls
162 // have all dependecies set correctly in second parameter. Missing dependencies can cause unexpected
163 // loops and race conditions in the queue.
164
165 callback();
166 elementsMap.delete(nextElement);
167 } while (hasTimeRemaining());
168
169 request_idle_callback(runWaitingList);
170 };
171 /**
172 * Add a callback to the queue for a given context.
173 *
174 * @type {WPPriorityQueueAdd}
175 *
176 * @param {WPPriorityQueueContext} element Context object.
177 * @param {WPPriorityQueueCallback} item Callback function.
178 */
179
180
181 const add = (element, item) => {
182 if (!elementsMap.has(element)) {
183 waitingList.push(element);
184 }
185
186 elementsMap.set(element, item);
187
188 if (!isRunning) {
189 isRunning = true;
190 request_idle_callback(runWaitingList);
191 }
192 };
193 /**
194 * Flushes queue for a given context, returning true if the flush was
195 * performed, or false if there is no queue for the given context.
196 *
197 * @type {WPPriorityQueueFlush}
198 *
199 * @param {WPPriorityQueueContext} element Context object.
200 *
201 * @return {boolean} Whether flush was performed.
202 */
203
204
205 const flush = element => {
206 if (!elementsMap.has(element)) {
207 return false;
208 }
209
210 const index = waitingList.indexOf(element);
211 waitingList.splice(index, 1);
212 const callback =
213 /** @type {WPPriorityQueueCallback} */
214 elementsMap.get(element);
215 elementsMap.delete(element);
216 callback();
217 return true;
218 };
219 /**
220 * Clears the queue for a given context, cancelling the callbacks without
221 * executing them. Returns `true` if there were scheduled callbacks to cancel,
222 * or `false` if there was is no queue for the given context.
223 *
224 * @type {WPPriorityQueueFlush}
225 *
226 * @param {WPPriorityQueueContext} element Context object.
227 *
228 * @return {boolean} Whether any callbacks got cancelled.
229 */
230
231
232 const cancel = element => {
233 if (!elementsMap.has(element)) {
234 return false;
235 }
236
237 const index = waitingList.indexOf(element);
238 waitingList.splice(index, 1);
239 elementsMap.delete(element);
240 return true;
241 };
242 /**
243 * Reset the queue without running the pending callbacks.
244 *
245 * @type {WPPriorityQueueReset}
246 */
247
248
249 const reset = () => {
250 waitingList = [];
251 elementsMap = new WeakMap();
252 isRunning = false;
253 };
254
255 return {
256 add,
257 flush,
258 cancel,
259 reset
260 };
261 };
262
263 (window.wp = window.wp || {}).priorityQueue = __webpack_exports__;
264 /******/ })()
265 ;