@oscarpalmer/atoms 0.202.0 → 0.204.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.d.mts +4 -5
- package/dist/array/exists.d.mts +5 -6
- package/dist/array/filter.d.mts +10 -11
- package/dist/array/filter.mjs +0 -1
- package/dist/array/find.d.mts +10 -11
- package/dist/array/find.mjs +0 -1
- package/dist/array/first.d.mts +10 -11
- package/dist/array/first.mjs +0 -1
- package/dist/array/flatten.d.mts +2 -3
- package/dist/array/from.d.mts +7 -8
- package/dist/array/group-by.d.mts +7 -8
- package/dist/array/group-by.mjs +0 -1
- package/dist/array/insert.d.mts +3 -4
- package/dist/array/intersection.d.mts +4 -5
- package/dist/array/last.d.mts +10 -11
- package/dist/array/last.mjs +0 -1
- package/dist/array/move.d.mts +9 -10
- package/dist/array/move.mjs +0 -4
- package/dist/array/partition.d.mts +5 -6
- package/dist/array/push.d.mts +2 -3
- package/dist/array/reverse.d.mts +2 -3
- package/dist/array/select.d.mts +11 -12
- package/dist/array/select.mjs +0 -1
- package/dist/array/single.d.mts +4 -5
- package/dist/array/slice.d.mts +12 -13
- package/dist/array/sort.d.mts +31 -32
- package/dist/array/sort.mjs +38 -39
- package/dist/array/splice.d.mts +5 -6
- package/dist/array/swap.d.mts +8 -9
- package/dist/array/swap.mjs +0 -1
- package/dist/array/to-map.d.mts +7 -8
- package/dist/array/to-map.mjs +0 -1
- package/dist/array/to-record.d.mts +7 -8
- package/dist/array/to-record.mjs +0 -1
- package/dist/array/to-set.d.mts +4 -5
- package/dist/array/toggle.d.mts +4 -5
- package/dist/array/union.d.mts +4 -5
- package/dist/array/unique.d.mts +4 -5
- package/dist/array/update.d.mts +4 -5
- package/dist/beacon.d.mts +8 -9
- package/dist/beacon.mjs +96 -71
- package/dist/color/constants.d.mts +14 -13
- package/dist/color/constants.mjs +9 -2
- package/dist/color/index.d.mts +2 -2
- package/dist/color/instance.d.mts +3 -3
- package/dist/color/instance.mjs +91 -69
- package/dist/color/misc/alpha.d.mts +7 -6
- package/dist/color/misc/alpha.mjs +8 -2
- package/dist/color/misc/format.d.mts +6 -7
- package/dist/color/misc/format.mjs +15 -12
- package/dist/color/misc/get.d.mts +14 -15
- package/dist/color/misc/is.d.mts +16 -17
- package/dist/color/misc/is.mjs +7 -7
- package/dist/color/misc/state.d.mts +33 -10
- package/dist/color/misc/state.mjs +28 -48
- package/dist/color/models.d.mts +25 -19
- package/dist/color/space/hex.d.mts +14 -11
- package/dist/color/space/hex.mjs +21 -2
- package/dist/color/space/hsl.d.mts +13 -10
- package/dist/color/space/hsl.mjs +25 -4
- package/dist/color/space/hwb.d.mts +14 -11
- package/dist/color/space/hwb.mjs +24 -3
- package/dist/color/space/rgb.d.mts +16 -13
- package/dist/color/space/rgb.mjs +41 -20
- package/dist/function/limit.d.mts +9 -8
- package/dist/function/limit.mjs +5 -7
- package/dist/function/once.d.mts +26 -6
- package/dist/function/once.mjs +129 -76
- package/dist/function/retry.d.mts +5 -6
- package/dist/function/retry.mjs +3 -4
- package/dist/index.d.mts +184 -60
- package/dist/index.mjs +1292 -970
- package/dist/internal/aborter.d.mts +9 -0
- package/dist/internal/{abort.mjs → aborter.mjs} +1 -1
- package/dist/internal/array/callbacks.d.mts +4 -5
- package/dist/internal/array/chunk.d.mts +2 -3
- package/dist/internal/array/compact.d.mts +3 -4
- package/dist/internal/array/find.d.mts +10 -11
- package/dist/internal/array/get.d.mts +7 -8
- package/dist/internal/array/get.mjs +3 -2
- package/dist/internal/array/group.d.mts +2 -3
- package/dist/internal/array/index-of.d.mts +10 -11
- package/dist/internal/array/index-of.mjs +0 -1
- package/dist/internal/array/insert.d.mts +5 -6
- package/dist/internal/array/match.d.mts +17 -18
- package/dist/internal/array/overlap.d.mts +2 -3
- package/dist/internal/array/sets.d.mts +5 -6
- package/dist/internal/array/shuffle.d.mts +2 -3
- package/dist/internal/array/update.d.mts +2 -3
- package/dist/internal/defaults.d.mts +3 -4
- package/dist/internal/function/assert.d.mts +10 -11
- package/dist/internal/function/assert.mjs +0 -7
- package/dist/internal/function/limit.d.mts +41 -0
- package/dist/internal/function/limit.mjs +106 -0
- package/dist/internal/function/memoize.d.mts +4 -5
- package/dist/internal/function/memoize.mjs +55 -31
- package/dist/internal/function/misc.d.mts +2 -3
- package/dist/internal/function/work.d.mts +53 -54
- package/dist/internal/function/work.mjs +0 -2
- package/dist/internal/herald.d.mts +8 -9
- package/dist/internal/herald.mjs +38 -27
- package/dist/internal/is.d.mts +19 -20
- package/dist/internal/math/aggregate.d.mts +14 -15
- package/dist/internal/math/misc.d.mts +3 -4
- package/dist/internal/number.d.mts +4 -5
- package/dist/internal/random.d.mts +3 -4
- package/dist/internal/result/misc.d.mts +29 -14
- package/dist/internal/result/misc.mjs +4 -3
- package/dist/internal/result/models.d.mts +9 -10
- package/dist/internal/sized/map.d.mts +2 -3
- package/dist/internal/sized/misc.d.mts +2 -3
- package/dist/internal/string/case.d.mts +9 -10
- package/dist/internal/string/misc.d.mts +8 -9
- package/dist/internal/subscription.d.mts +10 -11
- package/dist/internal/subscription.mjs +72 -58
- package/dist/internal/value/compare.d.mts +17 -9
- package/dist/internal/value/compare.mjs +6 -11
- package/dist/internal/value/equal.d.mts +34 -15
- package/dist/internal/value/equal.mjs +20 -13
- package/dist/internal/value/get.d.mts +3 -4
- package/dist/internal/value/handlers.d.mts +19 -9
- package/dist/internal/value/handlers.mjs +48 -50
- package/dist/internal/value/has.d.mts +6 -7
- package/dist/internal/value/has.mjs +0 -1
- package/dist/internal/value/misc.d.mts +6 -7
- package/dist/internal/value/misc.mjs +4 -3
- package/dist/internal/value/partial.d.mts +3 -4
- package/dist/internal/value/partial.mjs +2 -1
- package/dist/internal/value/set.d.mts +4 -5
- package/dist/internal/value/set.mjs +2 -1
- package/dist/is.d.mts +13 -13
- package/dist/logger.d.mts +5 -4
- package/dist/logger.mjs +72 -64
- package/dist/math.d.mts +20 -20
- package/dist/models.d.mts +45 -49
- package/dist/promise/constants.d.mts +14 -15
- package/dist/promise/delay.d.mts +3 -4
- package/dist/promise/delay.mjs +5 -5
- package/dist/promise/helpers.d.mts +7 -8
- package/dist/promise/helpers.mjs +4 -3
- package/dist/promise/index.d.mts +13 -14
- package/dist/promise/index.mjs +1 -2
- package/dist/promise/misc.d.mts +7 -7
- package/dist/promise/models.d.mts +17 -18
- package/dist/promise/timed.d.mts +4 -5
- package/dist/promise/timed.mjs +6 -6
- package/dist/query.d.mts +3 -4
- package/dist/queue.d.mts +14 -15
- package/dist/queue.mjs +198 -136
- package/dist/random.d.mts +8 -8
- package/dist/result/index.d.mts +8 -9
- package/dist/result/index.mjs +0 -1
- package/dist/result/match.d.mts +6 -7
- package/dist/result/match.mjs +0 -1
- package/dist/result/misc.d.mts +3 -3
- package/dist/result/work/flow.d.mts +28 -29
- package/dist/result/work/flow.mjs +0 -1
- package/dist/result/work/pipe.d.mts +24 -25
- package/dist/result/work/pipe.mjs +0 -1
- package/dist/sized/set.d.mts +2 -3
- package/dist/string/fuzzy.d.mts +21 -12
- package/dist/string/fuzzy.mjs +77 -52
- package/dist/string/index.d.mts +8 -8
- package/dist/string/match.d.mts +4 -5
- package/dist/string/normalize.d.mts +8 -9
- package/dist/string/normalize.mjs +9 -5
- package/dist/string/template.d.mts +9 -9
- package/dist/string/template.mjs +11 -5
- package/dist/value/clone.d.mts +21 -13
- package/dist/value/clone.mjs +19 -25
- package/dist/value/collection.d.mts +4 -5
- package/dist/value/diff.d.mts +5 -6
- package/dist/value/freeze.d.mts +18 -19
- package/dist/value/freeze.mjs +1 -5
- package/dist/value/merge.d.mts +13 -14
- package/dist/value/merge.mjs +21 -7
- package/dist/value/omit.d.mts +2 -3
- package/dist/value/pick.d.mts +2 -3
- package/dist/value/shake.d.mts +3 -4
- package/dist/value/smush.d.mts +3 -4
- package/dist/value/transform.d.mts +15 -10
- package/dist/value/transform.mjs +12 -8
- package/dist/value/unsmush.d.mts +3 -4
- package/package.json +2 -2
- package/dist/internal/abort.d.mts +0 -10
- package/dist/internal/function/timer.d.mts +0 -10
- package/dist/internal/function/timer.mjs +0 -87
package/dist/color/space/rgb.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import { COLOR_DEFAULTS, COLOR_MAX, COLOR_TYPE } from "../constants.mjs";
|
|
|
3
3
|
import { getAlpha, getAlphaValue } from "../misc/alpha.mjs";
|
|
4
4
|
import { getHexValue } from "../misc/get.mjs";
|
|
5
5
|
import { isRgbLike } from "../misc/is.mjs";
|
|
6
|
+
import { getStateValue, setStateValue } from "../misc/state.mjs";
|
|
6
7
|
//#region src/color/space/rgb.ts
|
|
7
8
|
function convertRgbToHex(rgb, alpha) {
|
|
8
9
|
const hex = `${join([
|
|
@@ -18,7 +19,7 @@ function convertRgbToHex(rgb, alpha) {
|
|
|
18
19
|
return `${hex}${a}`;
|
|
19
20
|
}
|
|
20
21
|
function convertRgbToHsla(value) {
|
|
21
|
-
const values =
|
|
22
|
+
const values = getRgbCalculationValues(isRgbLike(value) ? getRgbValues(value) : { ...COLOR_DEFAULTS.rgb });
|
|
22
23
|
const { delta, max, min } = values;
|
|
23
24
|
const lightness = (min + max) / 2;
|
|
24
25
|
let hue = 0;
|
|
@@ -36,7 +37,7 @@ function convertRgbToHsla(value) {
|
|
|
36
37
|
};
|
|
37
38
|
}
|
|
38
39
|
function convertRgbToHwba(value) {
|
|
39
|
-
const values =
|
|
40
|
+
const values = getRgbCalculationValues(isRgbLike(value) ? getRgbValues(value) : { ...COLOR_DEFAULTS.rgb });
|
|
40
41
|
const { delta, max, min } = values;
|
|
41
42
|
let hue = 0;
|
|
42
43
|
if (delta !== 0) {
|
|
@@ -62,22 +63,7 @@ function getColorFromRgb(state, type) {
|
|
|
62
63
|
}
|
|
63
64
|
return state[type];
|
|
64
65
|
}
|
|
65
|
-
function
|
|
66
|
-
const { blue, delta, green, max, red } = values;
|
|
67
|
-
switch (max) {
|
|
68
|
-
case blue: return (red - green) / delta + 4;
|
|
69
|
-
case green: return (blue - red) / delta + 2;
|
|
70
|
-
default: return (green - blue) / delta + (green < blue ? 6 : 0);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
function getRgbValue(value) {
|
|
74
|
-
return {
|
|
75
|
-
blue: getHexValue(value.blue),
|
|
76
|
-
green: getHexValue(value.green),
|
|
77
|
-
red: getHexValue(value.red)
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
function getRgbValues(rgb) {
|
|
66
|
+
function getRgbCalculationValues(rgb) {
|
|
81
67
|
const blue = rgb.blue / COLOR_MAX.hex;
|
|
82
68
|
const green = rgb.green / COLOR_MAX.hex;
|
|
83
69
|
const red = rgb.red / COLOR_MAX.hex;
|
|
@@ -92,6 +78,27 @@ function getRgbValues(rgb) {
|
|
|
92
78
|
red
|
|
93
79
|
};
|
|
94
80
|
}
|
|
81
|
+
function getRgbColor() {
|
|
82
|
+
return getStateValue(this, COLOR_TYPE.rgb, false);
|
|
83
|
+
}
|
|
84
|
+
function getRgbaColor() {
|
|
85
|
+
return getStateValue(this, COLOR_TYPE.rgb, true);
|
|
86
|
+
}
|
|
87
|
+
function getRgbHue(values) {
|
|
88
|
+
const { blue, delta, green, max, red } = values;
|
|
89
|
+
switch (max) {
|
|
90
|
+
case blue: return (red - green) / delta + 4;
|
|
91
|
+
case green: return (blue - red) / delta + 2;
|
|
92
|
+
default: return (green - blue) / delta + (green < blue ? 6 : 0);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function getRgbValues(value) {
|
|
96
|
+
return {
|
|
97
|
+
blue: getHexValue(value.blue),
|
|
98
|
+
green: getHexValue(value.green),
|
|
99
|
+
red: getHexValue(value.red)
|
|
100
|
+
};
|
|
101
|
+
}
|
|
95
102
|
/**
|
|
96
103
|
* Convert an _RGB(A)_ color to a hex color _(with optional alpha channel, i.e., opacity)_
|
|
97
104
|
*
|
|
@@ -102,7 +109,7 @@ function getRgbValues(rgb) {
|
|
|
102
109
|
* @returns Hex color string
|
|
103
110
|
*/
|
|
104
111
|
function rgbToHex(rgb, alpha) {
|
|
105
|
-
return convertRgbToHex(isRgbLike(rgb) ?
|
|
112
|
+
return convertRgbToHex(isRgbLike(rgb) ? getRgbValues(rgb) : { ...COLOR_DEFAULTS.rgb }, alpha ?? false);
|
|
106
113
|
}
|
|
107
114
|
/**
|
|
108
115
|
* Convert an _RGB(A)_ color to an _HSL_ color
|
|
@@ -166,5 +173,19 @@ function rgbToHwb(rgb) {
|
|
|
166
173
|
function rgbToHwba(rgb) {
|
|
167
174
|
return convertRgbToHwba(rgb);
|
|
168
175
|
}
|
|
176
|
+
function setRgbValue(value) {
|
|
177
|
+
setRgbColorValueInState(this, value, false);
|
|
178
|
+
}
|
|
179
|
+
function setRgbColorValueInState(instance, value, alpha) {
|
|
180
|
+
if (!isRgbLike(value)) return;
|
|
181
|
+
setStateValue(instance, COLOR_TYPE.rgb, {
|
|
182
|
+
red: getHexValue(value.red),
|
|
183
|
+
green: getHexValue(value.green),
|
|
184
|
+
blue: getHexValue(value.blue)
|
|
185
|
+
}, alpha ? value.alpha : void 0);
|
|
186
|
+
}
|
|
187
|
+
function setRgbaValue(value) {
|
|
188
|
+
setRgbColorValueInState(this, value, true);
|
|
189
|
+
}
|
|
169
190
|
//#endregion
|
|
170
|
-
export { convertRgbToHex, convertRgbToHsla, convertRgbToHwba, getColorFromRgb,
|
|
191
|
+
export { convertRgbToHex, convertRgbToHsla, convertRgbToHwba, getColorFromRgb, getRgbColor, getRgbValues, getRgbaColor, rgbToHex, rgbToHsl, rgbToHsla, rgbToHwb, rgbToHwba, setRgbValue, setRgbaValue };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GenericAsyncCallback, GenericCallback } from "../models.mjs";
|
|
2
|
+
import { AsyncLimiter, Limiter } from "../internal/function/limit.mjs";
|
|
2
3
|
//#region src/function/limit.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* Debounce a function, ensuring it is only called after `time` milliseconds have passed
|
|
@@ -12,7 +13,7 @@ import { AsyncCancelableCallback, CancelableCallback, GenericAsyncCallback, Gene
|
|
|
12
13
|
* @param time Time in milliseconds to wait before calling the callback _(defaults to `0`; e.g., as soon as possible)_
|
|
13
14
|
* @returns Debounced callback handler with a `cancel` method
|
|
14
15
|
*/
|
|
15
|
-
declare function asyncDebounce<Callback extends GenericAsyncCallback | GenericCallback>(callback: Callback, time?: number):
|
|
16
|
+
export declare function asyncDebounce<Callback extends GenericAsyncCallback | GenericCallback>(callback: Callback, time?: number): AsyncLimiter<Callback>;
|
|
16
17
|
/**
|
|
17
18
|
* Throttle a function, ensuring it is only called once every `time` milliseconds
|
|
18
19
|
*
|
|
@@ -25,7 +26,7 @@ declare function asyncDebounce<Callback extends GenericAsyncCallback | GenericCa
|
|
|
25
26
|
* @param time Time in milliseconds to wait before calling the callback again _(defaults to `0`; e.g., as soon as possible)_
|
|
26
27
|
* @returns Throttled callback handler with a `cancel` method
|
|
27
28
|
*/
|
|
28
|
-
declare function asyncThrottle<Callback extends GenericAsyncCallback | GenericCallback>(callback: Callback, time?: number):
|
|
29
|
+
export declare function asyncThrottle<Callback extends GenericAsyncCallback | GenericCallback>(callback: Callback, time?: number): AsyncLimiter<Callback>;
|
|
29
30
|
/**
|
|
30
31
|
* Debounce a function, ensuring it is only called after `time` milliseconds have passed
|
|
31
32
|
*
|
|
@@ -35,8 +36,8 @@ declare function asyncThrottle<Callback extends GenericAsyncCallback | GenericCa
|
|
|
35
36
|
* @param time Time in milliseconds to wait before calling the callback _(defaults to `0`; e.g., as soon as possible)_
|
|
36
37
|
* @returns Debounced callback handler with a `cancel` method
|
|
37
38
|
*/
|
|
38
|
-
declare function debounce<Callback extends GenericCallback>(callback: Callback, time?: number):
|
|
39
|
-
declare namespace debounce {
|
|
39
|
+
export declare function debounce<Callback extends GenericCallback>(callback: Callback, time?: number): Limiter<Callback>;
|
|
40
|
+
export declare namespace debounce {
|
|
40
41
|
var async: typeof asyncDebounce;
|
|
41
42
|
}
|
|
42
43
|
/**
|
|
@@ -46,9 +47,9 @@ declare namespace debounce {
|
|
|
46
47
|
* @param time Time in milliseconds to wait before calling the callback again _(defaults to `0`; e.g., as soon as possible)_
|
|
47
48
|
* @returns Throttled callback handler with a `cancel` method
|
|
48
49
|
*/
|
|
49
|
-
declare function throttle<Callback extends GenericCallback>(callback: Callback, time?: number):
|
|
50
|
-
declare namespace throttle {
|
|
50
|
+
export declare function throttle<Callback extends GenericCallback>(callback: Callback, time?: number): Limiter<Callback>;
|
|
51
|
+
export declare namespace throttle {
|
|
51
52
|
var async: typeof asyncThrottle;
|
|
52
53
|
}
|
|
53
54
|
//#endregion
|
|
54
|
-
export {
|
|
55
|
+
export type { AsyncLimiter, Limiter };
|
package/dist/function/limit.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LIMITER_DEBOUNCE, LIMITER_THROTTLE, getAsyncLimiter, getLimiter } from "../internal/function/limit.mjs";
|
|
2
2
|
//#region src/function/limit.ts
|
|
3
3
|
/**
|
|
4
4
|
* Debounce a function, ensuring it is only called after `time` milliseconds have passed
|
|
@@ -13,7 +13,7 @@ import { TIMER_DEBOUNCE, TIMER_THROTTLE, getAsyncTimer, getTimer } from "../inte
|
|
|
13
13
|
* @returns Debounced callback handler with a `cancel` method
|
|
14
14
|
*/
|
|
15
15
|
function asyncDebounce(callback, time) {
|
|
16
|
-
return
|
|
16
|
+
return getAsyncLimiter(LIMITER_DEBOUNCE, callback, time);
|
|
17
17
|
}
|
|
18
18
|
/**
|
|
19
19
|
* Throttle a function, ensuring it is only called once every `time` milliseconds
|
|
@@ -28,7 +28,7 @@ function asyncDebounce(callback, time) {
|
|
|
28
28
|
* @returns Throttled callback handler with a `cancel` method
|
|
29
29
|
*/
|
|
30
30
|
function asyncThrottle(callback, time) {
|
|
31
|
-
return
|
|
31
|
+
return getAsyncLimiter(LIMITER_THROTTLE, callback, time);
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* Debounce a function, ensuring it is only called after `time` milliseconds have passed
|
|
@@ -40,7 +40,7 @@ function asyncThrottle(callback, time) {
|
|
|
40
40
|
* @returns Debounced callback handler with a `cancel` method
|
|
41
41
|
*/
|
|
42
42
|
function debounce(callback, time) {
|
|
43
|
-
return
|
|
43
|
+
return getLimiter(LIMITER_DEBOUNCE, callback, time);
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
46
46
|
* Throttle a function, ensuring it is only called once every `time` milliseconds
|
|
@@ -50,11 +50,9 @@ function debounce(callback, time) {
|
|
|
50
50
|
* @returns Throttled callback handler with a `cancel` method
|
|
51
51
|
*/
|
|
52
52
|
function throttle(callback, time) {
|
|
53
|
-
return
|
|
53
|
+
return getLimiter(LIMITER_THROTTLE, callback, time);
|
|
54
54
|
}
|
|
55
55
|
debounce.async = asyncDebounce;
|
|
56
56
|
throttle.async = asyncThrottle;
|
|
57
|
-
Object.defineProperty(debounce, "async", { value: asyncDebounce });
|
|
58
|
-
Object.defineProperty(throttle, "async", { value: asyncThrottle });
|
|
59
57
|
//#endregion
|
|
60
58
|
export { asyncDebounce, asyncThrottle, debounce, throttle };
|
package/dist/function/once.d.mts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { GenericAsyncCallback, GenericCallback,
|
|
1
|
+
import { GenericAsyncCallback, GenericCallback, Once, OnceAsync } from "../models.mjs";
|
|
2
2
|
//#region src/function/once.d.ts
|
|
3
|
+
declare function OnceAsync(this: any, callback: GenericAsyncCallback): void;
|
|
4
|
+
declare function Once(this: any, callback: GenericCallback): void;
|
|
3
5
|
/**
|
|
4
6
|
* Create an asynchronous function that can only be called once, rejecting or resolving the same result on subsequent calls
|
|
5
7
|
*
|
|
@@ -8,16 +10,34 @@ import { GenericAsyncCallback, GenericCallback, OnceAsyncCallback, OnceCallback
|
|
|
8
10
|
* @param callback Callback to use once
|
|
9
11
|
* @returns _Once_ callback
|
|
10
12
|
*/
|
|
11
|
-
declare function asyncOnce<Callback extends GenericAsyncCallback>(callback: Callback):
|
|
13
|
+
export declare function asyncOnce<Callback extends GenericAsyncCallback>(callback: Callback): OnceAsync<Callback>;
|
|
14
|
+
export declare namespace asyncOnce {
|
|
15
|
+
var is: typeof isAsyncOnce;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Is the value an asynchronous once callback?
|
|
19
|
+
*
|
|
20
|
+
* @param value Value to check
|
|
21
|
+
* @returns `true` if the value is an asynchronous once callback, otherwise `false`
|
|
22
|
+
*/
|
|
23
|
+
export declare function isAsyncOnce(value: unknown): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Is the value a once callback?
|
|
26
|
+
*
|
|
27
|
+
* @param value Value to check
|
|
28
|
+
* @returns `true` if the value is a once callback, otherwise `false`
|
|
29
|
+
*/
|
|
30
|
+
export declare function isOnce(value: unknown): boolean;
|
|
12
31
|
/**
|
|
13
32
|
* Create a function that can only be called once, returning the same value on subsequent calls
|
|
14
33
|
*
|
|
15
34
|
* @param callback Callback to use once
|
|
16
35
|
* @returns _Once_ callback
|
|
17
36
|
*/
|
|
18
|
-
declare function once<Callback extends GenericCallback>(callback: Callback):
|
|
19
|
-
declare namespace once {
|
|
37
|
+
export declare function once<Callback extends GenericCallback>(callback: Callback): Once<Callback>;
|
|
38
|
+
export declare namespace once {
|
|
20
39
|
var async: typeof asyncOnce;
|
|
40
|
+
var is: typeof isOnce;
|
|
41
|
+
var isAsync: typeof isAsyncOnce;
|
|
21
42
|
}
|
|
22
|
-
//#endregion
|
|
23
|
-
export { asyncOnce, once };
|
|
43
|
+
//#endregion
|
package/dist/function/once.mjs
CHANGED
|
@@ -1,5 +1,61 @@
|
|
|
1
1
|
import { assert } from "../internal/function/assert.mjs";
|
|
2
2
|
//#region src/function/once.ts
|
|
3
|
+
const ONCE_NAME_ASYNC = "asyncOnce";
|
|
4
|
+
const ONCE_NAME_SYNC = "once";
|
|
5
|
+
const ONCE_PROPERTY = "$once";
|
|
6
|
+
function AsyncOnce(callback) {
|
|
7
|
+
this[ONCE_SYMBOL] = {
|
|
8
|
+
callback,
|
|
9
|
+
called: false,
|
|
10
|
+
cleared: false,
|
|
11
|
+
error: false,
|
|
12
|
+
finished: false,
|
|
13
|
+
items: [],
|
|
14
|
+
value: void 0
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
AsyncOnce.prototype[ONCE_PROPERTY] = ONCE_NAME_ASYNC;
|
|
18
|
+
AsyncOnce.prototype.clear = clearOnce;
|
|
19
|
+
AsyncOnce.prototype.run = runOnceAsync;
|
|
20
|
+
Object.defineProperties(AsyncOnce.prototype, {
|
|
21
|
+
called: {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: getOnceCalled
|
|
24
|
+
},
|
|
25
|
+
cleared: {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: getOnceCleared
|
|
28
|
+
},
|
|
29
|
+
error: {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
get: getAsyncOnceError
|
|
32
|
+
},
|
|
33
|
+
finished: {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
get: getAsyncOnceFinished
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
function Once(callback) {
|
|
39
|
+
this[ONCE_SYMBOL] = {
|
|
40
|
+
callback,
|
|
41
|
+
called: false,
|
|
42
|
+
cleared: false,
|
|
43
|
+
value: void 0
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
Once.prototype[ONCE_PROPERTY] = ONCE_NAME_SYNC;
|
|
47
|
+
Once.prototype.clear = clearOnce;
|
|
48
|
+
Once.prototype.run = runOnce;
|
|
49
|
+
Object.defineProperties(Once.prototype, {
|
|
50
|
+
called: {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
get: getOnceCalled
|
|
53
|
+
},
|
|
54
|
+
cleared: {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
get: getOnceCleared
|
|
57
|
+
}
|
|
58
|
+
});
|
|
3
59
|
/**
|
|
4
60
|
* Create an asynchronous function that can only be called once, rejecting or resolving the same result on subsequent calls
|
|
5
61
|
*
|
|
@@ -10,62 +66,27 @@ import { assert } from "../internal/function/assert.mjs";
|
|
|
10
66
|
*/
|
|
11
67
|
function asyncOnce(callback) {
|
|
12
68
|
assert(() => typeof callback === "function", ONCE_MESSAGE_EXPECTATION);
|
|
13
|
-
|
|
14
|
-
called: false,
|
|
15
|
-
cleared: false,
|
|
16
|
-
error: false,
|
|
17
|
-
finished: false,
|
|
18
|
-
items: [],
|
|
19
|
-
value: void 0
|
|
20
|
-
};
|
|
21
|
-
const fn = (...parameters) => {
|
|
22
|
-
if (state.cleared) return Promise.reject(new Error(ONCE_MESSAGE_CLEARED));
|
|
23
|
-
if (state.finished) return state.error ? Promise.reject(state.value) : Promise.resolve(state.value);
|
|
24
|
-
if (state.called) return new Promise((resolve, reject) => {
|
|
25
|
-
state.items.push({
|
|
26
|
-
reject,
|
|
27
|
-
resolve
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
state.called = true;
|
|
31
|
-
return new Promise((resolve, reject) => {
|
|
32
|
-
state.items.push({
|
|
33
|
-
reject,
|
|
34
|
-
resolve
|
|
35
|
-
});
|
|
36
|
-
callback(...parameters).then((value) => {
|
|
37
|
-
handleOnceResult(state, value, false);
|
|
38
|
-
}).catch((error) => {
|
|
39
|
-
handleOnceResult(state, error, true);
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
};
|
|
43
|
-
Object.defineProperties(fn, {
|
|
44
|
-
called: {
|
|
45
|
-
enumerable: true,
|
|
46
|
-
get: () => state.called
|
|
47
|
-
},
|
|
48
|
-
clear: { value: () => clearState(state) },
|
|
49
|
-
cleared: {
|
|
50
|
-
enumerable: true,
|
|
51
|
-
get: () => state.cleared
|
|
52
|
-
},
|
|
53
|
-
error: {
|
|
54
|
-
enumerable: true,
|
|
55
|
-
get: () => state.error
|
|
56
|
-
},
|
|
57
|
-
finished: {
|
|
58
|
-
enumerable: true,
|
|
59
|
-
get: () => state.finished
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
return fn;
|
|
69
|
+
return new AsyncOnce(callback);
|
|
63
70
|
}
|
|
64
|
-
function
|
|
71
|
+
function clearOnce() {
|
|
72
|
+
const state = this[ONCE_SYMBOL];
|
|
65
73
|
if (!state.called || state.cleared) return;
|
|
74
|
+
state.callback = void 0;
|
|
66
75
|
state.cleared = true;
|
|
67
76
|
state.value = void 0;
|
|
68
77
|
}
|
|
78
|
+
function getAsyncOnceError() {
|
|
79
|
+
return this[ONCE_SYMBOL].error;
|
|
80
|
+
}
|
|
81
|
+
function getAsyncOnceFinished() {
|
|
82
|
+
return this[ONCE_SYMBOL].finished;
|
|
83
|
+
}
|
|
84
|
+
function getOnceCalled() {
|
|
85
|
+
return this[ONCE_SYMBOL].called;
|
|
86
|
+
}
|
|
87
|
+
function getOnceCleared() {
|
|
88
|
+
return this[ONCE_SYMBOL].cleared;
|
|
89
|
+
}
|
|
69
90
|
function handleOnceResult(state, value, error) {
|
|
70
91
|
state.error = error;
|
|
71
92
|
state.finished = true;
|
|
@@ -79,6 +100,27 @@ function handleOnceResult(state, value, error) {
|
|
|
79
100
|
}
|
|
80
101
|
}
|
|
81
102
|
/**
|
|
103
|
+
* Is the value an asynchronous once callback?
|
|
104
|
+
*
|
|
105
|
+
* @param value Value to check
|
|
106
|
+
* @returns `true` if the value is an asynchronous once callback, otherwise `false`
|
|
107
|
+
*/
|
|
108
|
+
function isAsyncOnce(value) {
|
|
109
|
+
return isOnceInstance(value, ONCE_NAME_ASYNC);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Is the value a once callback?
|
|
113
|
+
*
|
|
114
|
+
* @param value Value to check
|
|
115
|
+
* @returns `true` if the value is a once callback, otherwise `false`
|
|
116
|
+
*/
|
|
117
|
+
function isOnce(value) {
|
|
118
|
+
return isOnceInstance(value, ONCE_NAME_SYNC);
|
|
119
|
+
}
|
|
120
|
+
function isOnceInstance(value, name) {
|
|
121
|
+
return typeof value === "object" && value !== null && ONCE_PROPERTY in value && value[ONCE_PROPERTY] === name;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
82
124
|
* Create a function that can only be called once, returning the same value on subsequent calls
|
|
83
125
|
*
|
|
84
126
|
* @param callback Callback to use once
|
|
@@ -86,34 +128,45 @@ function handleOnceResult(state, value, error) {
|
|
|
86
128
|
*/
|
|
87
129
|
function once(callback) {
|
|
88
130
|
assert(() => typeof callback === "function", ONCE_MESSAGE_EXPECTATION);
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
131
|
+
return new Once(callback);
|
|
132
|
+
}
|
|
133
|
+
function runOnce(...parameters) {
|
|
134
|
+
const state = this[ONCE_SYMBOL];
|
|
135
|
+
if (state.cleared) throw new Error(ONCE_MESSAGE_CLEARED);
|
|
136
|
+
if (state.called) return state.value;
|
|
137
|
+
state.called = true;
|
|
138
|
+
state.value = state.callback(...parameters);
|
|
139
|
+
return state.value;
|
|
140
|
+
}
|
|
141
|
+
function runOnceAsync(...parameters) {
|
|
142
|
+
const state = this[ONCE_SYMBOL];
|
|
143
|
+
if (state.cleared) return Promise.reject(new Error(ONCE_MESSAGE_CLEARED));
|
|
144
|
+
if (state.finished) return state.error ? Promise.reject(state.value) : Promise.resolve(state.value);
|
|
145
|
+
if (state.called) return new Promise((resolve, reject) => {
|
|
146
|
+
state.items.push({
|
|
147
|
+
reject,
|
|
148
|
+
resolve
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
state.called = true;
|
|
152
|
+
return new Promise((resolve, reject) => {
|
|
153
|
+
state.items.push({
|
|
154
|
+
reject,
|
|
155
|
+
resolve
|
|
156
|
+
});
|
|
157
|
+
state.callback(...parameters).then((value) => {
|
|
158
|
+
handleOnceResult(state, value, false);
|
|
159
|
+
}).catch((error) => {
|
|
160
|
+
handleOnceResult(state, error, true);
|
|
161
|
+
});
|
|
111
162
|
});
|
|
112
|
-
return fn;
|
|
113
163
|
}
|
|
114
164
|
const ONCE_MESSAGE_CLEARED = "Once has been cleared";
|
|
115
165
|
const ONCE_MESSAGE_EXPECTATION = "Once expected a function";
|
|
166
|
+
const ONCE_SYMBOL = Symbol(ONCE_PROPERTY);
|
|
167
|
+
asyncOnce.is = isAsyncOnce;
|
|
116
168
|
once.async = asyncOnce;
|
|
117
|
-
|
|
169
|
+
once.is = isOnce;
|
|
170
|
+
once.isAsync = isAsyncOnce;
|
|
118
171
|
//#endregion
|
|
119
|
-
export { asyncOnce, once };
|
|
172
|
+
export { asyncOnce, isAsyncOnce, isOnce, once };
|
|
@@ -3,11 +3,11 @@ import { GenericAsyncCallback, GenericCallback } from "../models.mjs";
|
|
|
3
3
|
/**
|
|
4
4
|
* An error thrown when a retry fails
|
|
5
5
|
*/
|
|
6
|
-
declare class RetryError extends Error {
|
|
6
|
+
export declare class RetryError extends Error {
|
|
7
7
|
readonly original: unknown;
|
|
8
8
|
constructor(message: string, original: unknown);
|
|
9
9
|
}
|
|
10
|
-
type RetryOptions = {
|
|
10
|
+
export type RetryOptions = {
|
|
11
11
|
delay?: number;
|
|
12
12
|
times?: number;
|
|
13
13
|
when?: (error: unknown) => boolean;
|
|
@@ -39,9 +39,8 @@ declare function asyncRetry<Callback extends GenericCallback>(callback: Callback
|
|
|
39
39
|
* @param options Retry options
|
|
40
40
|
* @returns Callback result
|
|
41
41
|
*/
|
|
42
|
-
declare function retry<Callback extends GenericCallback>(callback: Callback, options?: Omit<RetryOptions, 'delay'>): ReturnType<Callback>;
|
|
43
|
-
declare namespace retry {
|
|
42
|
+
export declare function retry<Callback extends GenericCallback>(callback: Callback, options?: Omit<RetryOptions, 'delay'>): ReturnType<Callback>;
|
|
43
|
+
export declare namespace retry {
|
|
44
44
|
var async: typeof asyncRetry;
|
|
45
45
|
}
|
|
46
|
-
//#endregion
|
|
47
|
-
export { RetryError, RetryOptions, retry };
|
|
46
|
+
//#endregion
|
package/dist/function/retry.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isPlainObject } from "../internal/is.mjs";
|
|
2
2
|
import { getNumberOrDefault } from "../internal/defaults.mjs";
|
|
3
|
-
import {
|
|
3
|
+
import { LIMITER_WAIT, getLimiter } from "../internal/function/limit.mjs";
|
|
4
4
|
//#region src/function/retry.ts
|
|
5
5
|
/**
|
|
6
6
|
* An error thrown when a retry fails
|
|
@@ -32,12 +32,12 @@ async function asyncRetry(callback, options) {
|
|
|
32
32
|
if (attempts >= times || !when(error)) rejector(new RetryError(RETRY_MESSAGE_FAILED, error));
|
|
33
33
|
else {
|
|
34
34
|
attempts += 1;
|
|
35
|
-
|
|
35
|
+
limiter.run();
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
const { delay, times, when } = createRetryOptions(options);
|
|
40
|
-
const
|
|
40
|
+
const limiter = getLimiter(LIMITER_WAIT, handle, delay);
|
|
41
41
|
let attempts = 0;
|
|
42
42
|
let rejector;
|
|
43
43
|
let resolver;
|
|
@@ -83,6 +83,5 @@ const RETRY_ERROR_NAME = "RetryError";
|
|
|
83
83
|
const RETRY_MESSAGE_EXPECTATION = "Retry expected a function";
|
|
84
84
|
const RETRY_MESSAGE_FAILED = "Retry failed";
|
|
85
85
|
retry.async = asyncRetry;
|
|
86
|
-
Object.defineProperty(retry, "async", { value: asyncRetry });
|
|
87
86
|
//#endregion
|
|
88
87
|
export { RetryError, retry };
|