@oscarpalmer/atoms 0.149.0 → 0.151.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/atoms.full.js +175 -169
- package/dist/index.js +9 -4
- package/dist/internal/function/limiter.js +3 -4
- package/dist/promise/delay.js +28 -0
- package/dist/promise/helpers.js +51 -0
- package/dist/promise/index.js +73 -0
- package/dist/promise/misc.js +36 -0
- package/dist/promise/models.js +37 -0
- package/dist/promise/timed.js +33 -0
- package/dist/result/index.js +4 -27
- package/dist/result/misc.js +40 -0
- package/package.json +3 -3
- package/src/index.ts +1 -5
- package/src/internal/function/limiter.ts +3 -4
- package/src/promise/delay.ts +63 -0
- package/src/promise/helpers.ts +91 -0
- package/src/promise/index.ts +230 -0
- package/src/promise/misc.ts +89 -0
- package/src/promise/models.ts +131 -0
- package/src/promise/timed.ts +90 -0
- package/src/result/index.ts +4 -66
- package/src/result/misc.ts +115 -0
- package/types/index.d.ts +1 -3
- package/types/promise/delay.d.ts +13 -0
- package/types/promise/helpers.d.ts +17 -0
- package/types/promise/index.d.ts +53 -0
- package/types/promise/misc.d.ts +22 -0
- package/types/promise/models.d.ts +81 -0
- package/types/promise/timed.d.ts +17 -0
- package/types/result/index.d.ts +3 -34
- package/types/result/misc.d.ts +51 -0
- package/dist/internal/frame-rate.js +0 -25
- package/dist/promise.js +0 -221
- package/src/internal/frame-rate.ts +0 -63
- package/src/promise.ts +0 -538
- package/types/internal/frame-rate.d.ts +0 -2
- package/types/promise.d.ts +0 -139
package/dist/atoms.full.js
CHANGED
|
@@ -1,27 +1,3 @@
|
|
|
1
|
-
function calculate() {
|
|
2
|
-
return new Promise((resolve) => {
|
|
3
|
-
const values = [];
|
|
4
|
-
let last;
|
|
5
|
-
function step(now) {
|
|
6
|
-
if (last != null) values.push(now - last);
|
|
7
|
-
last = now;
|
|
8
|
-
if (values.length >= TOTAL) resolve(values.sort().slice(TRIM_PART, -TRIM_PART).reduce((first, second) => first + second, 0) / (values.length - TRIM_TOTAL));
|
|
9
|
-
else requestAnimationFrame(step);
|
|
10
|
-
}
|
|
11
|
-
requestAnimationFrame(step);
|
|
12
|
-
});
|
|
13
|
-
}
|
|
14
|
-
const TOTAL = 10;
|
|
15
|
-
const TRIM_PART = 2;
|
|
16
|
-
const TRIM_TOTAL = 4;
|
|
17
|
-
let FRAME_RATE_MS = 1e3 / 60;
|
|
18
|
-
/**
|
|
19
|
-
* A calculated average of the refresh rate of the display _(in milliseconds)_
|
|
20
|
-
*/
|
|
21
|
-
calculate().then((value) => {
|
|
22
|
-
FRAME_RATE_MS = value;
|
|
23
|
-
});
|
|
24
|
-
var frame_rate_default = FRAME_RATE_MS;
|
|
25
1
|
function getArrayCallback(value) {
|
|
26
2
|
switch (typeof value) {
|
|
27
3
|
case "function": return value;
|
|
@@ -702,9 +678,9 @@ function unique(array, key) {
|
|
|
702
678
|
return array.length > 1 ? findValues("unique", array, [key, void 0]).matched : array;
|
|
703
679
|
}
|
|
704
680
|
function getLimiter(callback, throttler, time) {
|
|
705
|
-
const interval = typeof time === "number" && time
|
|
681
|
+
const interval = typeof time === "number" && time > 0 ? time : 0;
|
|
706
682
|
function step(now, parameters) {
|
|
707
|
-
if (interval ===
|
|
683
|
+
if (interval === 0 || now - timestamp >= interval) {
|
|
708
684
|
if (throttler) timestamp = now;
|
|
709
685
|
callback(...parameters);
|
|
710
686
|
} else frame = requestAnimationFrame((next) => {
|
|
@@ -716,7 +692,7 @@ function getLimiter(callback, throttler, time) {
|
|
|
716
692
|
const limiter = (...parameters) => {
|
|
717
693
|
limiter.cancel();
|
|
718
694
|
frame = requestAnimationFrame((now) => {
|
|
719
|
-
timestamp ??= now
|
|
695
|
+
timestamp ??= now;
|
|
720
696
|
step(now, parameters);
|
|
721
697
|
});
|
|
722
698
|
};
|
|
@@ -3009,60 +2985,22 @@ var CancelablePromise = class extends Promise {
|
|
|
3009
2985
|
};
|
|
3010
2986
|
var PromiseTimeoutError = class extends Error {
|
|
3011
2987
|
constructor() {
|
|
3012
|
-
super(
|
|
3013
|
-
this.name =
|
|
2988
|
+
super(PROMISE_MESSAGE_TIMEOUT);
|
|
2989
|
+
this.name = PROMISE_ERROR_NAME;
|
|
3014
2990
|
}
|
|
3015
2991
|
};
|
|
3016
|
-
|
|
3017
|
-
|
|
3018
|
-
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
|
|
3022
|
-
|
|
3023
|
-
|
|
3024
|
-
|
|
3025
|
-
|
|
3026
|
-
|
|
3027
|
-
|
|
3028
|
-
settlePromise(abort, resolve, result, signal);
|
|
3029
|
-
} catch (error) {
|
|
3030
|
-
settlePromise(abort, reject, error, signal);
|
|
3031
|
-
}
|
|
3032
|
-
}
|
|
3033
|
-
let rejector;
|
|
3034
|
-
signal?.addEventListener(EVENT_NAME$1, abort, ABORT_OPTIONS);
|
|
3035
|
-
const promise = new Promise((resolve, reject) => {
|
|
3036
|
-
rejector = reject;
|
|
3037
|
-
handler(resolve, reject);
|
|
3038
|
-
});
|
|
3039
|
-
return time > 0 ? getTimed(promise, time, signal) : promise;
|
|
3040
|
-
}
|
|
3041
|
-
/**
|
|
3042
|
-
* Create a cancelable promise
|
|
3043
|
-
* @param executor Executor function for the promise
|
|
3044
|
-
* @returns Cancelable promise
|
|
3045
|
-
*/
|
|
3046
|
-
function cancelable(executor) {
|
|
3047
|
-
return new CancelablePromise(executor);
|
|
3048
|
-
}
|
|
3049
|
-
function delay(options) {
|
|
3050
|
-
const { signal, time } = getPromiseOptions(options);
|
|
3051
|
-
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
3052
|
-
function abort() {
|
|
3053
|
-
clearTimeout(timeout);
|
|
3054
|
-
rejector(signal.reason);
|
|
3055
|
-
}
|
|
3056
|
-
signal?.addEventListener("abort", abort, ABORT_OPTIONS);
|
|
3057
|
-
let rejector;
|
|
3058
|
-
let timeout;
|
|
3059
|
-
return new Promise((resolve, reject) => {
|
|
3060
|
-
rejector = reject;
|
|
3061
|
-
timeout = setTimeout(() => {
|
|
3062
|
-
settlePromise(abort, resolve, void 0, signal);
|
|
3063
|
-
}, time);
|
|
3064
|
-
});
|
|
3065
|
-
}
|
|
2992
|
+
const PROMISE_ABORT_OPTIONS = { once: true };
|
|
2993
|
+
const PROMISE_ERROR_NAME = "PromiseTimeoutError";
|
|
2994
|
+
const PROMISE_EVENT_NAME = "abort";
|
|
2995
|
+
const PROMISE_MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
|
|
2996
|
+
const PROMISE_MESSAGE_EXPECTATION_PROMISES = "Promises expected an array of promises";
|
|
2997
|
+
const PROMISE_MESSAGE_EXPECTATION_RESULT = "toResult expected a Promise";
|
|
2998
|
+
const PROMISE_MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
|
|
2999
|
+
const PROMISE_MESSAGE_TIMEOUT = "Promise timed out";
|
|
3000
|
+
const PROMISE_STRATEGY_ALL = new Set(["complete", "first"]);
|
|
3001
|
+
const PROMISE_STRATEGY_DEFAULT = "complete";
|
|
3002
|
+
const PROMISE_TYPE_FULFILLED = "fulfilled";
|
|
3003
|
+
const PROMISE_TYPE_REJECTED = "rejected";
|
|
3066
3004
|
function getNumberOrDefault$1(value) {
|
|
3067
3005
|
return typeof value === "number" && value > 0 ? value : 0;
|
|
3068
3006
|
}
|
|
@@ -3082,7 +3020,7 @@ function getPromisesOptions(input) {
|
|
|
3082
3020
|
if (typeof input === "string") return { strategy: getStrategyOrDefault(input) };
|
|
3083
3021
|
if (input instanceof AbortSignal) return {
|
|
3084
3022
|
signal: input,
|
|
3085
|
-
strategy:
|
|
3023
|
+
strategy: PROMISE_STRATEGY_DEFAULT
|
|
3086
3024
|
};
|
|
3087
3025
|
const options = typeof input === "object" && input !== null ? input : {};
|
|
3088
3026
|
return {
|
|
@@ -3091,35 +3029,81 @@ function getPromisesOptions(input) {
|
|
|
3091
3029
|
};
|
|
3092
3030
|
}
|
|
3093
3031
|
function getStrategyOrDefault(value) {
|
|
3094
|
-
return
|
|
3032
|
+
return PROMISE_STRATEGY_ALL.has(value) ? value : PROMISE_STRATEGY_DEFAULT;
|
|
3095
3033
|
}
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
3034
|
+
/**
|
|
3035
|
+
* Is the value a fulfilled promise result?
|
|
3036
|
+
* @param value Value to check
|
|
3037
|
+
* @returns `true` if the value is a fulfilled promise result, `false` otherwise
|
|
3038
|
+
*/
|
|
3039
|
+
function isFulfilled(value) {
|
|
3040
|
+
return isType(value, PROMISE_TYPE_FULFILLED);
|
|
3041
|
+
}
|
|
3042
|
+
/**
|
|
3043
|
+
* Is the value a rejected promise result?
|
|
3044
|
+
* @param value Value to check
|
|
3045
|
+
* @returns `true` if the value is a rejected promise result, `false` otherwise
|
|
3046
|
+
*/
|
|
3047
|
+
function isRejected(value) {
|
|
3048
|
+
return isType(value, PROMISE_TYPE_REJECTED);
|
|
3049
|
+
}
|
|
3050
|
+
function isType(value, type) {
|
|
3051
|
+
return typeof value === "object" && value !== null && value.status === type;
|
|
3052
|
+
}
|
|
3053
|
+
function error(value, original) {
|
|
3054
|
+
return getError(value, original);
|
|
3055
|
+
}
|
|
3056
|
+
function getError(value, original) {
|
|
3057
|
+
const errorResult = {
|
|
3058
|
+
error: value,
|
|
3059
|
+
ok: false
|
|
3060
|
+
};
|
|
3061
|
+
if (original instanceof Error) errorResult.original = original;
|
|
3062
|
+
return errorResult;
|
|
3063
|
+
}
|
|
3064
|
+
/**
|
|
3065
|
+
* Creates an ok result
|
|
3066
|
+
* @param value Value
|
|
3067
|
+
* @returns Ok result
|
|
3068
|
+
*/
|
|
3069
|
+
function ok(value) {
|
|
3070
|
+
return {
|
|
3071
|
+
ok: true,
|
|
3072
|
+
value
|
|
3073
|
+
};
|
|
3074
|
+
}
|
|
3075
|
+
/**
|
|
3076
|
+
* Converts a result to a promise
|
|
3077
|
+
*
|
|
3078
|
+
* Resolves if ok, rejects for error
|
|
3079
|
+
* @param result Result to convert
|
|
3080
|
+
* @returns Promised result
|
|
3081
|
+
*/
|
|
3082
|
+
async function toPromise(result) {
|
|
3083
|
+
const actual = typeof result === "function" ? result() : result;
|
|
3084
|
+
if (!isResult(actual)) return Promise.reject(new Error(MESSAGE_PROMISE_RESULT));
|
|
3085
|
+
return isOk(actual) ? Promise.resolve(actual.value) : Promise.reject(actual.error);
|
|
3086
|
+
}
|
|
3087
|
+
function unwrap(value, defaultValue) {
|
|
3088
|
+
return isOk(value) ? value.value : defaultValue;
|
|
3089
|
+
}
|
|
3090
|
+
const MESSAGE_PROMISE_RESULT = "toPromise expected to receive a Result";
|
|
3091
|
+
/**
|
|
3092
|
+
* Create a cancelable promise
|
|
3093
|
+
* @param executor Executor function for the promise
|
|
3094
|
+
* @returns Cancelable promise
|
|
3095
|
+
*/
|
|
3096
|
+
function cancelable(executor) {
|
|
3097
|
+
return new CancelablePromise(executor);
|
|
3114
3098
|
}
|
|
3115
3099
|
function handleResult(status, parameters) {
|
|
3116
3100
|
const { abort, complete, data, handlers, index, signal, value } = parameters;
|
|
3117
3101
|
if (signal?.aborted ?? false) return;
|
|
3118
|
-
if (!complete && status ===
|
|
3102
|
+
if (!complete && status === PROMISE_TYPE_REJECTED) {
|
|
3119
3103
|
settlePromise(abort, handlers.reject, value, signal);
|
|
3120
3104
|
return;
|
|
3121
3105
|
}
|
|
3122
|
-
data.result[index] = !complete ? value : status ===
|
|
3106
|
+
data.result[index] = !complete ? value : status === PROMISE_TYPE_FULFILLED ? {
|
|
3123
3107
|
status,
|
|
3124
3108
|
value
|
|
3125
3109
|
} : {
|
|
@@ -3128,37 +3112,105 @@ function handleResult(status, parameters) {
|
|
|
3128
3112
|
};
|
|
3129
3113
|
if (index === data.last) settlePromise(abort, handlers.resolve, data.result, signal);
|
|
3130
3114
|
}
|
|
3131
|
-
|
|
3132
|
-
|
|
3133
|
-
|
|
3134
|
-
* @returns `true` if the value is a fulfilled promise result, `false` otherwise
|
|
3135
|
-
*/
|
|
3136
|
-
function isFulfilled(value) {
|
|
3137
|
-
return isType(value, TYPE_FULFILLED);
|
|
3115
|
+
function settlePromise(aborter, settler, value, signal) {
|
|
3116
|
+
signal?.removeEventListener(PROMISE_EVENT_NAME, aborter);
|
|
3117
|
+
settler(value);
|
|
3138
3118
|
}
|
|
3139
|
-
|
|
3140
|
-
|
|
3141
|
-
|
|
3142
|
-
|
|
3143
|
-
*/
|
|
3144
|
-
function isRejected(value) {
|
|
3145
|
-
return isType(value, TYPE_REJECTED);
|
|
3119
|
+
async function toResult(value) {
|
|
3120
|
+
const actual = typeof value === "function" ? value() : value;
|
|
3121
|
+
if (!(actual instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_RESULT));
|
|
3122
|
+
return actual.then((result) => ok(result)).catch((reason) => error(reason));
|
|
3146
3123
|
}
|
|
3147
|
-
function
|
|
3148
|
-
|
|
3124
|
+
async function getTimedPromise(promise, time, signal) {
|
|
3125
|
+
function abort() {
|
|
3126
|
+
cancelAnimationFrame(frame);
|
|
3127
|
+
rejector(signal.reason);
|
|
3128
|
+
}
|
|
3129
|
+
function run(now) {
|
|
3130
|
+
start ??= now;
|
|
3131
|
+
if (time === 0 || now - start >= time - 5) settlePromise(abort, rejector, new PromiseTimeoutError(), signal);
|
|
3132
|
+
else frame = requestAnimationFrame(run);
|
|
3133
|
+
}
|
|
3134
|
+
signal?.addEventListener(PROMISE_EVENT_NAME, abort, PROMISE_ABORT_OPTIONS);
|
|
3135
|
+
let frame;
|
|
3136
|
+
let rejector;
|
|
3137
|
+
let start;
|
|
3138
|
+
return Promise.race([promise, new Promise((_, reject) => {
|
|
3139
|
+
rejector = reject;
|
|
3140
|
+
frame = requestAnimationFrame(run);
|
|
3141
|
+
})]).then((value) => {
|
|
3142
|
+
cancelAnimationFrame(frame);
|
|
3143
|
+
signal?.removeEventListener(PROMISE_EVENT_NAME, abort);
|
|
3144
|
+
return value;
|
|
3145
|
+
});
|
|
3146
|
+
}
|
|
3147
|
+
async function timed(promise, options) {
|
|
3148
|
+
if (!(promise instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_TIMED));
|
|
3149
|
+
const { signal, time } = getPromiseOptions(options);
|
|
3150
|
+
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
3151
|
+
return time > 0 ? getTimedPromise(promise, time, signal) : promise;
|
|
3152
|
+
}
|
|
3153
|
+
function delay(options) {
|
|
3154
|
+
const { signal, time } = getPromiseOptions(options);
|
|
3155
|
+
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
3156
|
+
function abort() {
|
|
3157
|
+
cancelAnimationFrame(frame);
|
|
3158
|
+
rejector(signal.reason);
|
|
3159
|
+
}
|
|
3160
|
+
function run(now) {
|
|
3161
|
+
start ??= now;
|
|
3162
|
+
if (now - start >= time - 5) settlePromise(abort, resolver, void 0, signal);
|
|
3163
|
+
else frame = requestAnimationFrame(run);
|
|
3164
|
+
}
|
|
3165
|
+
signal?.addEventListener("abort", abort, PROMISE_ABORT_OPTIONS);
|
|
3166
|
+
let frame;
|
|
3167
|
+
let rejector;
|
|
3168
|
+
let resolver;
|
|
3169
|
+
let start;
|
|
3170
|
+
return new Promise((resolve, reject) => {
|
|
3171
|
+
rejector = reject;
|
|
3172
|
+
resolver = resolve;
|
|
3173
|
+
if (time === 0) settlePromise(abort, resolve, void 0, signal);
|
|
3174
|
+
else frame = requestAnimationFrame(run);
|
|
3175
|
+
});
|
|
3176
|
+
}
|
|
3177
|
+
async function attemptPromise(value, options) {
|
|
3178
|
+
const isFunction = typeof value === "function";
|
|
3179
|
+
if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_ATTEMPT));
|
|
3180
|
+
const { signal, time } = getPromiseOptions(options);
|
|
3181
|
+
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
3182
|
+
function abort() {
|
|
3183
|
+
rejector(signal.reason);
|
|
3184
|
+
}
|
|
3185
|
+
async function handler(resolve, reject) {
|
|
3186
|
+
try {
|
|
3187
|
+
let result = isFunction ? value() : await value;
|
|
3188
|
+
if (result instanceof Promise) result = await result;
|
|
3189
|
+
settlePromise(abort, resolve, result, signal);
|
|
3190
|
+
} catch (error) {
|
|
3191
|
+
settlePromise(abort, reject, error, signal);
|
|
3192
|
+
}
|
|
3193
|
+
}
|
|
3194
|
+
let rejector;
|
|
3195
|
+
signal?.addEventListener(PROMISE_EVENT_NAME, abort, PROMISE_ABORT_OPTIONS);
|
|
3196
|
+
const promise = new Promise((resolve, reject) => {
|
|
3197
|
+
rejector = reject;
|
|
3198
|
+
handler(resolve, reject);
|
|
3199
|
+
});
|
|
3200
|
+
return time > 0 ? getTimedPromise(promise, time, signal) : promise;
|
|
3149
3201
|
}
|
|
3150
3202
|
async function promises(items, options) {
|
|
3151
3203
|
const { signal, strategy } = getPromisesOptions(options);
|
|
3152
3204
|
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
3153
|
-
if (!Array.isArray(items)) return Promise.reject(new TypeError(
|
|
3205
|
+
if (!Array.isArray(items)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_PROMISES));
|
|
3154
3206
|
const actual = items.filter((item) => item instanceof Promise);
|
|
3155
3207
|
const { length } = actual;
|
|
3156
3208
|
if (length === 0) return actual;
|
|
3157
|
-
const complete = strategy ===
|
|
3209
|
+
const complete = strategy === PROMISE_STRATEGY_DEFAULT;
|
|
3158
3210
|
function abort() {
|
|
3159
3211
|
handlers.reject(signal.reason);
|
|
3160
3212
|
}
|
|
3161
|
-
signal?.addEventListener("abort", abort,
|
|
3213
|
+
signal?.addEventListener("abort", abort, PROMISE_ABORT_OPTIONS);
|
|
3162
3214
|
const data = {
|
|
3163
3215
|
last: length - 1,
|
|
3164
3216
|
result: []
|
|
@@ -3169,7 +3221,7 @@ async function promises(items, options) {
|
|
|
3169
3221
|
reject,
|
|
3170
3222
|
resolve
|
|
3171
3223
|
};
|
|
3172
|
-
for (let index = 0; index < length; index += 1) actual[index].then((value) => handleResult(
|
|
3224
|
+
for (let index = 0; index < length; index += 1) actual[index].then((value) => handleResult(PROMISE_TYPE_FULFILLED, {
|
|
3173
3225
|
abort,
|
|
3174
3226
|
complete,
|
|
3175
3227
|
data,
|
|
@@ -3177,7 +3229,7 @@ async function promises(items, options) {
|
|
|
3177
3229
|
index,
|
|
3178
3230
|
signal,
|
|
3179
3231
|
value
|
|
3180
|
-
})).catch((reason) => handleResult(
|
|
3232
|
+
})).catch((reason) => handleResult(PROMISE_TYPE_REJECTED, {
|
|
3181
3233
|
abort,
|
|
3182
3234
|
complete,
|
|
3183
3235
|
data,
|
|
@@ -3188,27 +3240,6 @@ async function promises(items, options) {
|
|
|
3188
3240
|
}));
|
|
3189
3241
|
});
|
|
3190
3242
|
}
|
|
3191
|
-
function settlePromise(aborter, settler, value, signal) {
|
|
3192
|
-
signal?.removeEventListener(EVENT_NAME$1, aborter);
|
|
3193
|
-
settler(value);
|
|
3194
|
-
}
|
|
3195
|
-
async function timed(promise, options) {
|
|
3196
|
-
if (!(promise instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_TIMED));
|
|
3197
|
-
const { signal, time } = getPromiseOptions(options);
|
|
3198
|
-
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
3199
|
-
return time > 0 ? getTimed(promise, time, signal) : promise;
|
|
3200
|
-
}
|
|
3201
|
-
const ABORT_OPTIONS = { once: true };
|
|
3202
|
-
const DEFAULT_STRATEGY = "complete";
|
|
3203
|
-
const ERROR_NAME$1 = "PromiseTimeoutError";
|
|
3204
|
-
const EVENT_NAME$1 = "abort";
|
|
3205
|
-
const MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
|
|
3206
|
-
const MESSAGE_EXPECTATION_PROMISES = "Promises expected an array of promises";
|
|
3207
|
-
const MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
|
|
3208
|
-
const MESSAGE_TIMEOUT = "Promise timed out";
|
|
3209
|
-
const strategies = new Set(["complete", "first"]);
|
|
3210
|
-
const TYPE_FULFILLED = "fulfilled";
|
|
3211
|
-
const TYPE_REJECTED = "rejected";
|
|
3212
3243
|
/**
|
|
3213
3244
|
* Convert a query string to a plain _(nested)_ object
|
|
3214
3245
|
* @param query Query string to convert
|
|
@@ -3618,31 +3649,6 @@ attempt.flow = attemptFlow;
|
|
|
3618
3649
|
attempt.match = matchResult;
|
|
3619
3650
|
attempt.pipe = attemptPipe;
|
|
3620
3651
|
attempt.promise = attemptPromise;
|
|
3621
|
-
function error(value, original) {
|
|
3622
|
-
return getError(value, original);
|
|
3623
|
-
}
|
|
3624
|
-
function getError(value, original) {
|
|
3625
|
-
const errorResult = {
|
|
3626
|
-
error: value,
|
|
3627
|
-
ok: false
|
|
3628
|
-
};
|
|
3629
|
-
if (original instanceof Error) errorResult.original = original;
|
|
3630
|
-
return errorResult;
|
|
3631
|
-
}
|
|
3632
|
-
/**
|
|
3633
|
-
* Creates an ok result
|
|
3634
|
-
* @param value Value
|
|
3635
|
-
* @returns Ok result
|
|
3636
|
-
*/
|
|
3637
|
-
function ok(value) {
|
|
3638
|
-
return {
|
|
3639
|
-
ok: true,
|
|
3640
|
-
value
|
|
3641
|
-
};
|
|
3642
|
-
}
|
|
3643
|
-
function unwrap(value, defaultValue) {
|
|
3644
|
-
return isOk(value) ? value.value : defaultValue;
|
|
3645
|
-
}
|
|
3646
3652
|
/**
|
|
3647
3653
|
* - A Set with a maximum size
|
|
3648
3654
|
* - Behavior is similar to a _LRU_-cache, where the oldest values are removed
|
|
@@ -3695,4 +3701,4 @@ var SizedSet = class extends Set {
|
|
|
3695
3701
|
}
|
|
3696
3702
|
}
|
|
3697
3703
|
};
|
|
3698
|
-
export { CancelablePromise,
|
|
3704
|
+
export { CancelablePromise, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, toResult, fromQuery, toPromise as fromResult, toPromise, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, times, titleCase, toMap, toQuery, toRecord, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
|
package/dist/index.js
CHANGED
|
@@ -36,7 +36,6 @@ import { rgbToHex, rgbToHsl, rgbToHsla } from "./color/space/rgb.js";
|
|
|
36
36
|
import { getNormalizedHex, hexToHsl, hexToHsla, hexToRgb, hexToRgba } from "./color/space/hex.js";
|
|
37
37
|
import { hslToHex, hslToRgb, hslToRgba } from "./color/space/hsl.js";
|
|
38
38
|
import { getColor } from "./color/index.js";
|
|
39
|
-
import frame_rate_default from "./internal/frame-rate.js";
|
|
40
39
|
import { debounce } from "./function/debounce.js";
|
|
41
40
|
import { SizedMap } from "./sized/map.js";
|
|
42
41
|
import { memoize } from "./function/memoize.js";
|
|
@@ -60,10 +59,16 @@ import { unsmush } from "./value/unsmush.js";
|
|
|
60
59
|
import { isEmpty, isInstanceOf, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumerical, isObject, isPrimitive } from "./is.js";
|
|
61
60
|
import { logger } from "./logger.js";
|
|
62
61
|
import { average, ceil, count, floor, median, min, round, sum } from "./math.js";
|
|
63
|
-
import { CancelablePromise, PromiseTimeoutError
|
|
62
|
+
import { CancelablePromise, PromiseTimeoutError } from "./promise/models.js";
|
|
63
|
+
import { isFulfilled, isRejected } from "./promise/helpers.js";
|
|
64
|
+
import { error, ok, toPromise, unwrap } from "./result/misc.js";
|
|
65
|
+
import { cancelable, toResult } from "./promise/misc.js";
|
|
66
|
+
import { timed } from "./promise/timed.js";
|
|
67
|
+
import { delay } from "./promise/delay.js";
|
|
68
|
+
import { attemptPromise, promises } from "./promise/index.js";
|
|
64
69
|
import { fromQuery, toQuery } from "./query.js";
|
|
65
70
|
import { QueueError, queue } from "./queue.js";
|
|
66
71
|
import { getRandomBoolean, getRandomCharacters, getRandomColor, getRandomHex, getRandomItem, getRandomItems } from "./random.js";
|
|
67
|
-
import { attempt
|
|
72
|
+
import { attempt } from "./result/index.js";
|
|
68
73
|
import { SizedSet } from "./sized/set.js";
|
|
69
|
-
export { CancelablePromise,
|
|
74
|
+
export { CancelablePromise, PromiseTimeoutError, QueueError, SizedMap, SizedSet, attempt, attemptPromise, average, beacon, between, camelCase, cancelable, capitalize, ceil, chunk, clamp, clone, compact, compare, count, debounce, delay, diff, endsWith, equal, error, exists, filter, find, flatten, floor, flow, toResult as fromPromise, fromQuery, toPromise as fromResult, getArray, getColor, getForegroundColor, getHexColor, getHexaColor, getHslColor, getHslaColor, getNormalizedHex, getNumber, getRandomBoolean, getRandomCharacters, getRandomColor, getRandomFloat, getRandomHex, getRandomInteger, getRandomItem, getRandomItems, getRgbColor, getRgbaColor, getString, getUuid, getValue, groupBy, hexToHsl, hexToHsla, hexToRgb, hexToRgba, hslToHex, hslToRgb, hslToRgba, ignoreKey, includes, indexOf, insert, isArrayOrPlainObject, isColor, isConstructor, isEmpty, isError, isFulfilled, isHexColor, isHslColor, isHslLike, isHslaColor, isInstanceOf, isKey, isNonNullable, isNullable, isNullableOrEmpty, isNullableOrWhitespace, isNumber, isNumerical, isObject, isOk, isPlainObject, isPrimitive, isRejected, isResult, isRgbColor, isRgbLike, isRgbaColor, isTypedArray, join, kebabCase, logger, lowerCase, max, median, memoize, merge, min, noop, ok, omit, parse, partition, pascalCase, pick, pipe, promises, push, queue, range, rgbToHex, rgbToHsl, rgbToHsla, round, select, setValue, shuffle, smush, snakeCase, sort, splice, startsWith, sum, template, throttle, timed, times, titleCase, toMap, toPromise, toQuery, toRecord, toResult, toSet, toggle, trim, truncate, tryDecode, tryEncode, unique, unsmush, unwrap, update, upperCase, words };
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import frame_rate_default from "../frame-rate.js";
|
|
2
1
|
function getLimiter(callback, throttler, time) {
|
|
3
|
-
const interval = typeof time === "number" && time
|
|
2
|
+
const interval = typeof time === "number" && time > 0 ? time : 0;
|
|
4
3
|
function step(now, parameters) {
|
|
5
|
-
if (interval ===
|
|
4
|
+
if (interval === 0 || now - timestamp >= interval) {
|
|
6
5
|
if (throttler) timestamp = now;
|
|
7
6
|
callback(...parameters);
|
|
8
7
|
} else frame = requestAnimationFrame((next) => {
|
|
@@ -14,7 +13,7 @@ function getLimiter(callback, throttler, time) {
|
|
|
14
13
|
const limiter = (...parameters) => {
|
|
15
14
|
limiter.cancel();
|
|
16
15
|
frame = requestAnimationFrame((now) => {
|
|
17
|
-
timestamp ??= now
|
|
16
|
+
timestamp ??= now;
|
|
18
17
|
step(now, parameters);
|
|
19
18
|
});
|
|
20
19
|
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { PROMISE_ABORT_OPTIONS } from "./models.js";
|
|
2
|
+
import { getPromiseOptions } from "./helpers.js";
|
|
3
|
+
import { settlePromise } from "./misc.js";
|
|
4
|
+
function delay(options) {
|
|
5
|
+
const { signal, time } = getPromiseOptions(options);
|
|
6
|
+
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
7
|
+
function abort() {
|
|
8
|
+
cancelAnimationFrame(frame);
|
|
9
|
+
rejector(signal.reason);
|
|
10
|
+
}
|
|
11
|
+
function run(now) {
|
|
12
|
+
start ??= now;
|
|
13
|
+
if (now - start >= time - 5) settlePromise(abort, resolver, void 0, signal);
|
|
14
|
+
else frame = requestAnimationFrame(run);
|
|
15
|
+
}
|
|
16
|
+
signal?.addEventListener("abort", abort, PROMISE_ABORT_OPTIONS);
|
|
17
|
+
let frame;
|
|
18
|
+
let rejector;
|
|
19
|
+
let resolver;
|
|
20
|
+
let start;
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
rejector = reject;
|
|
23
|
+
resolver = resolve;
|
|
24
|
+
if (time === 0) settlePromise(abort, resolve, void 0, signal);
|
|
25
|
+
else frame = requestAnimationFrame(run);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
export { delay };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { PROMISE_STRATEGY_ALL, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED } from "./models.js";
|
|
2
|
+
function getNumberOrDefault(value) {
|
|
3
|
+
return typeof value === "number" && value > 0 ? value : 0;
|
|
4
|
+
}
|
|
5
|
+
function getPromiseOptions(input) {
|
|
6
|
+
if (typeof input === "number") return { time: getNumberOrDefault(input) };
|
|
7
|
+
if (input instanceof AbortSignal) return {
|
|
8
|
+
signal: input,
|
|
9
|
+
time: 0
|
|
10
|
+
};
|
|
11
|
+
const options = typeof input === "object" && input !== null ? input : {};
|
|
12
|
+
return {
|
|
13
|
+
signal: options.signal instanceof AbortSignal ? options.signal : void 0,
|
|
14
|
+
time: getNumberOrDefault(options.time)
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function getPromisesOptions(input) {
|
|
18
|
+
if (typeof input === "string") return { strategy: getStrategyOrDefault(input) };
|
|
19
|
+
if (input instanceof AbortSignal) return {
|
|
20
|
+
signal: input,
|
|
21
|
+
strategy: PROMISE_STRATEGY_DEFAULT
|
|
22
|
+
};
|
|
23
|
+
const options = typeof input === "object" && input !== null ? input : {};
|
|
24
|
+
return {
|
|
25
|
+
signal: options.signal instanceof AbortSignal ? options.signal : void 0,
|
|
26
|
+
strategy: getStrategyOrDefault(options.strategy)
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function getStrategyOrDefault(value) {
|
|
30
|
+
return PROMISE_STRATEGY_ALL.has(value) ? value : PROMISE_STRATEGY_DEFAULT;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Is the value a fulfilled promise result?
|
|
34
|
+
* @param value Value to check
|
|
35
|
+
* @returns `true` if the value is a fulfilled promise result, `false` otherwise
|
|
36
|
+
*/
|
|
37
|
+
function isFulfilled(value) {
|
|
38
|
+
return isType(value, PROMISE_TYPE_FULFILLED);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Is the value a rejected promise result?
|
|
42
|
+
* @param value Value to check
|
|
43
|
+
* @returns `true` if the value is a rejected promise result, `false` otherwise
|
|
44
|
+
*/
|
|
45
|
+
function isRejected(value) {
|
|
46
|
+
return isType(value, PROMISE_TYPE_REJECTED);
|
|
47
|
+
}
|
|
48
|
+
function isType(value, type) {
|
|
49
|
+
return typeof value === "object" && value !== null && value.status === type;
|
|
50
|
+
}
|
|
51
|
+
export { getPromiseOptions, getPromisesOptions, getStrategyOrDefault, isFulfilled, isRejected };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { CancelablePromise, PROMISE_ABORT_OPTIONS, PROMISE_EVENT_NAME, PROMISE_MESSAGE_EXPECTATION_ATTEMPT, PROMISE_MESSAGE_EXPECTATION_PROMISES, PROMISE_STRATEGY_DEFAULT, PROMISE_TYPE_FULFILLED, PROMISE_TYPE_REJECTED, PromiseTimeoutError } from "./models.js";
|
|
2
|
+
import { getPromiseOptions, getPromisesOptions, isFulfilled, isRejected } from "./helpers.js";
|
|
3
|
+
import { toPromise } from "../result/misc.js";
|
|
4
|
+
import { cancelable, handleResult, settlePromise, toResult } from "./misc.js";
|
|
5
|
+
import { getTimedPromise, timed } from "./timed.js";
|
|
6
|
+
import { delay } from "./delay.js";
|
|
7
|
+
async function attemptPromise(value, options) {
|
|
8
|
+
const isFunction = typeof value === "function";
|
|
9
|
+
if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_ATTEMPT));
|
|
10
|
+
const { signal, time } = getPromiseOptions(options);
|
|
11
|
+
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
12
|
+
function abort() {
|
|
13
|
+
rejector(signal.reason);
|
|
14
|
+
}
|
|
15
|
+
async function handler(resolve, reject) {
|
|
16
|
+
try {
|
|
17
|
+
let result = isFunction ? value() : await value;
|
|
18
|
+
if (result instanceof Promise) result = await result;
|
|
19
|
+
settlePromise(abort, resolve, result, signal);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
settlePromise(abort, reject, error, signal);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
let rejector;
|
|
25
|
+
signal?.addEventListener(PROMISE_EVENT_NAME, abort, PROMISE_ABORT_OPTIONS);
|
|
26
|
+
const promise = new Promise((resolve, reject) => {
|
|
27
|
+
rejector = reject;
|
|
28
|
+
handler(resolve, reject);
|
|
29
|
+
});
|
|
30
|
+
return time > 0 ? getTimedPromise(promise, time, signal) : promise;
|
|
31
|
+
}
|
|
32
|
+
async function promises(items, options) {
|
|
33
|
+
const { signal, strategy } = getPromisesOptions(options);
|
|
34
|
+
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
35
|
+
if (!Array.isArray(items)) return Promise.reject(new TypeError(PROMISE_MESSAGE_EXPECTATION_PROMISES));
|
|
36
|
+
const actual = items.filter((item) => item instanceof Promise);
|
|
37
|
+
const { length } = actual;
|
|
38
|
+
if (length === 0) return actual;
|
|
39
|
+
const complete = strategy === PROMISE_STRATEGY_DEFAULT;
|
|
40
|
+
function abort() {
|
|
41
|
+
handlers.reject(signal.reason);
|
|
42
|
+
}
|
|
43
|
+
signal?.addEventListener("abort", abort, PROMISE_ABORT_OPTIONS);
|
|
44
|
+
const data = {
|
|
45
|
+
last: length - 1,
|
|
46
|
+
result: []
|
|
47
|
+
};
|
|
48
|
+
let handlers;
|
|
49
|
+
return new Promise((resolve, reject) => {
|
|
50
|
+
handlers = {
|
|
51
|
+
reject,
|
|
52
|
+
resolve
|
|
53
|
+
};
|
|
54
|
+
for (let index = 0; index < length; index += 1) actual[index].then((value) => handleResult(PROMISE_TYPE_FULFILLED, {
|
|
55
|
+
abort,
|
|
56
|
+
complete,
|
|
57
|
+
data,
|
|
58
|
+
handlers,
|
|
59
|
+
index,
|
|
60
|
+
signal,
|
|
61
|
+
value
|
|
62
|
+
})).catch((reason) => handleResult(PROMISE_TYPE_REJECTED, {
|
|
63
|
+
abort,
|
|
64
|
+
complete,
|
|
65
|
+
data,
|
|
66
|
+
handlers,
|
|
67
|
+
index,
|
|
68
|
+
signal,
|
|
69
|
+
value: reason
|
|
70
|
+
}));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
export { CancelablePromise, PromiseTimeoutError, attemptPromise, cancelable, delay, toPromise as fromResult, isFulfilled, isRejected, promises, timed, toResult };
|