@squiz/optimization-utils 4.8.0 → 4.9.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/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -16,6 +16,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./types/class-utilities-types"), exports);
|
|
18
18
|
__exportStar(require("./retries/Retries"), exports);
|
|
19
|
+
__exportStar(require("./promises/PromiseMaxConcurrency"), exports);
|
|
19
20
|
__exportStar(require("./object/getProperty"), exports);
|
|
20
21
|
__exportStar(require("./metadata/CopyReflection"), exports);
|
|
21
22
|
__exportStar(require("./measurement/performance"), exports);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gEAA8C;AAC9C,oDAAkC;AAClC,uDAAqC;AACrC,4DAA0C;AAC1C,4DAA0C;AAC1C,gDAA8B;AAC9B,2DAAyC;AACzC,iEAA+C;AAC/C,mDAAiC;AACjC,iDAA+B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gEAA8C;AAC9C,oDAAkC;AAClC,mEAAiD;AACjD,uDAAqC;AACrC,4DAA0C;AAC1C,4DAA0C;AAC1C,gDAA8B;AAC9B,2DAAyC;AACzC,iEAA+C;AAC/C,mDAAiC;AACjC,iDAA+B"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type MaxConcurrencyOptions = {
|
|
2
|
+
maxConcurrency: number;
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* A utility class that executes promises with controlled concurrency.
|
|
6
|
+
* This class helps to limit the number of promises running simultaneously,
|
|
7
|
+
* preventing potential memory or performance issues when dealing with large arrays.
|
|
8
|
+
*/
|
|
9
|
+
export declare class PromiseMaxConcurrency<T> {
|
|
10
|
+
private readonly items;
|
|
11
|
+
private options;
|
|
12
|
+
/**
|
|
13
|
+
* Creates a new instance of PromiseMaxConcurrency.
|
|
14
|
+
* @param items - Array of items to process
|
|
15
|
+
* @param options - Configuration options for controlling concurrency
|
|
16
|
+
* @param options.maxConcurrency - Maximum number of promises to run simultaneously (defaults to 3)
|
|
17
|
+
*/
|
|
18
|
+
constructor(items: Array<T>, options?: MaxConcurrencyOptions);
|
|
19
|
+
handle<R>(handler: (value: T, index: number, array: Array<T>) => Promise<R>): Promise<Array<R>>;
|
|
20
|
+
}
|
|
21
|
+
export declare class PromiseMaxConcurrencyFactory {
|
|
22
|
+
constructor();
|
|
23
|
+
create<T>(items: Array<T>, options?: MaxConcurrencyOptions): PromiseMaxConcurrency<T>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PromiseMaxConcurrencyFactory = exports.PromiseMaxConcurrency = void 0;
|
|
13
|
+
const inversify_1 = require("inversify");
|
|
14
|
+
/**
|
|
15
|
+
* A utility class that executes promises with controlled concurrency.
|
|
16
|
+
* This class helps to limit the number of promises running simultaneously,
|
|
17
|
+
* preventing potential memory or performance issues when dealing with large arrays.
|
|
18
|
+
*/
|
|
19
|
+
let PromiseMaxConcurrency = class PromiseMaxConcurrency {
|
|
20
|
+
items;
|
|
21
|
+
options;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a new instance of PromiseMaxConcurrency.
|
|
24
|
+
* @param items - Array of items to process
|
|
25
|
+
* @param options - Configuration options for controlling concurrency
|
|
26
|
+
* @param options.maxConcurrency - Maximum number of promises to run simultaneously (defaults to 3)
|
|
27
|
+
*/
|
|
28
|
+
constructor(items, options) {
|
|
29
|
+
this.items = items;
|
|
30
|
+
this.options = options ?? {
|
|
31
|
+
maxConcurrency: 3,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
async handle(handler) {
|
|
35
|
+
let promises = [];
|
|
36
|
+
const results = [];
|
|
37
|
+
const maxConcurrency = this.options.maxConcurrency;
|
|
38
|
+
for (let i = 0; i < this.items.length; i++) {
|
|
39
|
+
promises.push(handler(this.items[i], i, this.items));
|
|
40
|
+
if (promises.length === maxConcurrency) {
|
|
41
|
+
const result = await Promise.all(promises);
|
|
42
|
+
results.push(...result);
|
|
43
|
+
promises = [];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const result = await Promise.all(promises);
|
|
47
|
+
results.push(...result);
|
|
48
|
+
return results;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
exports.PromiseMaxConcurrency = PromiseMaxConcurrency;
|
|
52
|
+
exports.PromiseMaxConcurrency = PromiseMaxConcurrency = __decorate([
|
|
53
|
+
(0, inversify_1.injectable)(),
|
|
54
|
+
__metadata("design:paramtypes", [Array, Object])
|
|
55
|
+
], PromiseMaxConcurrency);
|
|
56
|
+
let PromiseMaxConcurrencyFactory = class PromiseMaxConcurrencyFactory {
|
|
57
|
+
constructor() { }
|
|
58
|
+
create(items, options) {
|
|
59
|
+
return new PromiseMaxConcurrency(items, options);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
exports.PromiseMaxConcurrencyFactory = PromiseMaxConcurrencyFactory;
|
|
63
|
+
exports.PromiseMaxConcurrencyFactory = PromiseMaxConcurrencyFactory = __decorate([
|
|
64
|
+
(0, inversify_1.injectable)(),
|
|
65
|
+
__metadata("design:paramtypes", [])
|
|
66
|
+
], PromiseMaxConcurrencyFactory);
|
|
67
|
+
//# sourceMappingURL=PromiseMaxConcurrency.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PromiseMaxConcurrency.js","sourceRoot":"","sources":["../../src/promises/PromiseMaxConcurrency.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAAuC;AAMvC;;;;GAIG;AAEI,IAAM,qBAAqB,GAA3B,MAAM,qBAAqB;IAUb;IATX,OAAO,CAAwB;IAEvC;;;;;OAKG;IACH,YACmB,KAAe,EAChC,OAA+B;QADd,UAAK,GAAL,KAAK,CAAU;QAGhC,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI;YACxB,cAAc,EAAE,CAAC;SAClB,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,MAAM,CACV,OAAiE;QAEjE,IAAI,QAAQ,GAAsB,EAAE,CAAC;QACrC,MAAM,OAAO,GAAa,EAAE,CAAC;QAC7B,MAAM,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,cAAc,CAAC;QAEnD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YAC3C,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;YAErD,IAAI,QAAQ,CAAC,MAAM,KAAK,cAAc,EAAE,CAAC;gBACvC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBAE3C,OAAO,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC;gBACxB,QAAQ,GAAG,EAAE,CAAC;YAChB,CAAC;QACH,CAAC;QAED,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAE3C,OAAO,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC;QAExB,OAAO,OAAO,CAAC;IACjB,CAAC;CACF,CAAA;AA1CY,sDAAqB;gCAArB,qBAAqB;IADjC,IAAA,sBAAU,GAAE;qCAWe,KAAK;GAVpB,qBAAqB,CA0CjC;AAGM,IAAM,4BAA4B,GAAlC,MAAM,4BAA4B;IACvC,gBAAe,CAAC;IAEhB,MAAM,CACJ,KAAe,EACf,OAA+B;QAE/B,OAAO,IAAI,qBAAqB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACnD,CAAC;CACF,CAAA;AATY,oEAA4B;uCAA5B,4BAA4B;IADxC,IAAA,sBAAU,GAAE;;GACA,4BAA4B,CASxC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@squiz/optimization-utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.9.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
"dependencies": {},
|
|
35
35
|
"devDependencies": {},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"inversify": "^7.
|
|
38
|
-
"@squiz/optimization-logger": "^1.8.
|
|
37
|
+
"inversify": "^7.9.1",
|
|
38
|
+
"@squiz/optimization-logger": "^1.8.4"
|
|
39
39
|
},
|
|
40
40
|
"author": "",
|
|
41
41
|
"license": "ISC"
|