@furystack/utils 2.0.2 → 2.0.3

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.
Files changed (40) hide show
  1. package/dist/index.d.ts +0 -2
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +0 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/path-helper.d.ts +1 -36
  6. package/dist/path-helper.d.ts.map +1 -1
  7. package/dist/path-helper.js +2 -73
  8. package/dist/path-helper.js.map +1 -1
  9. package/dist/path-helper.spec.d.ts.map +1 -1
  10. package/dist/path-helper.spec.js +2 -82
  11. package/dist/path-helper.spec.js.map +1 -1
  12. package/package.json +2 -2
  13. package/src/index.ts +0 -2
  14. package/src/path-helper.spec.ts +2 -101
  15. package/src/path-helper.ts +2 -77
  16. package/dist/filter-async.d.ts +0 -20
  17. package/dist/filter-async.d.ts.map +0 -1
  18. package/dist/filter-async.js +0 -23
  19. package/dist/filter-async.js.map +0 -1
  20. package/dist/filter-async.spec.d.ts +0 -2
  21. package/dist/filter-async.spec.d.ts.map +0 -1
  22. package/dist/filter-async.spec.js +0 -25
  23. package/dist/filter-async.spec.js.map +0 -1
  24. package/dist/retrier-options.d.ts +0 -50
  25. package/dist/retrier-options.d.ts.map +0 -1
  26. package/dist/retrier-options.js +0 -54
  27. package/dist/retrier-options.js.map +0 -1
  28. package/dist/retrier.d.ts +0 -52
  29. package/dist/retrier.d.ts.map +0 -1
  30. package/dist/retrier.js +0 -103
  31. package/dist/retrier.js.map +0 -1
  32. package/dist/retries.spec.d.ts +0 -5
  33. package/dist/retries.spec.d.ts.map +0 -1
  34. package/dist/retries.spec.js +0 -93
  35. package/dist/retries.spec.js.map +0 -1
  36. package/src/filter-async.spec.ts +0 -25
  37. package/src/filter-async.ts +0 -29
  38. package/src/retrier-options.ts +0 -65
  39. package/src/retrier.ts +0 -105
  40. package/src/retries.spec.ts +0 -98
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- require("./filter-async");
4
- const sleep_async_1 = require("./sleep-async");
5
- describe('FilterAsync', () => {
6
- it('Should return the original array in case of truthy values', async () => {
7
- const arr = [1, 2, 3];
8
- const filtered = await arr.filterAsync(async () => true);
9
- expect(filtered).toEqual(arr);
10
- });
11
- it('Should return an empty array in case of falsy values', async () => {
12
- const arr = [1, 2, 3];
13
- const filtered = await arr.filterAsync(async () => false);
14
- expect(filtered).toEqual([]);
15
- });
16
- it('Should return the correct values', async () => {
17
- const arr = [1, 2, 3, 4, 5];
18
- const filtered = await arr.filterAsync(async (value) => {
19
- await (0, sleep_async_1.sleepAsync)(1);
20
- return value % 2 === 0;
21
- });
22
- expect(filtered).toEqual([2, 4]);
23
- });
24
- });
25
- //# sourceMappingURL=filter-async.spec.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"filter-async.spec.js","sourceRoot":"","sources":["../src/filter-async.spec.ts"],"names":[],"mappings":";;AAAA,0BAAuB;AACvB,+CAA0C;AAE1C,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,EAAE,CAAC,2DAA2D,EAAE,KAAK,IAAI,EAAE;QACzE,MAAM,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;QACrB,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,WAAW,CAAC,KAAK,IAAI,EAAE,CAAC,IAAI,CAAC,CAAA;QACxD,MAAM,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;IAC/B,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,sDAAsD,EAAE,KAAK,IAAI,EAAE;QACpE,MAAM,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;QACrB,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,WAAW,CAAC,KAAK,IAAI,EAAE,CAAC,KAAK,CAAC,CAAA;QACzD,MAAM,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;IAC9B,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,kCAAkC,EAAE,KAAK,IAAI,EAAE;QAChD,MAAM,GAAG,GAAG,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;QAC3B,MAAM,QAAQ,GAAG,MAAM,GAAG,CAAC,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;YACrD,MAAM,IAAA,wBAAU,EAAC,CAAC,CAAC,CAAA;YACnB,OAAO,KAAK,GAAG,CAAC,KAAK,CAAC,CAAA;QACxB,CAAC,CAAC,CAAA;QACF,MAAM,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IAClC,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
@@ -1,50 +0,0 @@
1
- /**
2
- * Options class for Retrier
3
- */
4
- export declare class RetrierOptions {
5
- /**
6
- * The default value for retries
7
- */
8
- static readonly RETRIES_DEFAULT = 10;
9
- private retries;
10
- /**
11
- * How many times should retry the operation
12
- *
13
- * @returns the count of retries
14
- */
15
- get Retries(): number;
16
- set Retries(v: number);
17
- /**
18
- * The default interval between retries
19
- */
20
- static readonly RETRY_INTERVAL_MS_DEFAULT = 10;
21
- private retryIntervalMs?;
22
- /**
23
- * @returns the interval in millisecs
24
- */
25
- get RetryIntervalMs(): number;
26
- set RetryIntervalMs(v: number);
27
- /**
28
- * The default timeout in millisecs
29
- */
30
- static readonly TIMEOUT_MS_DEFAULT = 1000;
31
- private timeoutMsValue?;
32
- /**
33
- * @returns the Timeout interval in milliseconds
34
- */
35
- get timeoutMs(): number;
36
- set timeoutMs(v: number);
37
- /**
38
- * Optional callback, triggered right before each try
39
- */
40
- onTry?: () => void;
41
- /**
42
- * Optional callback, triggered on success
43
- */
44
- onSuccess?: () => void;
45
- /**
46
- * Optional callback, triggered on fail (timeout or too many retries)
47
- */
48
- onFail?: () => void;
49
- }
50
- //# sourceMappingURL=retrier-options.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"retrier-options.d.ts","sourceRoot":"","sources":["../src/retrier-options.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,cAAc;IACzB;;OAEG;IACH,gBAAuB,eAAe,MAAK;IAC3C,OAAO,CAAC,OAAO,CAAyC;IACxD;;;;OAIG;IACH,IAAW,OAAO,IAAI,MAAM,CAE3B;IACD,IAAW,OAAO,CAAC,CAAC,EAAE,MAAM,EAE3B;IAED;;OAEG;IACH,gBAAuB,yBAAyB,MAAK;IACrD,OAAO,CAAC,eAAe,CAAC,CAAQ;IAChC;;OAEG;IACH,IAAW,eAAe,IAAI,MAAM,CAEnC;IACD,IAAW,eAAe,CAAC,CAAC,EAAE,MAAM,EAEnC;IAED;;OAEG;IACH,gBAAuB,kBAAkB,QAAO;IAChD,OAAO,CAAC,cAAc,CAAC,CAAQ;IAE/B;;OAEG;IACH,IAAW,SAAS,IAAI,MAAM,CAE7B;IACD,IAAW,SAAS,CAAC,CAAC,EAAE,MAAM,EAE7B;IAED;;OAEG;IACI,KAAK,CAAC,EAAE,MAAM,IAAI,CAAA;IACzB;;OAEG;IACI,SAAS,CAAC,EAAE,MAAM,IAAI,CAAA;IAC7B;;OAEG;IACI,MAAM,CAAC,EAAE,MAAM,IAAI,CAAA;CAC3B"}
@@ -1,54 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RetrierOptions = void 0;
4
- /**
5
- * Options class for Retrier
6
- */
7
- class RetrierOptions {
8
- constructor() {
9
- this.retries = RetrierOptions.RETRIES_DEFAULT;
10
- }
11
- /**
12
- * How many times should retry the operation
13
- *
14
- * @returns the count of retries
15
- */
16
- get Retries() {
17
- return this.retries;
18
- }
19
- set Retries(v) {
20
- this.retries = v;
21
- }
22
- /**
23
- * @returns the interval in millisecs
24
- */
25
- get RetryIntervalMs() {
26
- return this.retryIntervalMs !== undefined ? this.retryIntervalMs : RetrierOptions.RETRY_INTERVAL_MS_DEFAULT;
27
- }
28
- set RetryIntervalMs(v) {
29
- this.retryIntervalMs = v;
30
- }
31
- /**
32
- * @returns the Timeout interval in milliseconds
33
- */
34
- get timeoutMs() {
35
- return this.timeoutMsValue !== undefined ? this.timeoutMsValue : RetrierOptions.TIMEOUT_MS_DEFAULT;
36
- }
37
- set timeoutMs(v) {
38
- this.timeoutMsValue = v;
39
- }
40
- }
41
- exports.RetrierOptions = RetrierOptions;
42
- /**
43
- * The default value for retries
44
- */
45
- RetrierOptions.RETRIES_DEFAULT = 10;
46
- /**
47
- * The default interval between retries
48
- */
49
- RetrierOptions.RETRY_INTERVAL_MS_DEFAULT = 10;
50
- /**
51
- * The default timeout in millisecs
52
- */
53
- RetrierOptions.TIMEOUT_MS_DEFAULT = 1000;
54
- //# sourceMappingURL=retrier-options.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"retrier-options.js","sourceRoot":"","sources":["../src/retrier-options.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,MAAa,cAAc;IAA3B;QAKU,YAAO,GAAW,cAAc,CAAC,eAAe,CAAA;IAwD1D,CAAC;IAvDC;;;;OAIG;IACH,IAAW,OAAO;QAChB,OAAO,IAAI,CAAC,OAAO,CAAA;IACrB,CAAC;IACD,IAAW,OAAO,CAAC,CAAS;QAC1B,IAAI,CAAC,OAAO,GAAG,CAAC,CAAA;IAClB,CAAC;IAOD;;OAEG;IACH,IAAW,eAAe;QACxB,OAAO,IAAI,CAAC,eAAe,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,CAAC,cAAc,CAAC,yBAAyB,CAAA;IAC7G,CAAC;IACD,IAAW,eAAe,CAAC,CAAS;QAClC,IAAI,CAAC,eAAe,GAAG,CAAC,CAAA;IAC1B,CAAC;IAQD;;OAEG;IACH,IAAW,SAAS;QAClB,OAAO,IAAI,CAAC,cAAc,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,CAAC,cAAc,CAAC,kBAAkB,CAAA;IACpG,CAAC;IACD,IAAW,SAAS,CAAC,CAAS;QAC5B,IAAI,CAAC,cAAc,GAAG,CAAC,CAAA;IACzB,CAAC;;AA/CH,wCA6DC;AA5DC;;GAEG;AACoB,8BAAe,GAAG,EAAE,CAAA;AAc3C;;GAEG;AACoB,wCAAyB,GAAG,EAAE,CAAA;AAYrD;;GAEG;AACoB,iCAAkB,GAAG,IAAI,CAAA"}
package/dist/retrier.d.ts DELETED
@@ -1,52 +0,0 @@
1
- import { RetrierOptions } from './retrier-options';
2
- /**
3
- * Utility class for retrying operations.
4
- *
5
- * Usage example:
6
- * ```
7
- * const methodToRetry: () => Promise<boolean> = async () => {
8
- * let hasSucceeded = false;
9
- * // ...
10
- * // custom logic
11
- * // ...
12
- * return hasSucceeded;
13
- * }
14
- * const retrierSuccess = await Retrier.create(methodToRetry)
15
- * .setup({
16
- * retries: 3,
17
- * retryIntervalMs: 1,
18
- * timeoutMs: 1000
19
- * })
20
- * .run();
21
- * ```
22
- */
23
- export declare class Retrier {
24
- private callback;
25
- readonly options: RetrierOptions;
26
- private isRunning;
27
- /**
28
- * Factory method for creating a Retrier
29
- *
30
- * @param {()=>Promise<boolean>} callback The method that will be invoked on each try
31
- * @returns the created Retrier instance
32
- */
33
- static create(callback: () => Promise<boolean>): Retrier;
34
- private constructor();
35
- private wait;
36
- /**
37
- * Method to override the default Retrier settings.
38
- *
39
- * @param options The options to be overridden
40
- * @throws Error if the Retrier is running.
41
- * @returns the Retrier instance
42
- */
43
- setup(options: Partial<RetrierOptions>): this;
44
- /**
45
- * Public method that starts the Retrier
46
- *
47
- * @throws Error if the Retrier is already started.
48
- * @returns {Promise<boolean>} A boolean value that indicates if the process has been succeeded.
49
- */
50
- run(): Promise<boolean>;
51
- }
52
- //# sourceMappingURL=retrier.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"retrier.d.ts","sourceRoot":"","sources":["../src/retrier.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAElD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,OAAO;IAaE,OAAO,CAAC,QAAQ;aAA0C,OAAO,EAAE,cAAc;IAZrG,OAAO,CAAC,SAAS,CAAQ;IAEzB;;;;;OAKG;WACW,MAAM,CAAC,QAAQ,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC;IAIrD,OAAO;YAEO,IAAI;IAMlB;;;;;;OAMG;IACI,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,cAAc,CAAC;IAQ7C;;;;;OAKG;IACU,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC;CAuCrC"}
package/dist/retrier.js DELETED
@@ -1,103 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Retrier = void 0;
4
- const retrier_options_1 = require("./retrier-options");
5
- /**
6
- * Utility class for retrying operations.
7
- *
8
- * Usage example:
9
- * ```
10
- * const methodToRetry: () => Promise<boolean> = async () => {
11
- * let hasSucceeded = false;
12
- * // ...
13
- * // custom logic
14
- * // ...
15
- * return hasSucceeded;
16
- * }
17
- * const retrierSuccess = await Retrier.create(methodToRetry)
18
- * .setup({
19
- * retries: 3,
20
- * retryIntervalMs: 1,
21
- * timeoutMs: 1000
22
- * })
23
- * .run();
24
- * ```
25
- */
26
- class Retrier {
27
- constructor(callback, options) {
28
- this.callback = callback;
29
- this.options = options;
30
- this.isRunning = false;
31
- }
32
- /**
33
- * Factory method for creating a Retrier
34
- *
35
- * @param {()=>Promise<boolean>} callback The method that will be invoked on each try
36
- * @returns the created Retrier instance
37
- */
38
- static create(callback) {
39
- return new Retrier(callback, new retrier_options_1.RetrierOptions());
40
- }
41
- async wait(ms) {
42
- return new Promise((resolve) => {
43
- setTimeout(resolve, ms);
44
- });
45
- }
46
- /**
47
- * Method to override the default Retrier settings.
48
- *
49
- * @param options The options to be overridden
50
- * @throws Error if the Retrier is running.
51
- * @returns the Retrier instance
52
- */
53
- setup(options) {
54
- if (this.isRunning) {
55
- throw Error('Retrier already started!');
56
- }
57
- Object.assign(this.options, options);
58
- return this;
59
- }
60
- /**
61
- * Public method that starts the Retrier
62
- *
63
- * @throws Error if the Retrier is already started.
64
- * @returns {Promise<boolean>} A boolean value that indicates if the process has been succeeded.
65
- */
66
- async run() {
67
- if (this.isRunning) {
68
- throw Error('Retrier already started!');
69
- }
70
- let succeeded = false;
71
- let retries = 0;
72
- let timedOut = false;
73
- this.isRunning = true;
74
- setTimeout(() => {
75
- if (!succeeded) {
76
- timedOut = true;
77
- }
78
- }, this.options.timeoutMs);
79
- while (!succeeded && !timedOut && this.options.Retries > retries) {
80
- retries++;
81
- if (this.options.onTry) {
82
- this.options.onTry();
83
- }
84
- succeeded = await this.callback();
85
- if (!succeeded) {
86
- await this.wait(this.options.RetryIntervalMs);
87
- }
88
- }
89
- if (succeeded) {
90
- if (!timedOut && this.options.onSuccess) {
91
- this.options.onSuccess();
92
- }
93
- }
94
- else {
95
- if (this.options.onFail) {
96
- this.options.onFail();
97
- }
98
- }
99
- return succeeded;
100
- }
101
- }
102
- exports.Retrier = Retrier;
103
- //# sourceMappingURL=retrier.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"retrier.js","sourceRoot":"","sources":["../src/retrier.ts"],"names":[],"mappings":";;;AAAA,uDAAkD;AAElD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAa,OAAO;IAalB,YAA4B,QAAgC,EAAkB,OAAuB;QAAzE,aAAQ,GAAR,QAAQ,CAAwB;QAAkB,YAAO,GAAP,OAAO,CAAgB;QAZ7F,cAAS,GAAG,KAAK,CAAA;IAY+E,CAAC;IAVzG;;;;;OAKG;IACI,MAAM,CAAC,MAAM,CAAC,QAAgC;QACnD,OAAO,IAAI,OAAO,CAAC,QAAQ,EAAE,IAAI,gCAAc,EAAE,CAAC,CAAA;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C,CAAA;aAC9C;SACF;QAED,IAAI,SAAS,EAAE;YACb,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE;gBACvC,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAA;aACzB;SACF;aAAM;YACL,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;gBACvB,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAA;aACtB;SACF;QACD,OAAO,SAAS,CAAA;IAClB,CAAC;CACF;AAjFD,0BAiFC"}
@@ -1,5 +0,0 @@
1
- /**
2
- * Retrier tests
3
- */
4
- export declare const retrierTests: void;
5
- //# sourceMappingURL=retries.spec.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"retries.spec.d.ts","sourceRoot":"","sources":["../src/retries.spec.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,eAAO,MAAM,YAAY,MA4FvB,CAAA"}
@@ -1,93 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.retrierTests = void 0;
4
- const retrier_1 = require("./retrier");
5
- /**
6
- * Retrier tests
7
- */
8
- exports.retrierTests = describe('Retrier', () => {
9
- describe('Counter', () => {
10
- it('Should be able to count to 3', async () => {
11
- let count = 0;
12
- await retrier_1.Retrier.create(async () => {
13
- count = count + 1;
14
- return count === 3;
15
- }).run();
16
- expect(count).toBe(3);
17
- });
18
- });
19
- describe('events', () => {
20
- it('should trigger onSuccess on success', async () => {
21
- let triggered = false;
22
- await retrier_1.Retrier.create(async () => true)
23
- .setup({
24
- onSuccess: () => {
25
- triggered = true;
26
- },
27
- })
28
- .run();
29
- expect(triggered).toBe(true);
30
- });
31
- it('should trigger onTimeout on timeout', async () => {
32
- let triggered = false;
33
- await retrier_1.Retrier.create(async () => false)
34
- .setup({
35
- onFail: () => {
36
- triggered = true;
37
- },
38
- timeoutMs: 1,
39
- })
40
- .run();
41
- expect(triggered).toBe(true);
42
- });
43
- it('should trigger onTry on each try', async () => {
44
- let triggered = false;
45
- await retrier_1.Retrier.create(async () => true)
46
- .setup({
47
- onTry: () => {
48
- triggered = true;
49
- },
50
- })
51
- .run();
52
- expect(triggered).toBe(true);
53
- });
54
- });
55
- it('should work with an example test', async () => {
56
- const funcToRetry = async () => {
57
- const hasSucceeded = false;
58
- // ...
59
- // custom logic
60
- // ...
61
- return hasSucceeded;
62
- };
63
- const retrierSuccess = await retrier_1.Retrier.create(funcToRetry)
64
- .setup({
65
- Retries: 3,
66
- RetryIntervalMs: 1,
67
- timeoutMs: 1000,
68
- })
69
- .run();
70
- expect(retrierSuccess).toBe(false);
71
- });
72
- it('should throw error when started twice', async () => {
73
- const retrier = retrier_1.Retrier.create(async () => false);
74
- retrier.run();
75
- try {
76
- await retrier.run();
77
- throw Error('Should have been failed');
78
- }
79
- catch (error) {
80
- // ignore
81
- }
82
- });
83
- it('should throw an error when trying to set up after started', () => {
84
- const retrier = retrier_1.Retrier.create(async () => false);
85
- retrier.run();
86
- expect(() => {
87
- retrier.setup({
88
- Retries: 2,
89
- });
90
- }).toThrow();
91
- });
92
- });
93
- //# sourceMappingURL=retries.spec.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"retries.spec.js","sourceRoot":"","sources":["../src/retries.spec.ts"],"names":[],"mappings":";;;AAAA,uCAAmC;AAEnC;;GAEG;AACU,QAAA,YAAY,GAAG,QAAQ,CAAC,SAAS,EAAE,GAAG,EAAE;IACnD,QAAQ,CAAC,SAAS,EAAE,GAAG,EAAE;QACvB,EAAE,CAAC,8BAA8B,EAAE,KAAK,IAAI,EAAE;YAC5C,IAAI,KAAK,GAAG,CAAC,CAAA;YACb,MAAM,iBAAO,CAAC,MAAM,CAAC,KAAK,IAAI,EAAE;gBAC9B,KAAK,GAAG,KAAK,GAAG,CAAC,CAAA;gBACjB,OAAO,KAAK,KAAK,CAAC,CAAA;YACpB,CAAC,CAAC,CAAC,GAAG,EAAE,CAAA;YACR,MAAM,CAAC,KAAK,CAAC,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AC,CAAA;AACJ,CAAC,CAAC,CAAA"}
@@ -1,25 +0,0 @@
1
- import './filter-async'
2
- import { sleepAsync } from './sleep-async'
3
-
4
- describe('FilterAsync', () => {
5
- it('Should return the original array in case of truthy values', async () => {
6
- const arr = [1, 2, 3]
7
- const filtered = await arr.filterAsync(async () => true)
8
- expect(filtered).toEqual(arr)
9
- })
10
-
11
- it('Should return an empty array in case of falsy values', async () => {
12
- const arr = [1, 2, 3]
13
- const filtered = await arr.filterAsync(async () => false)
14
- expect(filtered).toEqual([])
15
- })
16
-
17
- it('Should return the correct values', async () => {
18
- const arr = [1, 2, 3, 4, 5]
19
- const filtered = await arr.filterAsync(async (value) => {
20
- await sleepAsync(1)
21
- return value % 2 === 0
22
- })
23
- expect(filtered).toEqual([2, 4])
24
- })
25
- })
@@ -1,29 +0,0 @@
1
- /**
2
- * The async variant of the Array.filter() method that
3
- *
4
- * @param values An iterable of elements to filter
5
- * @param callbackFn The async callback that will be executed on the elements
6
- * @returns Promise<T[]>
7
- */
8
- export const filterAsync = async <T>(values: Iterable<T>, callbackFn: (entry: T) => Promise<boolean>) => {
9
- const returns = []
10
- for (const value of values) {
11
- ;(await callbackFn(value)) && returns.push(value)
12
- }
13
- return returns
14
- }
15
-
16
- declare global {
17
- /**
18
- * Defines an array of elements
19
- */
20
- export interface Array<T> {
21
- /**
22
- * Returns a promise with a new array of elements that meets the specified async callback
23
- */
24
- filterAsync: (callbackFn: (entry: T) => Promise<boolean>) => Promise<T[]>
25
- }
26
- }
27
- Array.prototype.filterAsync = function (callbackFn) {
28
- return filterAsync(this, callbackFn)
29
- }
@@ -1,65 +0,0 @@
1
- /**
2
- * Options class for Retrier
3
- */
4
- export class RetrierOptions {
5
- /**
6
- * The default value for retries
7
- */
8
- public static readonly RETRIES_DEFAULT = 10
9
- private retries: number = RetrierOptions.RETRIES_DEFAULT
10
- /**
11
- * How many times should retry the operation
12
- *
13
- * @returns the count of retries
14
- */
15
- public get Retries(): number {
16
- return this.retries
17
- }
18
- public set Retries(v: number) {
19
- this.retries = v
20
- }
21
-
22
- /**
23
- * The default interval between retries
24
- */
25
- public static readonly RETRY_INTERVAL_MS_DEFAULT = 10
26
- private retryIntervalMs?: number
27
- /**
28
- * @returns the interval in millisecs
29
- */
30
- public get RetryIntervalMs(): number {
31
- return this.retryIntervalMs !== undefined ? this.retryIntervalMs : RetrierOptions.RETRY_INTERVAL_MS_DEFAULT
32
- }
33
- public set RetryIntervalMs(v: number) {
34
- this.retryIntervalMs = v
35
- }
36
-
37
- /**
38
- * The default timeout in millisecs
39
- */
40
- public static readonly TIMEOUT_MS_DEFAULT = 1000
41
- private timeoutMsValue?: number
42
-
43
- /**
44
- * @returns the Timeout interval in milliseconds
45
- */
46
- public get timeoutMs(): number {
47
- return this.timeoutMsValue !== undefined ? this.timeoutMsValue : RetrierOptions.TIMEOUT_MS_DEFAULT
48
- }
49
- public set timeoutMs(v: number) {
50
- this.timeoutMsValue = v
51
- }
52
-
53
- /**
54
- * Optional callback, triggered right before each try
55
- */
56
- public onTry?: () => void
57
- /**
58
- * Optional callback, triggered on success
59
- */
60
- public onSuccess?: () => void
61
- /**
62
- * Optional callback, triggered on fail (timeout or too many retries)
63
- */
64
- public onFail?: () => void
65
- }
package/src/retrier.ts DELETED
@@ -1,105 +0,0 @@
1
- import { RetrierOptions } from './retrier-options'
2
-
3
- /**
4
- * Utility class for retrying operations.
5
- *
6
- * Usage example:
7
- * ```
8
- * const methodToRetry: () => Promise<boolean> = async () => {
9
- * let hasSucceeded = false;
10
- * // ...
11
- * // custom logic
12
- * // ...
13
- * return hasSucceeded;
14
- * }
15
- * const retrierSuccess = await Retrier.create(methodToRetry)
16
- * .setup({
17
- * retries: 3,
18
- * retryIntervalMs: 1,
19
- * timeoutMs: 1000
20
- * })
21
- * .run();
22
- * ```
23
- */
24
- export class Retrier {
25
- private isRunning = false
26
-
27
- /**
28
- * Factory method for creating a Retrier
29
- *
30
- * @param {()=>Promise<boolean>} callback The method that will be invoked on each try
31
- * @returns the created Retrier instance
32
- */
33
- public static create(callback: () => Promise<boolean>) {
34
- return new Retrier(callback, new RetrierOptions())
35
- }
36
-
37
- private constructor(private callback: () => Promise<boolean>, public readonly options: RetrierOptions) {}
38
-
39
- private async wait(ms: number) {
40
- return new Promise((resolve) => {
41
- setTimeout(resolve, ms)
42
- })
43
- }
44
-
45
- /**
46
- * Method to override the default Retrier settings.
47
- *
48
- * @param options The options to be overridden
49
- * @throws Error if the Retrier is running.
50
- * @returns the Retrier instance
51
- */
52
- public setup(options: Partial<RetrierOptions>) {
53
- if (this.isRunning) {
54
- throw Error('Retrier already started!')
55
- }
56
- Object.assign(this.options, options)
57
- return this
58
- }
59
-
60
- /**
61
- * Public method that starts the Retrier
62
- *
63
- * @throws Error if the Retrier is already started.
64
- * @returns {Promise<boolean>} A boolean value that indicates if the process has been succeeded.
65
- */
66
- public async run(): Promise<boolean> {
67
- if (this.isRunning) {
68
- throw Error('Retrier already started!')
69
- }
70
-
71
- let succeeded = false
72
- let retries = 0
73
- let timedOut = false
74
-
75
- this.isRunning = true
76
-
77
- setTimeout(() => {
78
- if (!succeeded) {
79
- timedOut = true
80
- }
81
- }, this.options.timeoutMs)
82
-
83
- while (!succeeded && !timedOut && this.options.Retries > retries) {
84
- retries++
85
- if (this.options.onTry) {
86
- this.options.onTry()
87
- }
88
- succeeded = await this.callback()
89
- if (!succeeded) {
90
- await this.wait(this.options.RetryIntervalMs)
91
- }
92
- }
93
-
94
- if (succeeded) {
95
- if (!timedOut && this.options.onSuccess) {
96
- this.options.onSuccess()
97
- }
98
- } else {
99
- if (this.options.onFail) {
100
- this.options.onFail()
101
- }
102
- }
103
- return succeeded
104
- }
105
- }