@percy/core 1.11.0 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/queue.js CHANGED
@@ -1,25 +1,16 @@
1
1
  function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
2
-
3
2
  function _classPrivateFieldInitSpec(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); }
4
-
5
3
  function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
6
-
7
4
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
8
-
9
5
  function _classPrivateFieldSet(receiver, privateMap, value) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); _classApplyDescriptorSet(receiver, descriptor, value); return value; }
10
-
11
6
  function _classApplyDescriptorSet(receiver, descriptor, value) { if (descriptor.set) { descriptor.set.call(receiver, value); } else { if (!descriptor.writable) { throw new TypeError("attempted to set read only private field"); } descriptor.value = value; } }
12
-
13
7
  function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
14
-
15
8
  function _classPrivateFieldGet(receiver, privateMap) { var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); return _classApplyDescriptorGet(receiver, descriptor); }
16
-
17
9
  function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to " + action + " private field on non-instance"); } return privateMap.get(receiver); }
18
-
19
10
  function _classApplyDescriptorGet(receiver, descriptor) { if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; }
11
+ import { yieldFor, generatePromise, AbortController } from './utils.js';
20
12
 
21
- import { yieldFor, generatePromise, AbortController } from './utils.js'; // Assigns a deffered promise and resolve & reject functions to an object
22
-
13
+ // Assigns a deffered promise and resolve & reject functions to an object
23
14
  function deferred(obj) {
24
15
  return Object.assign(obj, {
25
16
  deferred: new Promise((resolve, reject) => {
@@ -29,338 +20,305 @@ function deferred(obj) {
29
20
  });
30
21
  })
31
22
  });
32
- } // Returns the position of a needle within a haystack, or undefined if not found
33
-
23
+ }
34
24
 
25
+ // Returns the position of a needle within a haystack, or undefined if not found
35
26
  function positionOf(haystack, needle, i = 1) {
36
27
  for (let item of haystack) {
37
28
  if (item !== needle) i++;else return i;
38
29
  }
39
- } // Thrown when attempting to push to a closed queue
40
-
30
+ }
41
31
 
32
+ // Thrown when attempting to push to a closed queue
42
33
  class QueueClosedError extends Error {
43
34
  name = this.constructor.name;
44
- } // A queue instance keeps a list of arbitrary items to process concurrently,
45
- // configured and controlled by various methods
46
-
35
+ }
47
36
 
37
+ // A queue instance keeps a list of arbitrary items to process concurrently,
38
+ // configured and controlled by various methods
48
39
  var _handlers = /*#__PURE__*/new WeakMap();
49
-
50
40
  var _queued = /*#__PURE__*/new WeakMap();
51
-
52
41
  var _pending = /*#__PURE__*/new WeakMap();
53
-
54
42
  var _dequeue = /*#__PURE__*/new WeakSet();
55
-
56
43
  var _find = /*#__PURE__*/new WeakSet();
57
-
58
44
  var _start = /*#__PURE__*/new WeakMap();
59
-
60
45
  var _end = /*#__PURE__*/new WeakMap();
61
-
62
46
  var _process = /*#__PURE__*/new WeakSet();
63
-
64
47
  var _until = /*#__PURE__*/new WeakSet();
65
-
66
48
  export class Queue {
67
49
  constructor() {
68
50
  _classPrivateMethodInitSpec(this, _until);
69
-
70
51
  _classPrivateMethodInitSpec(this, _process);
71
-
72
52
  _classPrivateMethodInitSpec(this, _find);
73
-
74
53
  _classPrivateMethodInitSpec(this, _dequeue);
75
-
76
54
  _defineProperty(this, "concurrency", 10);
77
-
78
55
  _classPrivateFieldInitSpec(this, _handlers, {
79
56
  writable: true,
80
57
  value: {}
81
58
  });
82
-
83
59
  _classPrivateFieldInitSpec(this, _queued, {
84
60
  writable: true,
85
61
  value: new Set()
86
62
  });
87
-
88
63
  _classPrivateFieldInitSpec(this, _pending, {
89
64
  writable: true,
90
65
  value: new Set()
91
66
  });
92
-
93
67
  _classPrivateFieldInitSpec(this, _start, {
94
68
  writable: true,
95
69
  value: null
96
70
  });
97
-
98
71
  _classPrivateFieldInitSpec(this, _end, {
99
72
  writable: true,
100
73
  value: null
101
74
  });
102
-
103
75
  _defineProperty(this, "readyState", 0);
104
76
  }
105
-
106
77
  // Configure queue properties
107
78
  set({
108
79
  concurrency
109
80
  }) {
110
81
  if (concurrency) this.concurrency = concurrency;
111
82
  return this;
112
- } // Configure queue handlers
83
+ }
113
84
 
