@furystack/utils 1.2.46 → 2.0.4

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 (45) hide show
  1. package/CHANGELOG.md +76 -0
  2. package/dist/index.d.ts +0 -2
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +0 -2
  5. package/dist/index.js.map +1 -1
  6. package/dist/path-helper.d.ts +1 -36
  7. package/dist/path-helper.d.ts.map +1 -1
  8. package/dist/path-helper.js +2 -73
  9. package/dist/path-helper.js.map +1 -1
  10. package/dist/path-helper.spec.d.ts.map +1 -1
  11. package/dist/path-helper.spec.js +2 -82
  12. package/dist/path-helper.spec.js.map +1 -1
  13. package/dist/value-observer.d.ts +1 -1
  14. package/dist/value-observer.d.ts.map +1 -1
  15. package/dist/value-observer.js.map +1 -1
  16. package/package.json +4 -5
  17. package/src/index.ts +0 -2
  18. package/src/path-helper.spec.ts +2 -101
  19. package/src/path-helper.ts +2 -77
  20. package/src/value-observer.ts +1 -1
  21. package/dist/filter-async.d.ts +0 -20
  22. package/dist/filter-async.d.ts.map +0 -1
  23. package/dist/filter-async.js +0 -23
  24. package/dist/filter-async.js.map +0 -1
  25. package/dist/filter-async.spec.d.ts +0 -2
  26. package/dist/filter-async.spec.d.ts.map +0 -1
  27. package/dist/filter-async.spec.js +0 -25
  28. package/dist/filter-async.spec.js.map +0 -1
  29. package/dist/retrier-options.d.ts +0 -50
  30. package/dist/retrier-options.d.ts.map +0 -1
  31. package/dist/retrier-options.js +0 -54
  32. package/dist/retrier-options.js.map +0 -1
  33. package/dist/retrier.d.ts +0 -52
  34. package/dist/retrier.d.ts.map +0 -1
  35. package/dist/retrier.js +0 -103
  36. package/dist/retrier.js.map +0 -1
  37. package/dist/retries.spec.d.ts +0 -5
  38. package/dist/retries.spec.d.ts.map +0 -1
  39. package/dist/retries.spec.js +0 -93
  40. package/dist/retries.spec.js.map +0 -1
  41. package/src/filter-async.spec.ts +0 -25
  42. package/src/filter-async.ts +0 -29
  43. package/src/retrier-options.ts +0 -65
  44. package/src/retrier.ts +0 -105
  45. package/src/retries.spec.ts +0 -98
