@oscarpalmer/atoms 0.193.0 → 0.194.0
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/dist/array/difference.mjs +2 -2
- package/dist/array/intersection.mjs +2 -2
- package/dist/array/match.mjs +13 -13
- package/dist/array/slice.mjs +5 -5
- package/dist/array/sort.d.mts +5 -1
- package/dist/array/sort.mjs +4 -2
- package/dist/array/union.mjs +2 -2
- package/dist/beacon.d.mts +8 -1
- package/dist/beacon.mjs +52 -60
- package/dist/color/constants.d.mts +1 -1
- package/dist/color/constants.mjs +3 -5
- package/dist/color/instance.mjs +34 -41
- package/dist/color/misc/format.d.mts +3 -2
- package/dist/color/misc/format.mjs +4 -1
- package/dist/color/misc/is.d.mts +9 -1
- package/dist/color/misc/is.mjs +11 -1
- package/dist/color/misc/state.d.mts +6 -6
- package/dist/color/misc/state.mjs +11 -14
- package/dist/color/models.d.mts +23 -68
- package/dist/function/assert.mjs +2 -2
- package/dist/function/limit.d.mts +0 -6
- package/dist/function/limit.mjs +2 -2
- package/dist/function/memoize.mjs +19 -32
- package/dist/function/once.d.mts +0 -3
- package/dist/function/once.mjs +16 -13
- package/dist/function/retry.d.mts +1 -24
- package/dist/function/retry.mjs +1 -1
- package/dist/function/work.mjs +13 -11
- package/dist/herald.d.mts +30 -31
- package/dist/herald.mjs +74 -56
- package/dist/index.d.mts +100 -185
- package/dist/index.mjs +680 -702
- package/dist/internal/abort.d.mts +10 -0
- package/dist/internal/abort.mjs +14 -0
- package/dist/internal/array/chunk.mjs +2 -2
- package/dist/internal/array/find.mjs +2 -2
- package/dist/internal/array/sets.d.mts +4 -4
- package/dist/internal/array/sets.mjs +7 -7
- package/dist/internal/function/timer.mjs +19 -15
- package/dist/internal/number.mjs +7 -7
- package/dist/internal/result.mjs +3 -3
- package/dist/internal/sized.mjs +4 -4
- package/dist/internal/string.mjs +4 -4
- package/dist/internal/subscription.d.mts +24 -8
- package/dist/internal/subscription.mjs +49 -36
- package/dist/internal/value/equal.mjs +6 -6
- package/dist/internal/value/misc.mjs +14 -14
- package/dist/internal/value/set.mjs +2 -2
- package/dist/logger.mjs +15 -39
- package/dist/promise/constants.d.mts +17 -0
- package/dist/promise/constants.mjs +14 -0
- package/dist/promise/delay.mjs +7 -10
- package/dist/promise/helpers.mjs +1 -1
- package/dist/promise/index.mjs +14 -17
- package/dist/promise/misc.d.mts +2 -1
- package/dist/promise/misc.mjs +8 -7
- package/dist/promise/models.d.mts +4 -16
- package/dist/promise/models.mjs +4 -13
- package/dist/promise/timed.mjs +9 -10
- package/dist/query.mjs +15 -15
- package/dist/queue.mjs +103 -116
- package/dist/random.mjs +7 -7
- package/dist/result/index.d.mts +0 -3
- package/dist/result/index.mjs +1 -1
- package/dist/result/match.d.mts +0 -3
- package/dist/result/match.mjs +4 -4
- package/dist/result/misc.mjs +2 -2
- package/dist/result/work/flow.d.mts +0 -3
- package/dist/result/work/flow.mjs +5 -5
- package/dist/result/work/pipe.d.mts +0 -3
- package/dist/result/work/pipe.mjs +3 -3
- package/dist/string/case.mjs +20 -20
- package/dist/string/fuzzy.mjs +23 -18
- package/dist/string/index.mjs +7 -7
- package/dist/string/normalize.mjs +14 -14
- package/dist/string/template.mjs +2 -2
- package/dist/value/clone.mjs +10 -10
- package/dist/value/smush.mjs +2 -2
- package/package.json +13 -5
- package/src/array/difference.ts +0 -73
- package/src/array/exists.ts +0 -93
- package/src/array/filter.ts +0 -182
- package/src/array/find.ts +0 -183
- package/src/array/first.ts +0 -196
- package/src/array/flatten.ts +0 -20
- package/src/array/from.ts +0 -129
- package/src/array/get.ts +0 -131
- package/src/array/group-by.ts +0 -369
- package/src/array/index.ts +0 -26
- package/src/array/insert.ts +0 -52
- package/src/array/intersection.ts +0 -72
- package/src/array/last.ts +0 -194
- package/src/array/match.ts +0 -436
- package/src/array/move.ts +0 -315
- package/src/array/partition.ts +0 -91
- package/src/array/push.ts +0 -23
- package/src/array/reverse.ts +0 -33
- package/src/array/select.ts +0 -260
- package/src/array/single.ts +0 -93
- package/src/array/slice.ts +0 -335
- package/src/array/sort.ts +0 -607
- package/src/array/splice.ts +0 -106
- package/src/array/swap.ts +0 -322
- package/src/array/to-map.ts +0 -371
- package/src/array/to-record.ts +0 -351
- package/src/array/to-set.ts +0 -80
- package/src/array/toggle.ts +0 -79
- package/src/array/union.ts +0 -72
- package/src/array/unique.ts +0 -67
- package/src/array/update.ts +0 -75
- package/src/beacon.ts +0 -378
- package/src/color/constants.ts +0 -137
- package/src/color/index.ts +0 -83
- package/src/color/instance.ts +0 -162
- package/src/color/misc/alpha.ts +0 -56
- package/src/color/misc/format.ts +0 -29
- package/src/color/misc/get.ts +0 -210
- package/src/color/misc/is.ts +0 -221
- package/src/color/misc/state.ts +0 -243
- package/src/color/models.ts +0 -389
- package/src/color/space/hex.ts +0 -184
- package/src/color/space/hsl.ts +0 -174
- package/src/color/space/hwb.ts +0 -153
- package/src/color/space/rgb.ts +0 -240
- package/src/function/assert.ts +0 -174
- package/src/function/index.ts +0 -2
- package/src/function/limit.ts +0 -83
- package/src/function/memoize.ts +0 -202
- package/src/function/once.ts +0 -200
- package/src/function/retry.ts +0 -175
- package/src/function/work.ts +0 -1007
- package/src/herald.ts +0 -216
- package/src/index.ts +0 -68
- package/src/internal/array/callbacks.ts +0 -46
- package/src/internal/array/chunk.ts +0 -51
- package/src/internal/array/compact.ts +0 -56
- package/src/internal/array/find.ts +0 -200
- package/src/internal/array/group.ts +0 -42
- package/src/internal/array/index-of.ts +0 -182
- package/src/internal/array/insert.ts +0 -80
- package/src/internal/array/overlap.ts +0 -35
- package/src/internal/array/sets.ts +0 -74
- package/src/internal/array/shuffle.ts +0 -33
- package/src/internal/array/update.ts +0 -52
- package/src/internal/function/misc.ts +0 -8
- package/src/internal/function/timer.ts +0 -168
- package/src/internal/is.ts +0 -244
- package/src/internal/math/aggregate.ts +0 -164
- package/src/internal/math/misc.ts +0 -28
- package/src/internal/number.ts +0 -129
- package/src/internal/random.ts +0 -49
- package/src/internal/result.ts +0 -92
- package/src/internal/sized.ts +0 -25
- package/src/internal/string.ts +0 -128
- package/src/internal/subscription.ts +0 -264
- package/src/internal/value/compare.ts +0 -170
- package/src/internal/value/equal.ts +0 -465
- package/src/internal/value/get.ts +0 -57
- package/src/internal/value/handlers.ts +0 -95
- package/src/internal/value/has.ts +0 -118
- package/src/internal/value/misc.ts +0 -125
- package/src/internal/value/partial.ts +0 -46
- package/src/internal/value/set.ts +0 -132
- package/src/is.ts +0 -182
- package/src/logger.ts +0 -230
- package/src/math.ts +0 -281
- package/src/models.ts +0 -286
- package/src/number.ts +0 -1
- package/src/promise/delay.ts +0 -62
- package/src/promise/helpers.ts +0 -96
- package/src/promise/index.ts +0 -300
- package/src/promise/misc.ts +0 -96
- package/src/promise/models.ts +0 -195
- package/src/promise/timed.ts +0 -91
- package/src/query.ts +0 -147
- package/src/queue.ts +0 -814
- package/src/random.ts +0 -125
- package/src/result/index.ts +0 -105
- package/src/result/match.ts +0 -124
- package/src/result/misc.ts +0 -129
- package/src/result/models.ts +0 -83
- package/src/result/work/flow.ts +0 -488
- package/src/result/work/pipe.ts +0 -496
- package/src/sized/map.ts +0 -110
- package/src/sized/set.ts +0 -107
- package/src/string/case.ts +0 -236
- package/src/string/fuzzy.ts +0 -444
- package/src/string/index.ts +0 -199
- package/src/string/match.ts +0 -80
- package/src/string/normalize.ts +0 -205
- package/src/string/template.ts +0 -161
- package/src/value/clone.ts +0 -319
- package/src/value/collection.ts +0 -63
- package/src/value/diff.ts +0 -232
- package/src/value/freeze.ts +0 -322
- package/src/value/handle.ts +0 -3
- package/src/value/index.ts +0 -5
- package/src/value/merge.ts +0 -278
- package/src/value/omit.ts +0 -20
- package/src/value/pick.ts +0 -20
- package/src/value/shake.ts +0 -45
- package/src/value/smush.ts +0 -89
- package/src/value/transform.ts +0 -153
- package/src/value/unsmush.ts +0 -84
package/src/function/once.ts
DELETED
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
GenericAsyncCallback,
|
|
3
|
-
GenericCallback,
|
|
4
|
-
OnceAsyncCallback,
|
|
5
|
-
OnceCallback,
|
|
6
|
-
} from '../models';
|
|
7
|
-
import {assert} from './assert';
|
|
8
|
-
|
|
9
|
-
// #region Types
|
|
10
|
-
|
|
11
|
-
type OnceAsyncItem<Value> = {
|
|
12
|
-
reject: (reason?: unknown) => void;
|
|
13
|
-
resolve: (value: Value) => void;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
type OnceAsyncState<Value> = {
|
|
17
|
-
error: boolean;
|
|
18
|
-
finished: boolean;
|
|
19
|
-
items: OnceAsyncItem<Value>[];
|
|
20
|
-
} & OnceState<Value>;
|
|
21
|
-
|
|
22
|
-
type OnceState<Value> = {
|
|
23
|
-
called: boolean;
|
|
24
|
-
cleared: boolean;
|
|
25
|
-
value: Value;
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
// #endregion
|
|
29
|
-
|
|
30
|
-
// #region Functions
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Create an asynchronous function that can only be called once, rejecting or resolving the same result on subsequent calls
|
|
34
|
-
*
|
|
35
|
-
* _Available as `asyncOnce` and `once.async`_
|
|
36
|
-
*
|
|
37
|
-
* @param callback Callback to use once
|
|
38
|
-
* @returns _Once_ callback
|
|
39
|
-
*/
|
|
40
|
-
export function asyncOnce<Callback extends GenericAsyncCallback>(
|
|
41
|
-
callback: Callback,
|
|
42
|
-
): OnceAsyncCallback<Callback> {
|
|
43
|
-
assert(() => typeof callback === 'function', ONCE_MESSAGE_EXPECTATION);
|
|
44
|
-
|
|
45
|
-
const state: OnceAsyncState<Awaited<ReturnType<Callback>>> = {
|
|
46
|
-
called: false,
|
|
47
|
-
cleared: false,
|
|
48
|
-
error: false,
|
|
49
|
-
finished: false,
|
|
50
|
-
items: [],
|
|
51
|
-
value: undefined as never,
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const fn = (...parameters: Parameters<Callback>): Promise<Awaited<ReturnType<Callback>>> => {
|
|
55
|
-
if (state.cleared) {
|
|
56
|
-
return Promise.reject(new Error(ONCE_MESSAGE_CLEARED));
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (state.finished) {
|
|
60
|
-
return state.error ? Promise.reject(state.value) : Promise.resolve(state.value);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (state.called) {
|
|
64
|
-
return new Promise<Awaited<ReturnType<Callback>>>((resolve, reject) => {
|
|
65
|
-
state.items.push({reject, resolve});
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
state.called = true;
|
|
70
|
-
|
|
71
|
-
return new Promise<Awaited<ReturnType<Callback>>>((resolve, reject) => {
|
|
72
|
-
state.items.push({reject, resolve});
|
|
73
|
-
|
|
74
|
-
void callback(...parameters)
|
|
75
|
-
.then(value => {
|
|
76
|
-
handleOnceResult(state, value, false);
|
|
77
|
-
})
|
|
78
|
-
.catch(error => {
|
|
79
|
-
handleOnceResult(state, error, true);
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
Object.defineProperties(fn, {
|
|
85
|
-
called: {
|
|
86
|
-
enumerable: true,
|
|
87
|
-
get: (): boolean => state.called,
|
|
88
|
-
},
|
|
89
|
-
cleared: {
|
|
90
|
-
enumerable: true,
|
|
91
|
-
get: (): boolean => state.cleared,
|
|
92
|
-
},
|
|
93
|
-
error: {
|
|
94
|
-
enumerable: true,
|
|
95
|
-
get: (): boolean => state.error,
|
|
96
|
-
},
|
|
97
|
-
finished: {
|
|
98
|
-
enumerable: true,
|
|
99
|
-
get: (): boolean => state.finished,
|
|
100
|
-
},
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
fn.clear = (): void => {
|
|
104
|
-
if (!state.called || !state.finished || state.cleared) {
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
state.cleared = true;
|
|
109
|
-
state.value = undefined as never;
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
return fn as OnceAsyncCallback<Callback>;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function handleOnceResult<Value>(
|
|
116
|
-
state: OnceAsyncState<Value>,
|
|
117
|
-
value: unknown,
|
|
118
|
-
error: boolean,
|
|
119
|
-
): void {
|
|
120
|
-
state.error = error;
|
|
121
|
-
state.finished = true;
|
|
122
|
-
state.value = value as Value;
|
|
123
|
-
|
|
124
|
-
const items = state.items.splice(0);
|
|
125
|
-
const {length} = items;
|
|
126
|
-
|
|
127
|
-
for (let index = 0; index < length; index += 1) {
|
|
128
|
-
const {reject, resolve} = items[index];
|
|
129
|
-
|
|
130
|
-
if (error) {
|
|
131
|
-
reject(value);
|
|
132
|
-
} else {
|
|
133
|
-
resolve(value as Value);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Create a function that can only be called once, returning the same value on subsequent calls
|
|
140
|
-
*
|
|
141
|
-
* @param callback Callback to use once
|
|
142
|
-
* @returns _Once_ callback
|
|
143
|
-
*/
|
|
144
|
-
export function once<Callback extends GenericCallback>(callback: Callback): OnceCallback<Callback> {
|
|
145
|
-
assert(() => typeof callback === 'function', ONCE_MESSAGE_EXPECTATION);
|
|
146
|
-
|
|
147
|
-
const state: OnceState<ReturnType<Callback>> = {
|
|
148
|
-
called: false,
|
|
149
|
-
cleared: false,
|
|
150
|
-
value: undefined as never,
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
const fn = (...parameters: Parameters<Callback>): ReturnType<Callback> => {
|
|
154
|
-
if (state.cleared) {
|
|
155
|
-
throw new Error(ONCE_MESSAGE_CLEARED);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if (state.called) {
|
|
159
|
-
return state.value;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
state.called = true;
|
|
163
|
-
|
|
164
|
-
state.value = callback(...parameters);
|
|
165
|
-
|
|
166
|
-
return state.value;
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
Object.defineProperties(fn, {
|
|
170
|
-
called: {
|
|
171
|
-
get: (): boolean => state.called,
|
|
172
|
-
},
|
|
173
|
-
cleared: {
|
|
174
|
-
get: (): boolean => state.cleared,
|
|
175
|
-
},
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
fn.clear = (): void => {
|
|
179
|
-
if (!state.called || state.cleared) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
state.cleared = true;
|
|
184
|
-
state.value = undefined as never;
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
return fn as OnceCallback<Callback>;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
once.async = asyncOnce;
|
|
191
|
-
|
|
192
|
-
// #endregion
|
|
193
|
-
|
|
194
|
-
// #region Variables
|
|
195
|
-
|
|
196
|
-
const ONCE_MESSAGE_CLEARED = 'Once has been cleared';
|
|
197
|
-
|
|
198
|
-
const ONCE_MESSAGE_EXPECTATION = 'Once expected a function';
|
|
199
|
-
|
|
200
|
-
// #endregion
|
package/src/function/retry.ts
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import {getTimer, TIMER_WAIT} from '../internal/function/timer';
|
|
2
|
-
import {isPlainObject} from '../internal/is';
|
|
3
|
-
import type {GenericAsyncCallback, GenericCallback} from '../models';
|
|
4
|
-
|
|
5
|
-
// #region Types
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* An error thrown when a retry fails
|
|
9
|
-
*/
|
|
10
|
-
export class RetryError extends Error {
|
|
11
|
-
constructor(
|
|
12
|
-
message: string,
|
|
13
|
-
readonly original: unknown,
|
|
14
|
-
) {
|
|
15
|
-
super(message);
|
|
16
|
-
|
|
17
|
-
this.name = RETRY_ERROR_NAME;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type RetryOptions = {
|
|
22
|
-
delay?: number;
|
|
23
|
-
times?: number;
|
|
24
|
-
when?: (error: unknown) => boolean;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
// #endregion
|
|
28
|
-
|
|
29
|
-
// #region Functions
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Retry a callback a specified number of times, with a delay between attempts
|
|
33
|
-
*
|
|
34
|
-
* _Available as `asyncRetry` and `retry.async`_
|
|
35
|
-
*
|
|
36
|
-
* @param callback Callback to retry
|
|
37
|
-
* @param options Retry options
|
|
38
|
-
* @returns Callback result
|
|
39
|
-
*/
|
|
40
|
-
async function asyncRetry<Callback extends GenericAsyncCallback>(
|
|
41
|
-
callback: Callback,
|
|
42
|
-
options?: RetryOptions,
|
|
43
|
-
): Promise<Awaited<ReturnType<Callback>>>;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Retry a callback a specified number of times, with a delay between attempts
|
|
47
|
-
*
|
|
48
|
-
* _Available as `asyncRetry` and `retry.async`_
|
|
49
|
-
*
|
|
50
|
-
* @param callback Callback to retry
|
|
51
|
-
* @param options Retry options
|
|
52
|
-
* @returns Callback result
|
|
53
|
-
*/
|
|
54
|
-
async function asyncRetry<Callback extends GenericCallback>(
|
|
55
|
-
callback: Callback,
|
|
56
|
-
options?: RetryOptions,
|
|
57
|
-
): Promise<ReturnType<Callback>>;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Retry a callback a specified number of times, with a delay between attempts
|
|
61
|
-
*
|
|
62
|
-
* _Available as `asyncRetry` and `retry.async`_
|
|
63
|
-
*
|
|
64
|
-
* @param callback Callback to retry
|
|
65
|
-
* @param options Retry options
|
|
66
|
-
* @returns Callback result
|
|
67
|
-
*/
|
|
68
|
-
async function asyncRetry<Callback extends GenericCallback>(
|
|
69
|
-
callback: Callback,
|
|
70
|
-
options?: RetryOptions,
|
|
71
|
-
): Promise<ReturnType<Callback>> {
|
|
72
|
-
if (typeof callback !== 'function') {
|
|
73
|
-
throw new TypeError(RETRY_MESSAGE_EXPECTATION);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async function handle(): Promise<void> {
|
|
77
|
-
try {
|
|
78
|
-
const result = await callback();
|
|
79
|
-
|
|
80
|
-
resolver(result);
|
|
81
|
-
} catch (error) {
|
|
82
|
-
if (attempts >= times || !when(error)) {
|
|
83
|
-
rejector(new RetryError(RETRY_MESSAGE_FAILED, error));
|
|
84
|
-
} else {
|
|
85
|
-
attempts += 1;
|
|
86
|
-
|
|
87
|
-
void timer();
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const {delay, times, when} = getRetryOptions(options);
|
|
93
|
-
|
|
94
|
-
const timer = getTimer(TIMER_WAIT, handle, delay);
|
|
95
|
-
|
|
96
|
-
let attempts = 0;
|
|
97
|
-
|
|
98
|
-
let rejector: (reason?: unknown) => void;
|
|
99
|
-
let resolver: (value: Awaited<ReturnType<Callback>>) => void;
|
|
100
|
-
|
|
101
|
-
return new Promise<Awaited<ReturnType<Callback>>>((resolve, reject) => {
|
|
102
|
-
rejector = reject;
|
|
103
|
-
resolver = resolve;
|
|
104
|
-
|
|
105
|
-
void handle();
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function getRetryNumber(value?: unknown): number {
|
|
110
|
-
return typeof value === 'number' && value > 0 ? value : 0;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function getRetryOptions(input?: RetryOptions): Required<RetryOptions> {
|
|
114
|
-
const options = isPlainObject(input) ? input : {};
|
|
115
|
-
|
|
116
|
-
return {
|
|
117
|
-
delay: getRetryNumber(options.delay),
|
|
118
|
-
times: getRetryNumber(options.times),
|
|
119
|
-
when: typeof options.when === 'function' ? options.when : shouldRetry,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Retry a callback a specified number of times
|
|
125
|
-
*
|
|
126
|
-
* @param callback Callback to retry
|
|
127
|
-
* @param options Retry options
|
|
128
|
-
* @returns Callback result
|
|
129
|
-
*/
|
|
130
|
-
export function retry<Callback extends GenericCallback>(
|
|
131
|
-
callback: Callback,
|
|
132
|
-
options?: Omit<RetryOptions, 'delay'>,
|
|
133
|
-
): ReturnType<Callback> {
|
|
134
|
-
if (typeof callback !== 'function') {
|
|
135
|
-
throw new TypeError(RETRY_MESSAGE_EXPECTATION);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const {times, when} = getRetryOptions(options);
|
|
139
|
-
|
|
140
|
-
let last: unknown;
|
|
141
|
-
|
|
142
|
-
for (let index = 0; index <= times; index += 1) {
|
|
143
|
-
try {
|
|
144
|
-
const result = callback();
|
|
145
|
-
|
|
146
|
-
return result;
|
|
147
|
-
} catch (error) {
|
|
148
|
-
if (index >= times || !when(error)) {
|
|
149
|
-
last = error;
|
|
150
|
-
|
|
151
|
-
break;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
throw new RetryError(RETRY_MESSAGE_FAILED, last);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
retry.async = asyncRetry;
|
|
160
|
-
|
|
161
|
-
function shouldRetry(): boolean {
|
|
162
|
-
return true;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// #endregion
|
|
166
|
-
|
|
167
|
-
// #region Variables
|
|
168
|
-
|
|
169
|
-
const RETRY_ERROR_NAME = 'RetryError';
|
|
170
|
-
|
|
171
|
-
const RETRY_MESSAGE_EXPECTATION = 'Retry expected a function';
|
|
172
|
-
|
|
173
|
-
const RETRY_MESSAGE_FAILED = 'Retry failed';
|
|
174
|
-
|
|
175
|
-
// #endregion
|