@salesforce/lds-utils-adapters 1.103.1 → 1.105.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.
@@ -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 activeWorkers;
8
+ private activeWork;
5
9
  constructor(concurrency: number);
6
- push<T>(workFn: () => Promise<T>): Promise<T>;
7
- cancel(): void;
8
- private work;
10
+ push<T>(work: Work<T>): Promise<T>;
11
+ cancel(): Promise<void>;
12
+ private doWork;
9
13
  }
@@ -306,33 +306,58 @@ function runAdapterWithReport(adapterName, adapter, adapterConfig, requestContex
306
306
  class AsyncWorkerPool {
307
307
  constructor(concurrency) {
308
308
  this.queue = [];
309
- this.activeWorkers = 0;
309
+ this.activeWork = [];
310
310
  this.concurrency = concurrency;
311
311
  }
312
- push(workFn) {
312
+ push(work) {
313
313
  return new Promise((resolve, reject) => {
314
- this.queue.push(async () => {
315
- return workFn().then(resolve).catch(reject);
314
+ this.queue.push({
315
+ ...work,
316
+ workFn: () => work.workFn().then(resolve).catch(reject),
316
317
  });
317
- this.work();
318
+ this.doWork();
318
319
  });
319
320
  }
320
- cancel() {
321
- this.queue = [];
322
- // TODO [W-12513105]: thread cancellation through to active workers
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
- work() {
325
- while (this.queue.length > 0 && this.activeWorkers < this.concurrency) {
326
- this.activeWorkers += 1;
327
- const next = this.queue.shift();
328
- if (next) {
329
- next().finally(() => {
330
- this.activeWorkers -= 1;
331
- this.work();
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@salesforce/lds-utils-adapters",
3
- "version": "1.103.1",
3
+ "version": "1.105.0",
4
4
  "license": "SEE LICENSE IN LICENSE.txt",
5
5
  "description": "LDS Adapter Utilities",
6
6
  "main": "dist/ldsAdapterUtils.js",