@@ -22,88 +22,13 @@ export class PathHelper {
22
22
 
23
23
  /**
24
24
  * Splits a full path into path segments,
25
- * e.g.: /Root/Example('Content1') will be ["Root", "Example", "('Content1')"]
25
+ * e.g.: /Root/Example/stuff
26
26
  *
27
27
  * @param path The path to be splitted
28
28
  * @returns {string[]} the segments for the path
29
29
  */
30
30
  public static getSegments(path: string): string[] {
31
- return path
32
- .split(/\/|[(][']|[(]/g)
33
- .filter((segment) => segment && segment.length)
34
- .map((segment) => {
35
- if (segment.endsWith("')")) {
36
- segment = `('${segment}`
37
- } else if (segment.endsWith(')')) {
38
- segment = `(${segment}`
39
- }
40
- return segment
41
- })
42
- }
43
-
44
- /**
45
- * Checks if a specific segment is an Item segment or not (like "('Content1')"" or "(654)")
46
- *
47
- * @param segment The segment to be examined
48
- * @returns a boolean value that indicates if the segment is an item segment
49
- */
50
- public static isItemSegment(segment: string): boolean {
51
- return RegExp(/^\('+[\s\S]+'\)$/).test(segment) || RegExp(/^\(+\d+\)$/).test(segment)
52
- }
53
-
54
- /**
55
- * Method that tells if a path is an item path or an item reference path (e.g. contains an Item segment).
56
- *
57
- * @param {string} path Path that you want to test.
58
- * @returns {boolean} Returns if the given path is a path of a Content or not.
59
- */
60
- public static isItemPath(path: string): boolean {
61
- const segments = this.getSegments(path)
62
- const itemSegment = segments.find((s) => this.isItemSegment(s))
63
- return itemSegment && itemSegment.length ? true : false
64
- }
65
-
66
- /**
67
- * Returns the full path for a content based on its Id or Path
68
- *
69
- * @param {string | number} idOrPath the Id Or Path of the content
70
- * @returns A full Id or Path-based url of the content (e.g. *'/content(1)'* or *'/Root/Example/('Content')'*)
71
- */
72
- public static getContentUrl(idOrPath: string | number): string {
73
- const parsed = parseInt(idOrPath as string, 10)
74
- if (isNaN(parsed)) {
75
- return this.getContentUrlByPath(idOrPath.toString())
76
- } else {
77
- return this.getContentUrlbyId(parsed)
78
- }
79
- }
80
-
81
- /**
82
- * Method that gets the URL that refers to a single item in the Sense/Net Content Repository
83
- *
84
- * @param {string} path Path that you want to format.
85
- * @returns {string} Path in entity format e.g. /workspaces('project') from /workspaces/project
86
- */
87
- public static getContentUrlByPath(path: string): string {
88
- if (!path) {
89
- throw Error('Path is empty')
90
- }
91
-
92
- const segments = this.getSegments(path)
93
- if (!this.isItemPath(path)) {
94
- segments[segments.length - 1] = `('${segments[segments.length - 1]}')`
95
- }
96
- return segments.join('/')
97
- }
98
-
99
- /**
100
- * Method that gets the URL that refers to a single item in the Sense/Net Content Repository by its Id
101
- *
102
- * @param id {number} Id of the Content.
103
- * @returns {string} e.g. /content(123)
104
- */
105
- public static getContentUrlbyId(id: number): string {
106
- return `content(${id})`
31
+ return path.split('/').filter((segment) => segment && segment.length)
107
32
  }
108
33
 
109
34
  /**
@@ -37,5 +37,5 @@ export class ValueObserver<T> implements Disposable {
37
37
  * @param observable The related Observable object
38
38
  * @param callback The callback that will be fired on change
39
39
  */
40
- constructor(private readonly observable: ObservableValue<T>, public callback: ValueChangeCallback<T>) {}
40
+ constructor(public readonly observable: ObservableValue<T>, public callback: ValueChangeCallback<T>) {}
41
41
  }
@@ -1,20 +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 declare const filterAsync: <T>(values: Iterable<T>, callbackFn: (entry: T) => Promise<boolean>) => Promise<T[]>;
9
- declare global {
10
- /**
11
- * Defines an array of elements
12
- */
13
- export interface Array<T> {
14
- /**
15
- * Returns a promise with a new array of elements that meets the specified async callback
16
- */
17
- filterAsync: (callbackFn: (entry: T) => Promise<boolean>) => Promise<T[]>;
18
- }
19
- }
20
- //# sourceMappingURL=filter-async.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"filter-async.d.ts","sourceRoot":"","sources":["../src/filter-async.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,qDAA4D,QAAQ,OAAO,CAAC,iBAMnG,CAAA;AAED,OAAO,CAAC,MAAM,CAAC;IACb;;OAEG;IACH,MAAM,WAAW,KAAK,CAAC,CAAC;QACtB;;WAEG;QACH,WAAW,EAAE,CAAC,UAAU,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,KAAK,OAAO,CAAC,CAAC,EAAE,CAAC,CAAA;KAC1E;CACF"}
@@ -1,23 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.filterAsync = void 0;
4
- /**
5
- * The async variant of the Array.filter() method that
6
- *
7
- * @param values An iterable of elements to filter
8
- * @param callbackFn The async callback that will be executed on the elements
9
- * @returns Promise<T[]>
10
- */
11
- const filterAsync = async (values, callbackFn) => {
12
- const returns = [];
13
- for (const value of values) {
14
- ;
15
- (await callbackFn(value)) && returns.push(value);
16
- }
17
- return returns;
18
- };
19
- exports.filterAsync = filterAsync;
20
- Array.prototype.filterAsync = function (callbackFn) {
21
- return (0, exports.filterAsync)(this, callbackFn);
22
- };
23
- //# sourceMappingURL=filter-async.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"filter-async.js","sourceRoot":"","sources":["../src/filter-async.ts"],"names":[],"mappings":";;;AAAA;;;;;;GAMG;AACI,MAAM,WAAW,GAAG,KAAK,EAAK,MAAmB,EAAE,UAA0C,EAAE,EAAE;IACtG,MAAM,OAAO,GAAG,EAAE,CAAA;IAClB,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE;QAC1B,CAAC;QAAA,CAAC,MAAM,UAAU,CAAC,KAAK,CAAC,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;KAClD;IACD,OAAO,OAAO,CAAA;AAChB,CAAC,CAAA;AANY,QAAA,WAAW,eAMvB;AAaD,KAAK,CAAC,SAAS,CAAC,WAAW,GAAG,UAAU,UAAU;IAChD,OAAO,IAAA,mBAAW,EAAC,IAAI,EAAE,UAAU,CAAC,CAAA;AACtC,CAAC,CAAA"}
@@ -1,2 +0,0 @@
1
- import './filter-async';
2
- //# sourceMappingURL=filter-async.spec.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"filter-async.spec.d.ts","sourceRoot":"","sources":["../src/filter-async.spec.ts"],"names":[],"mappings":"AAAA,OAAO,gBAAgB,CAAA"}
@@ -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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I,IAAI,CAAC,OAAO,CAAC,OAAO,GAAG,OAAO,EAAE;YAChE,OAAO,EAAE,CAAA;YACT,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE;gBACtB,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAA;aACrB;YACD,SAAS,GAAG,MAAM,IAAI,CAAC,QAAQ,EAAE,CAAA;YACjC,IAAI,CAAC,SAAS,EAAE;gBACd,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,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,Q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AA;IAEF,EAAE,CAAC,2DAA2D,EAAE,GAAG,EAAE;QACnE,MAAM,OAAO,GAAG,iBAAO,CAAC,MAAM,CAAC,KAAK,IAAI,EAAE,CAAC,KAAK,CAAC,CAAA;QACjD,OAAO,CAAC,GAAG,EAAE,CAAA;QACb,MAAM,CAAC,GAAG,EAAE;YACV,OAAO,CAAC,KAAK,CAAC;gBACZ,OAAO,EAAE,CAAC;aACX,CAAC,CAAA;QACJ,CAAC,CAAC,CAAC,OAAO,EAAE,CAAA;IACd,CAAC,CAAC,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
- }