@creejs/commons-retrier 2.0.0 → 2.0.2

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 (39) hide show
  1. package/README.md +2 -4
  2. package/dist/cjs/index-dev.cjs +1151 -0
  3. package/dist/cjs/index-dev.cjs.map +1 -0
  4. package/dist/cjs/index-min.cjs +2 -0
  5. package/dist/cjs/index-min.cjs.map +1 -0
  6. package/dist/esm/index-dev.js +1129 -0
  7. package/dist/esm/index-dev.js.map +1 -0
  8. package/dist/esm/index-min.js +2 -0
  9. package/dist/esm/index-min.js.map +1 -0
  10. package/dist/umd/index.dev.js +1157 -0
  11. package/dist/umd/index.dev.js.map +1 -0
  12. package/dist/umd/index.min.js +2 -0
  13. package/dist/umd/index.min.js.map +1 -0
  14. package/index.js +2 -3
  15. package/package.json +41 -8
  16. package/types/alway-task.d.ts +7 -4
  17. package/types/constants.d.ts +6 -0
  18. package/types/event.d.ts +13 -1
  19. package/types/index.d.ts +49 -22
  20. package/types/policy/factor-increase-policy.d.ts +4 -4
  21. package/types/policy/fixed-increase-policy.d.ts +4 -4
  22. package/types/policy/fixed-interval-policy.d.ts +4 -4
  23. package/types/policy/shuttle-policy.d.ts +4 -4
  24. package/types/policy.d.ts +3 -3
  25. package/types/retrier-factory.d.ts +18 -1
  26. package/types/retrier.d.ts +10 -10
  27. package/types/task.d.ts +4 -4
  28. package/lib/alway-task.js +0 -44
  29. package/lib/constants.js +0 -11
  30. package/lib/event.js +0 -22
  31. package/lib/index.js +0 -28
  32. package/lib/policy/factor-increase-policy.js +0 -46
  33. package/lib/policy/fixed-increase-policy.js +0 -40
  34. package/lib/policy/fixed-interval-policy.js +0 -38
  35. package/lib/policy/shuttle-policy.js +0 -49
  36. package/lib/policy.js +0 -131
  37. package/lib/retrier-factory.js +0 -173
  38. package/lib/retrier.js +0 -534
  39. package/lib/task.js +0 -56
