@creejs/commons-retrier 1.0.8 → 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.
package/lib/event.js DELETED
@@ -1,21 +0,0 @@
1
- export const Start = 'start' // retry started
2
- export const Stop = 'stop' // retry stopped
3
- export const Retry = 'retry' // one retry began
4
- export const Success = 'success' // one task running succeeded
5
- export const Failure = 'failure' // one task ran failed
6
- export const Timeout = 'timeout' // total timeout
7
- export const TaskTimeout = 'task-timeout' // one task timed out
8
- export const Completed = 'complete' // all retries completed
9
- export const MaxRetries = 'max-retries' // Reach the max retries
10
-
11
- export default {
12
- Start,
13
- Retry,
14
- Success,
15
- Failure,
16
- Timeout,
17
- TaskTimeout,
18
- Stop,
19
- Completed,
20
- MaxRetries
21
- }
package/lib/index.js DELETED
@@ -1,44 +0,0 @@
1
- // 3rd
2
- import { LangUtils } from '@creejs/commons-lang'
3
-
4
- // owned
5
- import Policy from './policy.js'
6
- import Retrier from './retrier.js'
7
- import Event from './event.js'
8
- import RetrierFactory from './retrier-factory.js'
9
-
10
- /**
11
- * Add all factory methods to Retrier as static methods.
12
- * Now we can create do something like this:
13
- * ```
14
- * Retrier.name('myRetrier')
15
- * Retrier.infinite()
16
- * ...
17
- * ```
18
- */
19
- LangUtils.defaults(Retrier, RetrierFactory)
20
-
21
- /**
22
- * default export to support
23
- * 1. import CommonsRetrier from '@creejs/commons-retrier'
24
- */
25
- export default {
26
- Policy,
27
- Retrier,
28
- Event,
29
- RetrierFactory,
30
- ...RetrierFactory
31
- }
32
-
33
- /**
34
- * named export to support
35
- * 1. import { Retrier } from '@creejs/commons-retrier'
36
- * 2. import * as CommonsRetrier from '@creejs/commons-retrier'
37
- */
38
- export {
39
- Policy,
40
- Retrier,
41
- Event,
42
- RetrierFactory
43
- }
44
- export * from './retrier-factory.js'
@@ -1,46 +0,0 @@
1
- // 3rd
2
- // internal
3
- import { TypeAssert } from '@creejs/commons-lang'
4
- // owned
5
- import Policy from '../policy.js'
6
- // module vars
7
- const { assertPositive } = TypeAssert
8
-
9
- export default class FactoreIncreasePolicy extends Policy {
10
- /**
11
- * each call to _next() increases the interval by lastInterval * factor
12
- * @param {number} factor - the increasement factor, >= 1
13
- */
14
- constructor (factor) {
15
- super()
16
- assertPositive(factor, 'factor')
17
- if (factor < 1) {
18
- throw new Error('factor must be >= 1')
19
- }
20
- this._factor = factor
21
- }
22
-
23
- set factor (factor) {
24
- assertPositive(factor, 'factor')
25
- if (factor < 1) {
26
- throw new Error('factor must be >= 1')
27
- }
28
- this._factor = factor
29
- }
30
-
31
- get factor () {
32
- return this._factor
33
- }
34
-
35
- /**
36
- * Interval ms of next execution
37
- * @returns {number}
38
- */
39
- _next () {
40
- if (this._nextInterval >= this._max) {
41
- return this._max
42
- }
43
- return this._nextInterval * this.factor
44
- }
45
- }
46
- export { FactoreIncreasePolicy as FactoreIncreasePolicyType }
@@ -1,39 +0,0 @@
1
- // 3rd
2
- // internal
3
- import { TypeAssert } from '@creejs/commons-lang'
4
- // owned
5
- import Policy from '../policy.js'
6
- // module vars
7
- const { assertPositive } = TypeAssert
8
- export default class FixedIncreasePolicy extends Policy {
9
- /**
10
- * each call to _next() increases the interval by "increasement".
11
- * @param {number} increasement - The fixed interval (in milliseconds) between retry attempts.
12
- */
13
- constructor (increasement) {
14
- super()
15
- assertPositive(increasement, 'increasement')
16
- this._increasement = increasement
17
- }
18
-
19
- set increasement (increasement) {
20
- assertPositive(increasement, 'increasement')
21
- this._increasement = increasement
22
- }
23
-
24
- get increasement () {
25
- return this._increasement
26
- }
27
-
28
- /**
29
- * Interval ms of next execution
30
- * @returns {number}
31
- */
32
- _next () {
33
- if (this._nextInterval >= this._max) {
34
- return this._max
35
- }
36
- return this._nextInterval + this.increasement
37
- }
38
- }
39
- export { FixedIncreasePolicy as FixedIncreasePolicyType }
@@ -1,38 +0,0 @@
1
- // 3rd
2
- // internal
3
- import { TypeAssert } from '@creejs/commons-lang'
4
- // owned
5
- import Policy from '../policy.js'
6
- // module vars
7
- const { assertPositive } = TypeAssert
8
- export default class FixedIntervalPolicy extends Policy {
9
- /**
10
- * Creates a fixed interval retry policy with the specified interval.
11
- * @param {number} interval - The fixed interval (in milliseconds) between retry attempts.
12
- */
13
- constructor (interval) {
14
- super()
15
- assertPositive(interval, 'interval')
16
- this._interval = interval
17
- }
18
-
19
- set interval (interval) {
20
- assertPositive(interval, 'interval')
21
- this._interval = interval
22
- }
23
-
24
- get interval () {
25
- return this._interval
26
- }
27
-
28
- /**
29
- * Interval ms of next execution
30
- * @returns {number}
31
- * @throws {Error} Always throws "Not Implemented Yet" error.
32
- */
33
- _next () {
34
- return this.interval
35
- }
36
- }
37
-
38
- export { FixedIntervalPolicy as FixedIntervalPolicyType }
@@ -1,50 +0,0 @@
1
- // 3rd
2
- // internal
3
- import { TypeAssert } from '@creejs/commons-lang'
4
-
5
- // owned
6
- import Policy from '../policy.js'
7
- // module vars
8
- const { assertPositive } = TypeAssert
9
-
10
- export default class ShuttlePolicy extends Policy {
11
- /**
12
- * the inteval value shuttles between min and max
13
- * @param {number} stepLength - the step length to change
14
- */
15
- constructor (stepLength) {
16
- super()
17
- assertPositive(stepLength, 'stepLength')
18
- this._stepLength = stepLength
19
- this.increasement = stepLength
20
- }
21
-
22
- set stepLength (stepLength) {
23
- assertPositive(stepLength, 'stepLength')
24
- this._stepLength = stepLength
25
- this.increasement = stepLength
26
- }
27
-
28
- get stepLength () {
29
- return this._stepLength
30
- }
31
-
32
- /**
33
- * Interval ms of next execution
34
- * @returns {number}
35
- * @throws {Error} Always throws "Not Implemented Yet" error.
36
- */
37
- _next () {
38
- const nextInterval = this._nextInterval + this.increasement
39
- if (nextInterval >= this._max) {
40
- this.increasement = -this.stepLength
41
- return this._max
42
- } else if (nextInterval <= this._min) {
43
- this.increasement = this.stepLength
44
- return this._min
45
- }
46
- return nextInterval
47
- }
48
- }
49
-
50
- export { ShuttlePolicy as ShuttlePolicyType }
package/lib/policy.js DELETED
@@ -1,130 +0,0 @@
1
- // internal
2
- import { TypeAssert, TypeUtils } from '@creejs/commons-lang'
3
- // owned
4
- import { DefaultMaxInterval, DefaultMinInterval } from './constants.js'
5
-
6
- // module vars
7
- const { assertPositive } = TypeAssert
8
- const { isNumber } = TypeUtils
9
-
10
- export default class Policy {
11
- /**
12
- * Creates a new Policy instance with specified retry bounds.
13
- */
14
- constructor () {
15
- this._min = DefaultMinInterval
16
- this._max = DefaultMaxInterval
17
- this._nextInterval = this._min
18
- }
19
-
20
- /**
21
- * Copies settings to target policy.
22
- * @param {Policy} targetPolicy - The policy to modify.
23
- */
24
- copyPolicySetting (targetPolicy) {
25
- targetPolicy.range(this._min, this._max)
26
- targetPolicy._nextInterval = this._nextInterval
27
- }
28
-
29
- /**
30
- * Sets a fixed interval retry policy.
31
- }
32
-
33
- /**
34
- * Sets the minimum and maximum intervals for retries.
35
- * @param {number} min - Minimum delay in milliseconds (must be positive and less than max)
36
- * @param {number} max - Maximum delay in milliseconds (must be positive and greater than min)
37
- * @returns {this} Returns the Retrier instance for chaining
38
- * @throws {Error} If min is not less than max or if values are not positive
39
- */
40
- range (min, max) {
41
- assertPositive(min, 'min')
42
- assertPositive(max, 'max')
43
- if (min >= max) {
44
- throw new Error('min must < max')
45
- }
46
- this._min = min
47
- if (this._nextInterval < this._min) {
48
- this._nextInterval = this._min
49
- }
50
- this._max = max
51
- if (this._nextInterval > this._max) {
52
- this._nextInterval = this._max
53
- }
54
- return this
55
- }
56
-
57
- /**
58
- * Sets the minimum retry delay in milliseconds.
59
- * 1. will change currentInterval to min
60
- * @param {number} min - The minimum delay (must be positive and less than max).
61
- * @returns {this} The retrier instance for chaining.
62
- * @throws {Error} If min is not positive or is greater than/equal to max.
63
- */
64
- min (min) {
65
- assertPositive(min, 'min')
66
- if (min >= this._max) {
67
- throw new Error('min must < max')
68
- }
69
- this._min = min
70
- this._nextInterval = this._min
71
- return this
72
- }
73
-
74
- /**
75
- * Sets the maximum retry retry delay in milliseconds.
76
- * @param {number} max - The maximum delay (must be positive and greater than min).
77
- * @throws {Error} If max is not greater than min.
78
- * @returns {this} The retrier instance for chaining.
79
- */
80
- max (max) {
81
- assertPositive(max, 'max')
82
- if (max <= this._min) {
83
- throw new Error('max must > min')
84
- }
85
- this._max = max
86
- if (this._nextInterval > this._max) {
87
- this._nextInterval = this._max
88
- }
89
- return this
90
- }
91
-
92
- reset () {
93
- this._nextInterval = this._min
94
- return this
95
- }
96
-
97
- /**
98
- * Interval ms of next execution
99
- * @returns {number}
100
- * @throws {Error} Always throws "Not Implemented Yet" error.
101
- */
102
- generate () {
103
- const rtnVal = this._nextInterval
104
- this._increase()
105
- return rtnVal
106
- }
107
-
108
- _increase () {
109
- const nextInterval = this._next()
110
- if (!isNumber(nextInterval)) {
111
- throw new Error('Generated Next Interval Not Number')
112
- }
113
- if (nextInterval < this._min) {
114
- return (this._nextInterval = this._min)
115
- } else if (nextInterval > this._max) {
116
- return (this._nextInterval = this._max)
117
- }
118
- return (this._nextInterval = nextInterval)
119
- }
120
-
121
- /**
122
- * subclass should implement this method
123
- * @returns {number} The interval in milliseconds to wait before the next retry attempt.
124
- * @protected
125
- */
126
- _next () {
127
- throw new Error('Not Impled Yet')
128
- }
129
- }
130
- export { Policy as PolicyType }
@@ -1,191 +0,0 @@
1
- // owned
2
- import Retrier from './retrier.js'
3
- /**
4
- * Creates a new Retrier instance with the specified name.
5
- * @param {string} name - The name to assign to the retrier.
6
- * @returns {Retrier} A new Retrier instance with the given name.
7
- */
8
- function name (name) {
9
- const retrier = new Retrier()
10
- retrier.name(name)
11
- return retrier
12
- }
13
-
14
- /**
15
- * Creates and returns a Retrier instance configured for infinite retries.
16
- * @returns {Retrier} A Retrier instance with infinite retry behavior.
17
- */
18
- function infinite () {
19
- const retrier = new Retrier()
20
- retrier.infinite()
21
- return retrier
22
- }
23
-
24
- /**
25
- * Creates a retrier configured to attempt an operation a specified number of times.
26
- * @param {number} times - The maximum number of retry attempts.
27
- * @returns {Retrier} A configured Retrier instance with the specified max retries.
28
- */
29
- function times (times) {
30
- const retrier = new Retrier()
31
- retrier.times(times)
32
- return retrier
33
- }
34
-
35
- /**
36
- * Alias for times.
37
- * @param {number} maxRetries - The maximum number of retry attempts.
38
- * @returns {Retrier} A configured Retrier instance with the specified max retries.
39
- */
40
- function maxRetries (maxRetries) {
41
- const retrier = new Retrier()
42
- retrier.maxRetries(maxRetries)
43
- return retrier
44
- }
45
-
46
- /**
47
- * Sets the minimum Interval for the retrier and returns the instance.
48
- * @param {number} min - The minimum Interval in milliseconds.
49
- * @returns {Retrier} The retrier instance with updated minimum Interval.
50
- */
51
- function min (min) {
52
- const retrier = new Retrier()
53
- retrier.min(min)
54
- return retrier
55
- }
56
-
57
- /**
58
- * Sets the maximum Interval for the retrier and returns the instance.
59
- * @param {number} max - The maximum Interval in milliseconds.
60
- * @returns {Retrier} The retrier instance with updated maximum Interval.
61
- */
62
- function max (max) {
63
- const retrier = new Retrier()
64
- retrier.max(max)
65
- return retrier
66
- }
67
-
68
- /**
69
- * Creates a retrier with the specified Interval range.
70
- * @param {number} min - Minimum Interval.
71
- * @param {number} max - Maximum Interval.
72
- * @returns {Retrier} A new Retrier instance configured with specified Interval range.
73
- */
74
- function range (min, max) {
75
- const retrier = new Retrier()
76
- retrier.range(min, max)
77
- return retrier
78
- }
79
-
80
- /**
81
- * Creates a retrier with a fixed interval between attempts.
82
- * @param {number} fixedInterval - The fixed interval in milliseconds between retry attempts.
83
- * @returns {Retrier} A new Retrier instance configured with the specified fixed interval.
84
- */
85
- function fixedInterval (fixedInterval) {
86
- const retrier = new Retrier()
87
- retrier.fixedInterval(fixedInterval)
88
- return retrier
89
- }
90
-
91
- /**
92
- * Creates a retrier with a fixed increase strategy.
93
- * @param {number} increasement - The fixed amount to increase on each retry.
94
- * @returns {Retrier} A retrier instance configured with fixed increase.
95
- */
96
- function fixedIncrease (increasement) {
97
- const retrier = new Retrier()
98
- retrier.fixedIncrease(increasement)
99
- return retrier
100
- }
101
-
102
- /**
103
- * Creates a new Retrier instance with factor-increase strategy.
104
- * @param {number} factor - The factor by which to increase the interval on each retry.
105
- * @returns {Retrier} A new Retrier instance with factor-increase strategy.
106
- */
107
- function factorIncrease (factor) {
108
- const retrier = new Retrier()
109
- retrier.factorIncrease(factor)
110
- return retrier
111
- }
112
-
113
- /**
114
- * Creates a Retrier instance with a shuttle-interval strategt.
115
- * @param {number} stepLength - The interval step length of each change
116
- * @returns {Retrier} A configured Retrier instance with shuttle-interval strategt.
117
- */
118
- function shuttleInterval (stepLength) {
119
- const retrier = new Retrier()
120
- retrier.shuttleInterval(stepLength)
121
- return retrier
122
- }
123
-
124
- /**
125
- * Creates a Retrier instance with a total-opertion timeout.
126
- * @param {number} timeout - The timeout value in milliseconds.
127
- * @returns {Retrier} A Retrier instance configured with the given timeout.
128
- */
129
- function timeout (timeout) {
130
- const retrier = new Retrier()
131
- retrier.timeout(timeout)
132
- return retrier
133
- }
134
-
135
- /**
136
- * Creates a retrier instance with a single-task timeout.
137
- * @param {number} timeout - The timeout duration in milliseconds for the retrier task.
138
- * @returns {Retrier} A Retrier instance configured with the given task timeout.
139
- */
140
- function taskTimeout (timeout) {
141
- const retrier = new Retrier()
142
- retrier.taskTimeout(timeout)
143
- return retrier
144
- }
145
-
146
- /**
147
- * Creates a new Retrier instance, sets the task to be retried, and starts the retry process.
148
- * @param {Function} task - The asynchronous task function to be retried.
149
- * @returns {Promise<*>} A promise that resolves when the retry process completes.
150
- */
151
- function start (task) {
152
- const retrier = new Retrier()
153
- retrier.task(task)
154
- return retrier.start()
155
- }
156
-
157
- // default export
158
- export default {
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
- }
174
-
175
- // named export
176
- export {
177
- name,
178
- infinite,
179
- times,
180
- maxRetries,
181
- min,
182
- max,
183
- range,
184
- fixedInterval,
185
- fixedIncrease,
186
- factorIncrease,
187
- shuttleInterval,
188
- timeout,
189
- taskTimeout,
190
- start
191
- }