@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/types/event.d.ts CHANGED
@@ -1,9 +1,21 @@
1
1
  export const Start: "start";
2
+ export const Stop: "stop";
2
3
  export const Retry: "retry";
3
4
  export const Success: "success";
4
5
  export const Failure: "failure";
5
6
  export const Timeout: "timeout";
6
7
  export const TaskTimeout: "task-timeout";
7
- export const Stop: "stop";
8
8
  export const Completed: "complete";
9
9
  export const MaxRetries: "max-retries";
10
+ declare namespace _default {
11
+ export { Start };
12
+ export { Retry };
13
+ export { Success };
14
+ export { Failure };
15
+ export { Timeout };
16
+ export { TaskTimeout };
17
+ export { Stop };
18
+ export { Completed };
19
+ export { MaxRetries };
20
+ }
21
+ export default _default;
package/types/index.d.ts CHANGED
@@ -1,25 +1,52 @@
1
- declare const _exports: {
2
- name: typeof RetrierFactory.name;
3
- infinite: typeof RetrierFactory.infinite;
4
- times: typeof RetrierFactory.times;
5
- maxRetries: typeof RetrierFactory.maxRetries;
6
- min: typeof RetrierFactory.min;
7
- max: typeof RetrierFactory.max;
8
- range: typeof RetrierFactory.range;
9
- fixedInterval: typeof RetrierFactory.fixedInterval;
10
- fixedIncrease: typeof RetrierFactory.fixedIncrease;
11
- factorIncrease: typeof RetrierFactory.factorIncrease;
12
- shuttleInterval: typeof RetrierFactory.shuttleInterval;
13
- timeout: typeof RetrierFactory.timeout;
14
- taskTimeout: typeof RetrierFactory.taskTimeout;
15
- start: typeof RetrierFactory.start;
1
+ declare const _default: {
2
+ name: typeof import("./retrier-factory.js").name;
3
+ infinite: typeof import("./retrier-factory.js").infinite;
4
+ times: typeof import("./retrier-factory.js").times;
5
+ maxRetries: typeof import("./retrier-factory.js").maxRetries;
6
+ min: typeof import("./retrier-factory.js").min;
7
+ max: typeof import("./retrier-factory.js").max;
8
+ range: typeof import("./retrier-factory.js").range;
9
+ fixedInterval: typeof import("./retrier-factory.js").fixedInterval;
10
+ fixedIncrease: typeof import("./retrier-factory.js").fixedIncrease;
11
+ factorIncrease: typeof import("./retrier-factory.js").factorIncrease;
12
+ shuttleInterval: typeof import("./retrier-factory.js").shuttleInterval;
13
+ timeout: typeof import("./retrier-factory.js").timeout;
14
+ taskTimeout: typeof import("./retrier-factory.js").taskTimeout;
15
+ start: typeof import("./retrier-factory.js").start;
16
16
  Policy: typeof Policy;
17
17
  Retrier: typeof Retrier;
18
- Event: typeof Event;
19
- RetrierFactory: typeof RetrierFactory;
18
+ Event: {
19
+ Start: string;
20
+ Retry: string;
21
+ Success: string;
22
+ Failure: string;
23
+ Timeout: string;
24
+ TaskTimeout: string;
25
+ Stop: string;
26
+ Completed: string;
27
+ MaxRetries: string;
28
+ };
29
+ RetrierFactory: {
30
+ name: typeof import("./retrier-factory.js").name;
31
+ infinite: typeof import("./retrier-factory.js").infinite;
32
+ times: typeof import("./retrier-factory.js").times;
33
+ maxRetries: typeof import("./retrier-factory.js").maxRetries;
34
+ min: typeof import("./retrier-factory.js").min;
35
+ max: typeof import("./retrier-factory.js").max;
36
+ range: typeof import("./retrier-factory.js").range;
37
+ fixedInterval: typeof import("./retrier-factory.js").fixedInterval;
38
+ fixedIncrease: typeof import("./retrier-factory.js").fixedIncrease;
39
+ factorIncrease: typeof import("./retrier-factory.js").factorIncrease;
40
+ shuttleInterval: typeof import("./retrier-factory.js").shuttleInterval;
41
+ timeout: typeof import("./retrier-factory.js").timeout;
42
+ taskTimeout: typeof import("./retrier-factory.js").taskTimeout;
43
+ start: typeof import("./retrier-factory.js").start;
44
+ };
20
45
  };
21
- export = _exports;
22
- import RetrierFactory = require("./retrier-factory");
23
- import Policy = require("./policy");
24
- import Retrier = require("./retrier");
25
- import Event = require("./event");
46
+ export default _default;
47
+ export * from "./retrier-factory.js";
48
+ import Policy from "./policy.js";
49
+ import Retrier from "./retrier.js";
50
+ import Event from "./event.js";
51
+ import RetrierFactory from "./retrier-factory.js";
52
+ export { Policy, Retrier, Event, RetrierFactory };
@@ -1,12 +1,12 @@
1
- export = FactoreIncreasePolicy;
2
- declare class FactoreIncreasePolicy extends Policy {
1
+ export default class FactoreIncreasePolicy extends Policy {
3
2
  /**
4
3
  * each call to _next() increases the interval by lastInterval * factor
5
4
  * @param {number} factor - the increasement factor, >= 1
6
5
  */
7
6
  constructor(factor: number);
8
7
  _factor: number;
9
- set factor(factor: number);
8
+ set factor(arg: number);
10
9
  get factor(): number;
11
10
  }
12
- import Policy = require("../policy");
11
+ export { FactoreIncreasePolicy as FactoreIncreasePolicyType };
12
+ import Policy from "../policy.js";
@@ -1,12 +1,12 @@
1
- export = FixedIncreasePolicy;
2
- declare class FixedIncreasePolicy extends Policy {
1
+ export default class FixedIncreasePolicy extends Policy {
3
2
  /**
4
3
  * each call to _next() increases the interval by "increasement".
5
4
  * @param {number} increasement - The fixed interval (in milliseconds) between retry attempts.
6
5
  */
7
6
  constructor(increasement: number);
8
7
  _increasement: number;
9
- set increasement(increasement: number);
8
+ set increasement(arg: number);
10
9
  get increasement(): number;
11
10
  }
12
- import Policy = require("../policy");
11
+ export { FixedIncreasePolicy as FixedIncreasePolicyType };
12
+ import Policy from "../policy.js";
@@ -1,12 +1,12 @@
1
- export = FixedIntervalPolicy;
2
- declare class FixedIntervalPolicy extends Policy {
1
+ export default class FixedIntervalPolicy extends Policy {
3
2
  /**
4
3
  * Creates a fixed interval retry policy with the specified interval.
5
4
  * @param {number} interval - The fixed interval (in milliseconds) between retry attempts.
6
5
  */
7
6
  constructor(interval: number);
8
7
  _interval: number;
9
- set interval(interval: number);
8
+ set interval(arg: number);
10
9
  get interval(): number;
11
10
  }
12
- import Policy = require("../policy");
11
+ export { FixedIntervalPolicy as FixedIntervalPolicyType };
12
+ import Policy from "../policy.js";
@@ -1,5 +1,4 @@
1
- export = ShuttlePolicy;
2
- declare class ShuttlePolicy extends Policy {
1
+ export default class ShuttlePolicy extends Policy {
3
2
  /**
4
3
  * the inteval value shuttles between min and max
5
4
  * @param {number} stepLength - the step length to change
@@ -7,7 +6,8 @@ declare class ShuttlePolicy extends Policy {
7
6
  constructor(stepLength: number);
8
7
  _stepLength: number;
9
8
  increasement: number;
10
- set stepLength(stepLength: number);
9
+ set stepLength(arg: number);
11
10
  get stepLength(): number;
12
11
  }
13
- import Policy = require("../policy");
12
+ export { ShuttlePolicy as ShuttlePolicyType };
13
+ import Policy from "../policy.js";
package/types/policy.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- export = Policy;
2
- declare class Policy {
1
+ export default class Policy {
3
2
  _min: number;
4
3
  _max: number;
5
4
  _nextInterval: number;
@@ -35,7 +34,7 @@ declare class Policy {
35
34
  * @returns {this} The retrier instance for chaining.
36
35
  */
37
36
  max(max: number): this;
38
- reset(): this;
37
+ reset(): Policy;
39
38
  /**
40
39
  * Interval ms of next execution
41
40
  * @returns {number}
@@ -50,3 +49,4 @@ declare class Policy {
50
49
  */
51
50
  protected _next(): number;
52
51
  }
52
+ export { Policy as PolicyType };
@@ -1,3 +1,20 @@
1
+ declare namespace _default {
2
+ export { name };
3
+ export { infinite };
4
+ export { times };
5
+ export { maxRetries };
6
+ export { min };
7
+ export { max };
8
+ export { range };
9
+ export { fixedInterval };
10
+ export { fixedIncrease };
11
+ export { factorIncrease };
12
+ export { shuttleInterval };
13
+ export { timeout };
14
+ export { taskTimeout };
15
+ export { start };
16
+ }
17
+ export default _default;
1
18
  /**
2
19
  * Creates a new Retrier instance with the specified name.
3
20
  * @param {string} name - The name to assign to the retrier.
@@ -82,4 +99,4 @@ export function taskTimeout(timeout: number): Retrier;
82
99
  * @returns {Promise<*>} A promise that resolves when the retry process completes.
83
100
  */
84
101
  export function start(task: Function): Promise<any>;
85
- import Retrier = require("./retrier");
102
+ import Retrier from "./retrier.js";
@@ -1,13 +1,12 @@
1
- export = Retrier;
2
1
  /**
3
2
  * @extends EventEmitter
4
3
  */
5
- declare class Retrier {
4
+ export default class Retrier {
6
5
  /**
7
6
  * Creates a new Retrier instance with a fixed interval policy.
8
7
  * @param {number} [fixedInterval=1000] - The fixed interval in milliseconds between retry attempts. Defaults to 1000ms if not provided.
9
8
  */
10
- constructor(fixedInterval?: number);
9
+ constructor(fixedInterval?: number | undefined);
11
10
  /**
12
11
  * @type {Policy}
13
12
  */
@@ -153,7 +152,7 @@ declare class Retrier {
153
152
  * @param {boolean} [resetAfterSuccess=false] - Whether to reset retry counters after success.
154
153
  * @returns {this} The Retrier instance for chaining.
155
154
  */
156
- always(task: Function, resetAfterSuccess?: boolean): this;
155
+ always(task: Function, resetAfterSuccess?: boolean | undefined): this;
157
156
  /**
158
157
  * Starts the retry process.
159
158
  * @returns {Promise<*>}
@@ -164,7 +163,7 @@ declare class Retrier {
164
163
  * @param {Error} [reason] - Optional reason for stopping (defaults to 'Manually Stop' error).
165
164
  * @returns {Promise<void>} A promise that resolves when the retrier has fully stopped.
166
165
  */
167
- stop(reason?: Error): Promise<void>;
166
+ stop(reason?: Error | undefined): Promise<void>;
168
167
  /**
169
168
  * Resets the retry policy to its initial state.
170
169
  */
@@ -209,7 +208,7 @@ declare class Retrier {
209
208
  * Registers a listener for "timeout" events.
210
209
  * @param {Function} listener - The callback function
211
210
  */
212
- onTimeout(listener: Function): this;
211
+ onTimeout(listener: Function): Retrier;
213
212
  /**
214
213
  * Registers a listener for "task-timeout" events.
215
214
  * @param {Function} listener - The callback function
@@ -220,7 +219,7 @@ declare class Retrier {
220
219
  * Registers a listener for "completed" events.
221
220
  * @param {Function} listener - The callback function
222
221
  */
223
- onCompleted(listener: Function): this;
222
+ onCompleted(listener: Function): Retrier;
224
223
  /**
225
224
  * Registers a listener for the 'MaxRetries' event.
226
225
  * @param {Function} listener - The callback function to be executed when max retries are reached.
@@ -228,6 +227,7 @@ declare class Retrier {
228
227
  */
229
228
  onMaxRetries(listener: Function): this;
230
229
  }
231
- import Policy = require("./policy");
232
- import Task = require("./task");
233
- import AlwaysTask = require("./alway-task");
230
+ export { Retrier as RetrierType };
231
+ import Policy from "./policy.js";
232
+ import Task from "./task.js";
233
+ import AlwaysTask from "./alway-task.js";
package/types/task.d.ts CHANGED
@@ -1,12 +1,11 @@
1
- export = Task;
2
- declare class Task {
1
+ export default class Task {
3
2
  /**
4
3
  * Creates a new Task instance.
5
4
  * @param {Retrier} retrier - The retrier instance.
6
5
  * @param {Function} task - The function to be executed as the task.
7
6
  */
8
7
  constructor(retrier: Retrier, task: Function);
9
- retrier: Retrier;
8
+ retrier: import("./retrier.js").default;
10
9
  task: Function;
11
10
  result: any;
12
11
  error: unknown;
@@ -25,4 +24,5 @@ declare class Task {
25
24
  execute(retries: number, latence: number, nextInterval: number): Promise<void>;
26
25
  dispose(): void;
27
26
  }
28
- import Retrier = require("./retrier");
27
+ export { Task as TaskType };
28
+ export type Retrier = import('./retrier.js').default;
package/lib/alway-task.js DELETED
@@ -1,44 +0,0 @@
1
- 'use strict'
2
-
3
- // owned
4
- // eslint-disable-next-line no-unused-vars
5
- const Retrier = require('./retrier')
6
- const Task = require('./task')
7
-
8
- class AlwaysTask extends Task {
9
- /**
10
- * Checks if the given task is an instance of AlwaysTask.
11
- * @param {*} task - The task to check.
12
- * @returns {boolean} True if the task is an instance of AlwaysTask, false otherwise.
13
- */
14
- static isAlwaysTask (task) {
15
- return task instanceof AlwaysTask
16
- }
17
-
18
- /**
19
- * Creates an AlwaysTask instance.
20
- * @param {Retrier} retrier - The retrier instance to use for retry logic
21
- * @param {Function} task - The task function to execute
22
- * @param {boolean} resetRetryPolicyAfterSuccess - Whether to reset retry policy after successful execution
23
- */
24
- constructor (retrier, task, resetRetryPolicyAfterSuccess) {
25
- super(retrier, task)
26
- this.resetPolicy = resetRetryPolicyAfterSuccess
27
- }
28
-
29
- /**
30
- * Executes the task with the given retry parameters.
31
- * @param {number} retries - The number of retries attempted so far.
32
- * @param {number} latence - The current latency ms.
33
- * @param {number} nextInterval - The next interval ms.
34
- * @returns {Promise<*>} The result of the task execution.
35
- */
36
- async execute (retries, latence, nextInterval) {
37
- await super.execute(retries, latence, nextInterval)
38
- if (this.succeeded && this.resetPolicy) {
39
- this.retrier.resetRetryPolicy()
40
- }
41
- }
42
- }
43
-
44
- module.exports = AlwaysTask
package/lib/constants.js DELETED
@@ -1,11 +0,0 @@
1
- 'use strict'
2
- // module vars
3
- const DefaultMinInterval = 50
4
- const DefaultMaxInterval = 30 * 1000 // 30s
5
-
6
- const DefaultMaxRetries = 3
7
- module.exports = {
8
- DefaultMinInterval,
9
- DefaultMaxInterval,
10
- DefaultMaxRetries
11
- }
package/lib/event.js DELETED
@@ -1,22 +0,0 @@
1
- 'use strict'
2
- const Start = 'start' // retry started
3
- const Stop = 'stop' // retry stopped
4
- const Retry = 'retry' // one retry began
5
- const Success = 'success' // one task running succeeded
6
- const Failure = 'failure' // one task ran failed
7
- const Timeout = 'timeout' // total timeout
8
- const TaskTimeout = 'task-timeout' // one task timed out
9
- const Completed = 'complete' // all retries completed
10
- const MaxRetries = 'max-retries' // Reach the max retries
11
-
12
- module.exports = {
13
- Start,
14
- Retry,
15
- Success,
16
- Failure,
17
- Timeout,
18
- TaskTimeout,
19
- Stop,
20
- Completed,
21
- MaxRetries
22
- }
package/lib/index.js DELETED
@@ -1,28 +0,0 @@
1
- 'use strict'
2
- // 3rd
3
- const { LangUtils } = require('@creejs/commons-lang')
4
-
5
- // owned
6
- const Policy = require('./policy')
7
- const Retrier = require('./retrier')
8
- const Event = require('./event')
9
- const RetrierFactory = require('./retrier-factory')
10
-
11
- /**
12
- * Add all factory methods to Retrier as static methods.
13
- * Now we can create do something like this:
14
- * ```
15
- * Retrier.name('myRetrier')
16
- * Retrier.infinite()
17
- * ...
18
- * ```
19
- */
20
- LangUtils.defaults(Retrier, RetrierFactory)
21
-
22
- module.exports = {
23
- Policy,
24
- Retrier,
25
- Event,
26
- RetrierFactory,
27
- ...RetrierFactory
28
- }
@@ -1,46 +0,0 @@
1
- 'use strict'
2
- // 3rd
3
- // internal
4
- const { TypeAssert: { assertPositive } } = require('@creejs/commons-lang')
5
- // owned
6
- const Policy = require('../policy')
7
-
8
- class FactoreIncreasePolicy extends Policy {
9
- /**
10
- * each call to _next() increases the interval by lastInterval * factor
11
- * @param {number} factor - the increasement factor, >= 1
12
- */
13
- constructor (factor) {
14
- super()
15
- assertPositive(factor, 'factor')
16
- if (factor < 1) {
17
- throw new Error('factor must be >= 1')
18
- }
19
- this._factor = factor
20
- }
21
-
22
- set factor (factor) {
23
- assertPositive(factor, 'factor')
24
- if (factor < 1) {
25
- throw new Error('factor must be >= 1')
26
- }
27
- this._factor = factor
28
- }
29
-
30
- get factor () {
31
- return this._factor
32
- }
33
-
34
- /**
35
- * Interval ms of next execution
36
- * @returns {number}
37
- */
38
- _next () {
39
- if (this._nextInterval >= this._max) {
40
- return this._max
41
- }
42
- return this._nextInterval * this.factor
43
- }
44
- }
45
-
46
- module.exports = FactoreIncreasePolicy
@@ -1,40 +0,0 @@
1
- 'use strict'
2
- // 3rd
3
- // internal
4
- const { TypeAssert: { assertPositive } } = require('@creejs/commons-lang')
5
- // owned
6
- const Policy = require('../policy')
7
-
8
- 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
-
40
- module.exports = FixedIncreasePolicy
@@ -1,38 +0,0 @@
1
- 'use strict'
2
- // 3rd
3
- // internal
4
- const { TypeAssert: { assertPositive } } = require('@creejs/commons-lang')
5
- // owned
6
- const Policy = require('../policy')
7
-
8
- 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
- module.exports = FixedIntervalPolicy
@@ -1,49 +0,0 @@
1
- 'use strict'
2
- // 3rd
3
- // internal
4
- const { TypeAssert: { assertPositive } } = require('@creejs/commons-lang')
5
-
6
- // owned
7
- const Policy = require('../policy')
8
-
9
- class ShuttlePolicy extends Policy {
10
- /**
11
- * the inteval value shuttles between min and max
12
- * @param {number} stepLength - the step length to change
13
- */
14
- constructor (stepLength) {
15
- super()
16
- assertPositive(stepLength, 'stepLength')
17
- this._stepLength = stepLength
18
- this.increasement = stepLength
19
- }
20
-
21
- set stepLength (stepLength) {
22
- assertPositive(stepLength, 'stepLength')
23
- this._stepLength = stepLength
24
- this.increasement = stepLength
25
- }
26
-
27
- get stepLength () {
28
- return this._stepLength
29
- }
30
-
31
- /**
32
- * Interval ms of next execution
33
- * @returns {number}
34
- * @throws {Error} Always throws "Not Implemented Yet" error.
35
- */
36
- _next () {
37
- const nextInterval = this._nextInterval + this.increasement
38
- if (nextInterval >= this._max) {
39
- this.increasement = -this.stepLength
40
- return this._max
41
- } else if (nextInterval <= this._min) {
42
- this.increasement = this.stepLength
43
- return this._min
44
- }
45
- return nextInterval
46
- }
47
- }
48
-
49
- module.exports = ShuttlePolicy