85
+ // Configure queue handlers
114
86
 
115
87
  handle(event, handler) {
116
88
  _classPrivateFieldGet(this, _handlers)[event] = handler;
117
89
  return this;
118
- } // internal queues
90
+ }
119
91
 
92
+ // internal queues
120
93
 
121
94
  // Queue size is total queued and pending items
122
95
  get size() {
123
96
  return _classPrivateFieldGet(this, _queued).size + _classPrivateFieldGet(this, _pending).size;
124
- } // Pushes an item into the queue, additional args are passed to any configured task handler.
125
-
97
+ }
126
98
 
99
+ // Pushes an item into the queue, additional args are passed to any configured task handler.
127
100
  push(item, ...args) {
128
101
  let task = deferred({
129
102
  item
130
- }); // attach any configured error handler
103
+ });
131
104
 
105
+ // attach any configured error handler
132
106
  task.deferred = task.deferred.catch(e => {
133
107
  if (!_classPrivateFieldGet(this, _handlers).error) throw e;
134
108
  return _classPrivateFieldGet(this, _handlers).error(item, e);
135
- }); // when closed, reject with a queue closed error
109
+ });
136
110
 
111
+ // when closed, reject with a queue closed error
137
112
  if (this.readyState > 2) {
138
113
  task.reject(new QueueClosedError());
139
114
  return task.deferred;
140
- } // call or set up other handlers
141
-
115
+ }
142
116
 
117
+ // call or set up other handlers
143
118
  let exists = this.cancel(item);
144
119
  task.item = item = _classPrivateFieldGet(this, _handlers).push ? _classPrivateFieldGet(this, _handlers).push(item, exists) : item;
145
-
146
120
  task.handler = () => _classPrivateFieldGet(this, _handlers).task ? _classPrivateFieldGet(this, _handlers).task(item, ...args) : item;
121
+ task.ctrl = new AbortController();
147
122
 
148
- task.ctrl = new AbortController(); // queue this task & maybe dequeue the next task
149
-
123
+ // queue this task & maybe dequeue the next task
150
124
  _classPrivateFieldGet(this, _queued).add(task);
125
+ _classPrivateMethodGet(this, _dequeue, _dequeue2).call(this);
151
126
 
152
- _classPrivateMethodGet(this, _dequeue, _dequeue2).call(this); // return the deferred task promise
153
-
154
-
127
+ // return the deferred task promise
155
128
  return task.deferred;
156
- } // Maybe processes the next queued item task.
129
+ }
157
130
 
131
+ // Maybe processes the next queued item task.
158
132
 
159
133
  // Cancels and aborts a specific item task.
160
134
  cancel(item) {
161
135
  let task = _classPrivateMethodGet(this, _find, _find2).call(this, item);
162
-
163
136
  task === null || task === void 0 ? void 0 : task.ctrl.abort();
164
-
165
137
  let queued = _classPrivateFieldGet(this, _queued).delete(task);
138
+ let pending = _classPrivateFieldGet(this, _pending).delete(task);
166
139
 
167
- let pending = _classPrivateFieldGet(this, _pending).delete(task); // reject queued tasks that are not pending
168
-
169
-
140
+ // reject queued tasks that are not pending
170
141
  if (task && queued && !pending) {
171
142
  task.reject(task.ctrl.signal.reason);
172
- } // return the cancelled item
173
-
143
+ }
174
144
 
145
+ // return the cancelled item
175
146
  return task === null || task === void 0 ? void 0 : task.item;
176
- } // Returns an item task matching the provided subject.
147
+ }
177
148
 
149
+ // Returns an item task matching the provided subject.
178
150
 
179
151
  // Initialize a starting task or return an existing one.
180
152
  start() {
181
153
  var _classPrivateFieldGet2;
182
-
183
154
  _classPrivateFieldGet(this, _start) ?? _classPrivateFieldSet(this, _start, deferred({
184
155
  readyState: 1
185
156
  }));
186
157
  (_classPrivateFieldGet2 = _classPrivateFieldGet(this, _start)).handler ?? (_classPrivateFieldGet2.handler = _classPrivateFieldGet(this, _end) // wait for any ending task to complete first
187
158
  ? () => _classPrivateFieldGet(this, _end).promise.then(_classPrivateFieldGet(this, _handlers).start) : _classPrivateFieldGet(this, _handlers).start);
188
159
  return _classPrivateMethodGet(this, _process, _process2).call(this, _classPrivateFieldGet(this, _start)).deferred;
189
- } // intialize an ending task or return an existing one
190
-
160
+ }
191
161
 