package/lib/policy.js DELETED
@@ -1,131 +0,0 @@
1
- 'use strict'
2
- // internal
3
- const {
4
- TypeAssert: { assertPositive },
5
- TypeUtils: { isNumber }
6
- } = require('@creejs/commons-lang')
7
- // owned
8
- const { DefaultMaxInterval, DefaultMinInterval } = require('./constants')
9
- // module vars
10
-
11
- class Policy {
12
- /**
13
- * Creates a new Policy instance with specified retry bounds.
14
- */
15
- constructor () {
16
- this._min = DefaultMinInterval
17
- this._max = DefaultMaxInterval
18
- this._nextInterval = this._min
19
- }
20
-
21
- /**
22
- * Copies settings to target policy.
23
- * @param {Policy} targetPolicy - The policy to modify.
24
- */
25
- copyPolicySetting (targetPolicy) {
26
- targetPolicy.range(this._min, this._max)
27
- targetPolicy._nextInterval = this._nextInterval
28
- }
29
-
30
- /**
31
- * Sets a fixed interval retry policy.
32
- }
33
-
34
- /**
35
- * Sets the minimum and maximum intervals for retries.
36
- * @param {number} min - Minimum delay in milliseconds (must be positive and less than max)
37
- * @param {number} max - Maximum delay in milliseconds (must be positive and greater than min)
38
- * @returns {this} Returns the Retrier instance for chaining
39
- * @throws {Error} If min is not less than max or if values are not positive
40
- */
41
- range (min, max) {
42
- assertPositive(min, 'min')
43
- assertPositive(max, 'max')
44
- if (min >= max) {
45
- throw new Error('min must < max')
46
- }
47
- this._min = min
48
- if (this._nextInterval < this._min) {
49
- this._nextInterval = this._min
50
- }
51
- this._max = max
52
- if (this._nextInterval > this._max) {
53
- this._nextInterval = this._max
54
- }
55
- return this
56
- }
57
-
58
- /**
59
- * Sets the minimum retry delay in milliseconds.
60
- * 1. will change currentInterval to min
61
- * @param {number} min - The minimum delay (must be positive and less than max).
62
- * @returns {this} The retrier instance for chaining.
63
- * @throws {Error} If min is not positive or is greater than/equal to max.
64
- */
65
- min (min) {
66
- assertPositive(min, 'min')
67
- if (min >= this._max) {
68
- throw new Error('min must < max')
69
- }
70
- this._min = min
71
- this._nextInterval = this._min
72
- return this
73
- }
74
-
75
- /**
76
- * Sets the maximum retry retry delay in milliseconds.
77
- * @param {number} max - The maximum delay (must be positive and greater than min).
78
- * @throws {Error} If max is not greater than min.
79
- * @returns {this} The retrier instance for chaining.
80
- */
81
- max (max) {
82
- assertPositive(max, 'max')
83
- if (max <= this._min) {
84
- throw new Error('max must > min')
85
- }
86
- this._max = max
87
- if (this._nextInterval > this._max) {
88
- this._nextInterval = this._max
89
- }
90
- return this
91
- }
92
-
93
- reset () {
94
- this._nextInterval = this._min
95
- return this
96
- }
97
-
98
- /**
99
- * Interval ms of next execution
100
- * @returns {number}
101
- * @throws {Error} Always throws "Not Implemented Yet" error.
102
- */
103
- generate () {
104
- const rtnVal = this._nextInterval
105
- this._increase()
106
- return rtnVal
107
- }
108
-
109
- _increase () {
110
- const nextInterval = this._next()
111
- if (!isNumber(nextInterval)) {
112
- throw new Error('Generated Next Interval Not Number')
113
- }
114
- if (nextInterval < this._min) {
115
- return (this._nextInterval = this._min)
116
- } else if (nextInterval > this._max) {
117
- return (this._nextInterval = this._max)
118
- }
119
- return (this._nextInterval = nextInterval)
120
- }
121
-
122
- /**
123
- * subclass should implement this method
124
- * @returns {number} The interval in milliseconds to wait before the next retry attempt.
125
- * @protected
126
- */
127
- _next () {
128
- throw new Error('Not Impled Yet')
129
- }
130
- }
131
- module.exports = Policy
@@ -1,173 +0,0 @@
1
- 'use strict'
2
- // owned
3
- const Retrier = require('./retrier')
4
- /**
5
- * Creates a new Retrier instance with the specified name.
6
- * @param {string} name - The name to assign to the retrier.
7
- * @returns {Retrier} A new Retrier instance with the given name.
8
- */
9
- function name (name) {
10
- const retrier = new Retrier()
11
- retrier.name(name)
12
- return retrier
13
- }
14
-
15
- /**
16
- * Creates and returns a Retrier instance configured for infinite retries.
17
- * @returns {Retrier} A Retrier instance with infinite retry behavior.
18
- */
19
- function infinite () {
20
- const retrier = new Retrier()
21
- retrier.infinite()
22
- return retrier
23
- }
24
-
25
- /**
26
- * Creates a retrier configured to attempt an operation a specified number of times.
27
- * @param {number} times - The maximum number of retry attempts.
28
- * @returns {Retrier} A configured Retrier instance with the specified max retries.
29
- */
30
- function times (times) {
31
- const retrier = new Retrier()
32
- retrier.times(times)
33
- return retrier
34
- }
35
-
36
- /**
37
- * Alias for times.
38
- * @param {number} maxRetries - The maximum number of retry attempts.
39
- * @returns {Retrier} A configured Retrier instance with the specified max retries.
40
- */
41
- function maxRetries (maxRetries) {
42
- const retrier = new Retrier()
43
- retrier.maxRetries(maxRetries)
44
- return retrier
45
- }
46
-
47
- /**
48
- * Sets the minimum Interval for the retrier and returns the instance.
49
- * @param {number} min - The minimum Interval in milliseconds.
50
- * @returns {Retrier} The retrier instance with updated minimum Interval.
51
- */
52
- function min (min) {
53
- const retrier = new Retrier()
54
- retrier.min(min)
55
- return retrier
56
- }
57
-
58
- /**
59
- * Sets the maximum Interval for the retrier and returns the instance.
60
- * @param {number} max - The maximum Interval in milliseconds.
61
- * @returns {Retrier} The retrier instance with updated maximum Interval.
62
- */
63
- function max (max) {
64
- const retrier = new Retrier()
65
- retrier.max(max)
66
- return retrier
67
- }
68
-
69
- /**
70
- * Creates a retrier with the specified Interval range.
71
- * @param {number} min - Minimum Interval.
72
- * @param {number} max - Maximum Interval.
73
- * @returns {Retrier} A new Retrier instance configured with specified Interval range.
74
- */
75
- function range (min, max) {
76
- const retrier = new Retrier()
77
- retrier.range(min, max)
78
- return retrier
79
- }
80
-
81
- /**
82
- * Creates a retrier with a fixed interval between attempts.
83
- * @param {number} fixedInterval - The fixed interval in milliseconds between retry attempts.
84
- * @returns {Retrier} A new Retrier instance configured with the specified fixed interval.
85
- */
86
- function fixedInterval (fixedInterval) {
87
- const retrier = new Retrier()
88
- retrier.fixedInterval(fixedInterval)
89
- return retrier
90
- }
91
-
92
- /**
93
- * Creates a retrier with a fixed increase strategy.
94
- * @param {number} increasement - The fixed amount to increase on each retry.
95
- * @returns {Retrier} A retrier instance configured with fixed increase.
96
- */
97
- function fixedIncrease (increasement) {
98
- const retrier = new Retrier()
99
- retrier.fixedIncrease(increasement)
100
- return retrier
101
- }
102
-
103
- /**
104
- * Creates a new Retrier instance with factor-increase strategy.
105
- * @param {number} factor - The factor by which to increase the interval on each retry.
106
- * @returns {Retrier} A new Retrier instance with factor-increase strategy.
107
- */
108
- function factorIncrease (factor) {
109
- const retrier = new Retrier()
110
- retrier.factorIncrease(factor)
111
- return retrier
112
- }
113
-
114
- /**
115
- * Creates a Retrier instance with a shuttle-interval strategt.
116
- * @param {number} stepLength - The interval step length of each change
117
- * @returns {Retrier} A configured Retrier instance with shuttle-interval strategt.
118
- */
119
- function shuttleInterval (stepLength) {
120
- const retrier = new Retrier()
121
- retrier.shuttleInterval(stepLength)
122
- return retrier
123
- }
124
-
125
- /**
126
- * Creates a Retrier instance with a total-opertion timeout.
127
- * @param {number} timeout - The timeout value in milliseconds.
128
- * @returns {Retrier} A Retrier instance configured with the given timeout.
129
- */
130
- function timeout (timeout) {
131
- const retrier = new Retrier()
132
- retrier.timeout(timeout)
133
- return retrier
134
- }
135
-
136
- /**
137
- * Creates a retrier instance with a single-task timeout.
138
- * @param {number} timeout - The timeout duration in milliseconds for the retrier task.
139
- * @returns {Retrier} A Retrier instance configured with the given task timeout.
140
- */
141
- function taskTimeout (timeout) {
142
- const retrier = new Retrier()
143
- retrier.taskTimeout(timeout)
144
- return retrier
145
- }
146
-
147
- /**
148
- * Creates a new Retrier instance, sets the task to be retried, and starts the retry process.
149
- * @param {Function} task - The asynchronous task function to be retried.
150
- * @returns {Promise<*>} A promise that resolves when the retry process completes.
151
- */
152
- function start (task) {
153
- const retrier = new Retrier()
154
- retrier.task(task)
155
- return retrier.start()
156
- }
157
-
158
- module.exports = {
159
- name,
160
- infinite,
161
- times,
162
- maxRetries,
163
- min,
164
- max,
165
- range,
166
- fixedInterval,
167
- fixedIncrease,
168
- factorIncrease,
169
- shuttleInterval,
170
- timeout,
171
- taskTimeout,
172
- start
173
- }