@salesforce/lds-utils-adapters 1.103.0 → 1.104.1
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/AsyncWorkerPool.d.ts +8 -4
- package/dist/ldsAdapterUtils.js +41 -16
- package/package.json +1 -1
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
export interface Work<T> {
|
|
2
|
+
workFn: () => Promise<T>;
|
|
3
|
+
cancelFn?: () => Promise<void>;
|
|
4
|
+
}
|
|
1
5
|
export declare class AsyncWorkerPool {
|
|
2
6
|
private concurrency;
|
|
3
7
|
private queue;
|
|
4
|
-
private
|
|
8
|
+
private activeWork;
|
|
5
9
|
constructor(concurrency: number);
|
|
6
|
-
push<T>(
|
|
7
|
-
cancel(): void
|
|
8
|
-
private
|
|
10
|
+
push<T>(work: Work<T>): Promise<T>;
|
|
11
|
+
cancel(): Promise<void>;
|
|
12
|
+
private doWork;
|
|
9
13
|
}
|
package/dist/ldsAdapterUtils.js
CHANGED
|
@@ -306,33 +306,58 @@ function runAdapterWithReport(adapterName, adapter, adapterConfig, requestContex
|
|
|
306
306
|
class AsyncWorkerPool {
|
|
307
307
|
constructor(concurrency) {
|
|
308
308
|
this.queue = [];
|
|
309
|
-
this.
|
|
309
|
+
this.activeWork = [];
|
|
310
310
|
this.concurrency = concurrency;
|
|
311
311
|
}
|
|
312
|
-
push(
|
|
312
|
+
push(work) {
|
|
313
313
|
return new Promise((resolve, reject) => {
|
|
314
|
-
this.queue.push(
|
|
315
|
-
|
|
314
|
+
this.queue.push({
|
|
315
|
+
...work,
|
|
316
|
+
workFn: () => work.workFn().then(resolve).catch(reject),
|
|
316
317
|
});
|
|
317
|
-
this.
|
|
318
|
+
this.doWork();
|
|
318
319
|
});
|
|
319
320
|
}
|
|
320
|
-
cancel() {
|
|
321
|
-
|
|
322
|
-
|
|
321
|
+
async cancel() {
|
|
322
|
+
const promises = [];
|
|
323
|
+
const cancellingWork = [
|
|
324
|
+
...this.activeWork.splice(0, this.activeWork.length),
|
|
325
|
+
...this.queue.splice(0, this.queue.length),
|
|
326
|
+
];
|
|
327
|
+
cancellingWork.forEach((work) => {
|
|
328
|
+
const { cancelFn } = work;
|
|
329
|
+
if (cancelFn) {
|
|
330
|
+
promises.push(cancelFn());
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
await promiseAllSettled(promises);
|
|
323
334
|
}
|
|
324
|
-
|
|
325
|
-
while (this.queue.length > 0 && this.
|
|
326
|
-
this.
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
335
|
+
doWork() {
|
|
336
|
+
while (this.queue.length > 0 && this.activeWork.length < this.concurrency) {
|
|
337
|
+
const work = this.queue.shift();
|
|
338
|
+
if (work) {
|
|
339
|
+
this.activeWork.push(work);
|
|
340
|
+
const { workFn } = work;
|
|
341
|
+
workFn()
|
|
342
|
+
.then()
|
|
343
|
+
.finally(() => {
|
|
344
|
+
this.activeWork = this.activeWork.filter((w) => w !== work);
|
|
345
|
+
this.doWork();
|
|
332
346
|
});
|
|
333
347
|
}
|
|
334
348
|
}
|
|
335
349
|
}
|
|
350
|
+
}
|
|
351
|
+
function promiseAllSettled(promises) {
|
|
352
|
+
return Promise.all(promises.map((promise) => promise
|
|
353
|
+
.then((value) => ({
|
|
354
|
+
status: 'fulfilled',
|
|
355
|
+
value,
|
|
356
|
+
}))
|
|
357
|
+
.catch((reason) => ({
|
|
358
|
+
status: 'rejected',
|
|
359
|
+
reason,
|
|
360
|
+
}))));
|
|
336
361
|
}
|
|
337
362
|
|
|
338
363
|
export { AsyncWorkerPool, runAdapterWithReport };
|