162
+ // intialize an ending task or return an existing one
192
163
  end() {
193
164
  var _classPrivateFieldGet3;
194
-
195
165
  _classPrivateFieldGet(this, _end) ?? _classPrivateFieldSet(this, _end, deferred({
196
166
  readyState: 0
197
167
  }));
198
168
  (_classPrivateFieldGet3 = _classPrivateFieldGet(this, _end)).handler ?? (_classPrivateFieldGet3.handler = _classPrivateFieldGet(this, _start) // wait for any starting task to complete first
199
169
  ? () => _classPrivateFieldGet(this, _start).promise.then(_classPrivateFieldGet(this, _handlers).end) : _classPrivateFieldGet(this, _handlers).end);
200
170
  return _classPrivateMethodGet(this, _process, _process2).call(this, _classPrivateFieldGet(this, _end)).deferred;
201
- } // represents various queue states such as ready, running, or closed
171
+ }
202
172
 
173
+ // represents various queue states such as ready, running, or closed
203
174
 
204
175
  // run the queue, starting it if necessary, and start dequeuing tasks
205
176
  run() {
206
- if (!_classPrivateFieldGet(this, _start)) this.start(); // when starting, state is updated afterwards
207
-
177
+ if (!_classPrivateFieldGet(this, _start)) this.start();
178
+ // when starting, state is updated afterwards
208
179
  if (this.readyState === 0) _classPrivateFieldGet(this, _start).readyState = 2;
209
180
  if (this.readyState === 1) this.readyState = 2;
210
-
211
181
  while (_classPrivateMethodGet(this, _dequeue, _dequeue2).call(this)) _classPrivateMethodGet(this, _dequeue, _dequeue2).call(this);
212
-
213
182
  return this;
214
- } // stop a running queue
215
-
183
+ }
216
184
 
185
+ // stop a running queue
217
186
  stop() {
218
187
  if (this.readyState === 2) this.readyState = 1;
219
188
  return this;
220
- } // close a running queue, optionally aborting it
221
-
189
+ }
222
190
 
191
+ // close a running queue, optionally aborting it
223
192
  close(abort) {
224
193
  var _classPrivateFieldGet4;
225
-
226
194
  // when starting, state is updated afterwards
227
195
  if ((_classPrivateFieldGet4 = _classPrivateFieldGet(this, _start)) !== null && _classPrivateFieldGet4 !== void 0 && _classPrivateFieldGet4.pending) _classPrivateFieldGet(this, _start).readyState = 3;
228
196
  if (this.readyState < 3) this.readyState = 3;
229
197
  if (abort) this.clear();
230
198
  return this;
231
- } // clear and abort any queued tasks
232
-
199
+ }
233
200
 
201
+ // clear and abort any queued tasks
234
202
  clear() {
235
203
  let tasks = [..._classPrivateFieldGet(this, _queued)];
236
-
237
204
  _classPrivateFieldGet(this, _queued).clear();
238
-
239
205
  for (let task of tasks) {
240
206
  task.ctrl.abort();
241
207
  task.reject(task.ctrl.signal.reason);
242
208
  }
243
- } // process a single item task when started
244
-
209
+ }
245
210
 
211
+ // process a single item task when started
246
212
  process(item) {
247
213
  var _classPrivateFieldGet5;
248
-
249
214
  let task = _classPrivateMethodGet(this, _find, _find2).call(this, item);
250
-
251
215
  if (task && !_classPrivateFieldGet(this, _start)) this.start();
252
216
  (_classPrivateFieldGet5 = _classPrivateFieldGet(this, _start)) === null || _classPrivateFieldGet5 === void 0 ? void 0 : _classPrivateFieldGet5.promise.then(() => _classPrivateMethodGet(this, _process, _process2).call(this, task));
253
217
  return task === null || task === void 0 ? void 0 : task.deferred;
254
- } // processes tasks using a generator promise, allowing task handlers to be cancelable
218
+ }
255
219
 
220
+ // processes tasks using a generator promise, allowing task handlers to be cancelable
256
221
 
257
222
  // returns a generator that yeilds until started and no longer pending, calling the
258
223
  // callback every 10ms during checks with the current number of pending tasks
