more-proms 1.3.3 → 1.4.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.
@@ -15,11 +15,13 @@ export declare class ResablePromise<T = void> extends SettledPromise<T> {
15
15
  }
16
16
  export declare class CancelAblePromise<T = unknown, C = unknown> extends SettledPromise<T> {
17
17
  cancelled: boolean;
18
- cancel: () => void;
18
+ cancel: () => C;
19
19
  onCancel: Promise<void>;
20
20
  private nestedCancels;
21
21
  constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void, cancel?: () => C);
22
22
  then<TResult1 = T, TResult2 = never>(onfulfilled: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): CancelAblePromise<TResult1 | TResult2>;
23
23
  catch<TResult = never>(onrejected: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): CancelAblePromise<T | TResult>;
24
+ static all(proms: CancelAblePromise[], ifOneChildCancels?: "ignore" | "cancelThis" | "cancelAll"): CancelAblePromise<unknown[], unknown[]>;
25
+ static race(proms: CancelAblePromise[], ifOneChildCancels?: "ignore" | "cancelThis" | "cancelAll"): CancelAblePromise<unknown[], unknown[]>;
24
26
  }
25
27
  export {};
@@ -120,6 +120,30 @@ class CancelAblePromise extends SettledPromise {
120
120
  this.nestedCancels.push(r.cancel);
121
121
  return r;
122
122
  }
123
+ static all(proms, ifOneChildCancels = "ignore") {
124
+ return allOrRace("all", proms, ifOneChildCancels);
125
+ }
126
+ static race(proms, ifOneChildCancels = "ignore") {
127
+ return allOrRace("race", proms, ifOneChildCancels);
128
+ }
129
+ }
130
+ function allOrRace(allOrRace, proms, ifOneChildCancels = "ignore") {
131
+ const newP = new CancelAblePromise((res, rej) => {
132
+ Promise[allOrRace](proms).then(res, rej);
133
+ }, () => {
134
+ return proms.map((p) => p.cancel());
135
+ });
136
+ if (ifOneChildCancels === "ignore")
137
+ Promise.all(proms.map((p) => p.cancelled)).then(newP.cancel);
138
+ else if (ifOneChildCancels === "cancelThis")
139
+ Promise.race(proms.map((p) => p.cancelled)).then(newP.cancel);
140
+ else if (ifOneChildCancels === "cancelAll")
141
+ Promise.all(proms.map((p) => p.cancelled)).then(() => {
142
+ for (const prom of proms)
143
+ prom.cancel();
144
+ newP.cancel();
145
+ });
146
+ return newP;
123
147
  }
124
148
 
125
149
  exports.CancelAblePromise = CancelAblePromise;
@@ -15,11 +15,13 @@ export declare class ResablePromise<T = void> extends SettledPromise<T> {
15
15
  }
16
16
  export declare class CancelAblePromise<T = unknown, C = unknown> extends SettledPromise<T> {
17
17
  cancelled: boolean;
18
- cancel: () => void;
18
+ cancel: () => C;
19
19
  onCancel: Promise<void>;
20
20
  private nestedCancels;
21
21
  constructor(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void, cancel?: () => C);
22
22
  then<TResult1 = T, TResult2 = never>(onfulfilled: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): CancelAblePromise<TResult1 | TResult2>;
23
23
  catch<TResult = never>(onrejected: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): CancelAblePromise<T | TResult>;
24
+ static all(proms: CancelAblePromise[], ifOneChildCancels?: "ignore" | "cancelThis" | "cancelAll"): CancelAblePromise<unknown[], unknown[]>;
25
+ static race(proms: CancelAblePromise[], ifOneChildCancels?: "ignore" | "cancelThis" | "cancelAll"): CancelAblePromise<unknown[], unknown[]>;
24
26
  }
25
27
  export {};
@@ -117,4 +117,28 @@ export class CancelAblePromise extends SettledPromise {
117
117
  this.nestedCancels.push(r.cancel);
118
118
  return r;
119
119
  }
120
+ static all(proms, ifOneChildCancels = "ignore") {
121
+ return allOrRace("all", proms, ifOneChildCancels);
122
+ }
123
+ static race(proms, ifOneChildCancels = "ignore") {
124
+ return allOrRace("race", proms, ifOneChildCancels);
125
+ }
126
+ }
127
+ function allOrRace(allOrRace, proms, ifOneChildCancels = "ignore") {
128
+ const newP = new CancelAblePromise((res, rej) => {
129
+ Promise[allOrRace](proms).then(res, rej);
130
+ }, () => {
131
+ return proms.map((p) => p.cancel());
132
+ });
133
+ if (ifOneChildCancels === "ignore")
134
+ Promise.all(proms.map((p) => p.cancelled)).then(newP.cancel);
135
+ else if (ifOneChildCancels === "cancelThis")
136
+ Promise.race(proms.map((p) => p.cancelled)).then(newP.cancel);
137
+ else if (ifOneChildCancels === "cancelAll")
138
+ Promise.all(proms.map((p) => p.cancelled)).then(() => {
139
+ for (const prom of proms)
140
+ prom.cancel();
141
+ newP.cancel();
142
+ });
143
+ return newP;
120
144
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "more-proms",
3
- "version": "1.3.3",
3
+ "version": "1.4.0",
4
4
  "description": "A collection of additional promise extending classes. Including a (from the outside) ResablePromise, CancelAblePromise and a latestLatent utility function.",
5
5
  "main": "./app/dist/esm/moreProms.mjs",
6
6
  "types": "./app/dist/esm/moreProms.d.ts",