@furystack/utils 2.0.2 → 2.0.5

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 (46) 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 +10 -12
  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/sleep-async.d.ts +0 -1
  14. package/dist/sleep-async.d.ts.map +1 -1
  15. package/dist/sleep-async.js +0 -1
  16. package/dist/sleep-async.js.map +1 -1
  17. package/package.json +4 -5
  18. package/src/index.ts +0 -2
  19. package/src/path-helper.spec.ts +2 -101
  20. package/src/path-helper.ts +2 -77
  21. package/src/sleep-async.ts +0 -1
  22. package/dist/filter-async.d.ts +0 -20
  23. package/dist/filter-async.d.ts.map +0 -1
  24. package/dist/filter-async.js +0 -23
  25. package/dist/filter-async.js.map +0 -1
  26. package/dist/filter-async.spec.d.ts +0 -2
  27. package/dist/filter-async.spec.d.ts.map +0 -1
  28. package/dist/filter-async.spec.js +0 -25
  29. package/dist/filter-async.spec.js.map +0 -1
  30. package/dist/retrier-options.d.ts +0 -50
  31. package/dist/retrier-options.d.ts.map +0 -1
  32. package/dist/retrier-options.js +0 -54
  33. package/dist/retrier-options.js.map +0 -1
  34. package/dist/retrier.d.ts +0 -52
  35. package/dist/retrier.d.ts.map +0 -1
  36. package/dist/retrier.js +0 -103
  37. package/dist/retrier.js.map +0 -1
  38. package/dist/retries.spec.d.ts +0 -5
  39. package/dist/retries.spec.d.ts.map +0 -1
  40. package/dist/retries.spec.js +0 -93
  41. package/dist/retries.spec.js.map +0 -1
  42. package/src/filter-async.spec.ts +0 -25
  43. package/src/filter-async.ts +0 -29
  44. package/src/retrier-options.ts +0 -65
  45. package/src/retrier.ts +0 -105
  46. package/src/retries.spec.ts +0 -98
@@ -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
- }
@@ -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
- })