@furystack/utils 1.2.45 → 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 (44) 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/dist/value-observer.d.ts +1 -1
  13. package/dist/value-observer.d.ts.map +1 -1
  14. package/dist/value-observer.js.map +1 -1
  15. package/package.json +3 -3
  16. package/src/index.ts +0 -2
  17. package/src/path-helper.spec.ts +2 -101
  18. package/src/path-helper.ts +2 -77
  19. package/src/value-observer.ts +1 -1
  20. package/dist/filter-async.d.ts +0 -20
  21. package/dist/filter-async.d.ts.map +0 -1
  22. package/dist/filter-async.js +0 -23
  23. package/dist/filter-async.js.map +0 -1
  24. package/dist/filter-async.spec.d.ts +0 -2
  25. package/dist/filter-async.spec.d.ts.map +0 -1
  26. package/dist/filter-async.spec.js +0 -25
  27. package/dist/filter-async.spec.js.map +0 -1
  28. package/dist/retrier-options.d.ts +0 -50
  29. package/dist/retrier-options.d.ts.map +0 -1
  30. package/dist/retrier-options.js +0 -54
  31. package/dist/retrier-options.js.map +0 -1
  32. package/dist/retrier.d.ts +0 -52
  33. package/dist/retrier.d.ts.map +0 -1
  34. package/dist/retrier.js +0 -103
  35. package/dist/retrier.js.map +0 -1
  36. package/dist/retries.spec.d.ts +0 -5
  37. package/dist/retries.spec.d.ts.map +0 -1
  38. package/dist/retries.spec.js +0 -93
  39. package/dist/retries.spec.js.map +0 -1
  40. package/src/filter-async.spec.ts +0 -25
  41. package/src/filter-async.ts +0 -29
  42. package/src/retrier-options.ts +0 -65
  43. package/src/retrier.ts +0 -105
  44. package/src/retries.spec.ts +0 -98
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
- }
@@ -1,98 +0,0 @@
1
- import { Retrier } from './retrier'
2
-
3
- /**
4
- * Retrier tests
5
- */
6
- export const retrierTests = describe('Retrier', () => {
7
- describe('Counter', () => {
8
- it('Should be able to count to 3', async () => {
9
- let count = 0
10
- await Retrier.create(async () => {
11
- count = count + 1
12
- return count === 3
13
- }).run()
14
- expect(count).toBe(3)
15
- })
16
- })
17
-
18
- describe('events', () => {
19
- it('should trigger onSuccess on success', async () => {
20
- let triggered = false
21
- await Retrier.create(async () => true)
22
- .setup({
23
- onSuccess: () => {
24
- triggered = true
25
- },
26
- })
27
- .run()
28
-
29
- expect(triggered).toBe(true)
30
- })
31
-
32
- it('should trigger onTimeout on timeout', async () => {
33
- let triggered = false
34
- await Retrier.create(async () => false)
35
- .setup({
36
- onFail: () => {
37
- triggered = true
38
- },
39
- timeoutMs: 1,
40
- })
41
- .run()
42
-
43
- expect(triggered).toBe(true)
44
- })
45
-
46
- it('should trigger onTry on each try', async () => {
47
- let triggered = false
48
- await Retrier.create(async () => true)
49
- .setup({
50
- onTry: () => {
51
- triggered = true
52
- },
53
- })
54
- .run()
55
- expect(triggered).toBe(true)
56
- })
57
- })
58
-
59
- it('should work with an example test', async () => {
60
- const funcToRetry: () => Promise<boolean> = async () => {
61
- const hasSucceeded = false
62
- // ...
63
- // custom logic
64
- // ...
65
- return hasSucceeded
66
- }
67
- const retrierSuccess = await Retrier.create(funcToRetry)
68
- .setup({
69
- Retries: 3,
70
- RetryIntervalMs: 1,
71
- timeoutMs: 1000,
72
- })
73
- .run()
74
-
75
- expect(retrierSuccess).toBe(false)
76
- })
77
-
78
- it('should throw error when started twice', async () => {
79
- const retrier = Retrier.create(async () => false)
80
- retrier.run()
81
- try {
82
- await retrier.run()
83
- throw Error('Should have been failed')
84
- } catch (error) {
85
- // ignore
86
- }
87
- })
88
-
89
- it('should throw an error when trying to set up after started', () => {
90
- const retrier = Retrier.create(async () => false)
91
- retrier.run()
92
- expect(() => {
93
- retrier.setup({
94
- Retries: 2,
95
- })
96
- }).toThrow()
97
- })
98
- })