259
224
  idle(callback) {
260
225
  return yieldFor(() => {
261
226
  var _classPrivateFieldGet6;
262
-
263
227
  callback === null || callback === void 0 ? void 0 : callback(_classPrivateFieldGet(this, _pending).size);
264
228
  let starting = ((_classPrivateFieldGet6 = _classPrivateFieldGet(this, _start)) === null || _classPrivateFieldGet6 === void 0 ? void 0 : _classPrivateFieldGet6.pending) === true;
265
229
  return !starting && !_classPrivateFieldGet(this, _pending).size;
266
230
  }, {
267
231
  idle: 10
268
232
  });
269
- } // process items up to the latest queued item, starting the queue if necessary;
270
- // returns a generator that yields until the flushed item has finished processing
271
-
233
+ }
272
234
 
235
+ // process items up to the latest queued item, starting the queue if necessary;
236
+ // returns a generator that yields until the flushed item has finished processing
273
237
  flush(callback) {
274
- let interrupt = // check for existing interrupts
275
- [..._classPrivateFieldGet(this, _pending)].find(t => t.stop) ?? [..._classPrivateFieldGet(this, _queued)].find(t => t.stop); // get the latest queued or pending task to track
276
-
277
- let flush = [..._classPrivateFieldGet(this, _queued)].pop() ?? [..._classPrivateFieldGet(this, _pending)].pop(); // determine if the queue should be stopped after flushing
278
-
279
- if (flush) flush.stop = (interrupt === null || interrupt === void 0 ? void 0 : interrupt.stop) ?? this.readyState < 2; // remove the old interrupt to avoid stopping early
280
-
281
- if (interrupt) delete interrupt.stop; // start the queue if not started
282
-
283
- if (!_classPrivateFieldGet(this, _start)) this.start(); // run the queue if stopped
284
-
285
- if (flush !== null && flush !== void 0 && flush.stop) this.run(); // will yield with the callback until done flushing
286
-
238
+ let interrupt =
239
+ // check for existing interrupts
240
+ [..._classPrivateFieldGet(this, _pending)].find(t => t.stop) ?? [..._classPrivateFieldGet(this, _queued)].find(t => t.stop);
241
+
242
+ // get the latest queued or pending task to track
243
+ let flush = [..._classPrivateFieldGet(this, _queued)].pop() ?? [..._classPrivateFieldGet(this, _pending)].pop();
244
+ // determine if the queue should be stopped after flushing
245
+ if (flush) flush.stop = (interrupt === null || interrupt === void 0 ? void 0 : interrupt.stop) ?? this.readyState < 2;
246
+ // remove the old interrupt to avoid stopping early
247
+ if (interrupt) delete interrupt.stop;
248
+ // start the queue if not started
249
+ if (!_classPrivateFieldGet(this, _start)) this.start();
250
+ // run the queue if stopped
251
+ if (flush !== null && flush !== void 0 && flush.stop) this.run();
252
+
253
+ // will yield with the callback until done flushing
287
254
  return _classPrivateMethodGet(this, _until, _until2).call(this, flush, callback);
288
- } // Repeatedly yields, calling the callback with the position of the task within the queue
289
-
255
+ }
290
256
 
257
+ // Repeatedly yields, calling the callback with the position of the task within the queue
291
258
  }
292
-
293
259
  function _dequeue2() {
294
260
  if (!_classPrivateFieldGet(this, _queued).size || this.readyState < 2) return;
295
261
  if (_classPrivateFieldGet(this, _pending).size >= this.concurrency) return;
296
-
297
262
  let [task] = _classPrivateFieldGet(this, _queued);
298
-
299
263
  return _classPrivateMethodGet(this, _process, _process2).call(this, task);
300
264
  }
301
-
302
265
  function _find2(subject) {
303
- let find = _classPrivateFieldGet(this, _handlers).find // use any configured find handler to match items
266
+ let find = _classPrivateFieldGet(this, _handlers).find
267
+ // use any configured find handler to match items
304
268
  ? ({
305
269
  item
306
270
  }) => _classPrivateFieldGet(this, _handlers).find(subject, item) : ({
307
271
  item
308
272
  }) => subject === item;
309
- return (// look at queued then pending items
273
+ return (
274
+ // look at queued then pending items
310
275
  [..._classPrivateFieldGet(this, _queued)].find(find) ?? [..._classPrivateFieldGet(this, _pending)].find(find)
311
276
  );
312
277
  }
313
-
314
278
  function _process2(task) {
315
279
  var _task$ctrl;
316
-
317
280
  if (!task || task.promise) return task;
318
-
319
- let queued = _classPrivateFieldGet(this, _queued).has(task); // remove queued tasks from the queue
320
-
321
-
322
- if (queued) _classPrivateFieldGet(this, _queued).delete(task); // clear queued tasks when ending
323
-
324
- if (task === _classPrivateFieldGet(this, _end)) this.clear(); // add queued tasks to pending queue
325
-
326
- if (queued) _classPrivateFieldGet(this, _pending).add(task); // stop the queue when necessary
327
-
328
- if (task.stop) this.stop(); // mark task as pending
329
-
330
- task.pending = true; // handle the task using a generator promise
331
-
281
+ let queued = _classPrivateFieldGet(this, _queued).has(task);
282
+ // remove queued tasks from the queue
283
+ if (queued) _classPrivateFieldGet(this, _queued).delete(task);
284
+ // clear queued tasks when ending
285
+ if (task === _classPrivateFieldGet(this, _end)) this.clear();
286
+ // add queued tasks to pending queue
287
+ if (queued) _classPrivateFieldGet(this, _pending).add(task);
288
+ // stop the queue when necessary
289
+ if (task.stop) this.stop();
290
+ // mark task as pending
291
+ task.pending = true;
292
+
293
+ // handle the task using a generator promise
332
294
  task.promise = generatePromise(task.handler, (_task$ctrl = task.ctrl) === null || _task$ctrl === void 0 ? void 0 : _task$ctrl.signal, (err, val) => {
333
295
  // clean up pending tasks that have not been aborted
334
- if (queued && !task.ctrl.signal.aborted) _classPrivateFieldGet(this, _pending).delete(task); // update queue state when necessary
335
-
336
- if (task.readyState != null) this.readyState = task.readyState; // clean up internal tasks after ending
337
-
338
- if (!this.readyState) _classPrivateFieldSet(this, _start, _classPrivateFieldSet(this, _end, null)); // resolve or reject the deferred task promise
339
-
340
- task[err ? 'reject' : 'resolve'](err ?? val); // keep dequeuing when running
341
-
342
- if (this.readyState === 2) this.run(); // mark pending task done
343
-
296
+ if (queued && !task.ctrl.signal.aborted) _classPrivateFieldGet(this, _pending).delete(task);
297
+ // update queue state when necessary
298
+ if (task.readyState != null) this.readyState = task.readyState;
299
+ // clean up internal tasks after ending
300
+ if (!this.readyState) _classPrivateFieldSet(this, _start, _classPrivateFieldSet(this, _end, null));
301
+ // resolve or reject the deferred task promise
302
+ task[err ? 'reject' : 'resolve'](err ?? val);
303
+ // keep dequeuing when running
304
+ if (this.readyState === 2) this.run();
305
+ // mark pending task done
344
306
  task.pending = false;
345
307
  });
346
308
  return task;
347
309
  }
348
-
349
310
  async function* _until2(task, callback) {
350
311
  try {
351
312
  yield* yieldFor(() => {
352
313
  var _classPrivateFieldGet7;
353
-
354
314
  if ((_classPrivateFieldGet7 = _classPrivateFieldGet(this, _start)) !== null && _classPrivateFieldGet7 !== void 0 && _classPrivateFieldGet7.pending) return false;
355
-
356
315
  let queued,
357
- pending = _classPrivateFieldGet(this, _pending).size; // calculate the position within queued when not pending
358
-
359
-
360
- if (task && task.pending == null) queued = positionOf(_classPrivateFieldGet(this, _queued), task); // calculate the position within pending when not stopping
361
-
362
- if (!(task !== null && task !== void 0 && task.stop) && (task === null || task === void 0 ? void 0 : task.pending) != null) pending = positionOf(_classPrivateFieldGet(this, _pending), task); // call the callback and return true when not queued or pending
363
-
316
+ pending = _classPrivateFieldGet(this, _pending).size;
317
+ // calculate the position within queued when not pending
318
+ if (task && task.pending == null) queued = positionOf(_classPrivateFieldGet(this, _queued), task);
319
+ // calculate the position within pending when not stopping
320
+ if (!(task !== null && task !== void 0 && task.stop) && (task === null || task === void 0 ? void 0 : task.pending) != null) pending = positionOf(_classPrivateFieldGet(this, _pending), task);
321
+ // call the callback and return true when not queued or pending
364
322
  let position = (queued ?? 0) + (pending ?? 0);
365
323
  callback === null || callback === void 0 ? void 0 : callback(position);
366
324
  return !position;
@@ -374,5 +332,4 @@ async function* _until2(task, callback) {
374
332
  throw err;
375
333
  }
376
334
  }
377
-
378
335
  export default Queue;