@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
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
export declare class CancelablePromise<Value = void> extends Promise<Value> {
|
|
2
|
+
#private;
|
|
3
|
+
constructor(executor: (resolve: (value: Value) => void, reject: (reason: unknown) => void) => void);
|
|
4
|
+
/**
|
|
5
|
+
* Cancel the promise, rejecting it with an optional reason
|
|
6
|
+
* @param reason Optional reason for canceling the promise
|
|
7
|
+
*/
|
|
8
|
+
cancel(reason?: unknown): void;
|
|
9
|
+
}
|
|
10
|
+
export type FulfilledPromise<Value> = {
|
|
11
|
+
status: typeof PROMISE_TYPE_FULFILLED;
|
|
12
|
+
value: Value;
|
|
13
|
+
};
|
|
14
|
+
export type PromiseData<Items extends unknown[]> = {
|
|
15
|
+
last: number;
|
|
16
|
+
result: Items | PromisesResult<Items>;
|
|
17
|
+
};
|
|
18
|
+
export type PromiseHandlers<Items extends unknown[]> = {
|
|
19
|
+
resolve: (value: Items | PromisesResult<Items>) => void;
|
|
20
|
+
reject: (reason: unknown) => void;
|
|
21
|
+
};
|
|
22
|
+
export type PromiseOptions = {
|
|
23
|
+
/**
|
|
24
|
+
* AbortSignal for aborting the promise; when aborted, the promise will reject with the reason of the signal
|
|
25
|
+
*/
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
/**
|
|
28
|
+
* How long to wait for (in milliseconds; defaults to `0`)
|
|
29
|
+
*/
|
|
30
|
+
time?: number;
|
|
31
|
+
};
|
|
32
|
+
export type PromiseParameters<Items extends unknown[]> = {
|
|
33
|
+
abort: () => void;
|
|
34
|
+
complete: boolean;
|
|
35
|
+
data: PromiseData<Items>;
|
|
36
|
+
handlers: PromiseHandlers<Items>;
|
|
37
|
+
index: number;
|
|
38
|
+
signal?: AbortSignal;
|
|
39
|
+
value?: unknown;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Promise handling strategy
|
|
43
|
+
*
|
|
44
|
+
* - `complete`: wait for all promises to settle, then return the results
|
|
45
|
+
* - Returns an array of fulfilled and/or rejected results
|
|
46
|
+
* - `first`: rejects on the first rejected promise
|
|
47
|
+
* - Returns an array of values
|
|
48
|
+
*/
|
|
49
|
+
export type PromiseStrategy = 'complete' | 'first';
|
|
50
|
+
export declare class PromiseTimeoutError extends Error {
|
|
51
|
+
constructor();
|
|
52
|
+
}
|
|
53
|
+
export type Promises<Items extends unknown[]> = {
|
|
54
|
+
[K in keyof Items]: Promise<Items[K]>;
|
|
55
|
+
};
|
|
56
|
+
export type PromisesOptions = {
|
|
57
|
+
signal?: AbortSignal;
|
|
58
|
+
strategy?: PromiseStrategy;
|
|
59
|
+
};
|
|
60
|
+
export type PromisesResult<Items extends unknown[]> = {
|
|
61
|
+
[K in keyof Items]: Items[K] extends Promise<infer Value> ? PromisesResultItem<Value> : never;
|
|
62
|
+
};
|
|
63
|
+
export type PromisesResultItem<Value> = FulfilledPromise<Value> | RejectedPromise;
|
|
64
|
+
export type RejectedPromise = {
|
|
65
|
+
status: typeof PROMISE_TYPE_REJECTED;
|
|
66
|
+
reason: unknown;
|
|
67
|
+
};
|
|
68
|
+
export declare const PROMISE_ABORT_OPTIONS: {
|
|
69
|
+
once: boolean;
|
|
70
|
+
};
|
|
71
|
+
export declare const PROMISE_ERROR_NAME = "PromiseTimeoutError";
|
|
72
|
+
export declare const PROMISE_EVENT_NAME = "abort";
|
|
73
|
+
export declare const PROMISE_MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
|
|
74
|
+
export declare const PROMISE_MESSAGE_EXPECTATION_PROMISES = "Promises expected an array of promises";
|
|
75
|
+
export declare const PROMISE_MESSAGE_EXPECTATION_RESULT = "toResult expected a Promise";
|
|
76
|
+
export declare const PROMISE_MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
|
|
77
|
+
export declare const PROMISE_MESSAGE_TIMEOUT = "Promise timed out";
|
|
78
|
+
export declare const PROMISE_STRATEGY_ALL: Set<PromiseStrategy>;
|
|
79
|
+
export declare const PROMISE_STRATEGY_DEFAULT: PromiseStrategy;
|
|
80
|
+
export declare const PROMISE_TYPE_FULFILLED = "fulfilled";
|
|
81
|
+
export declare const PROMISE_TYPE_REJECTED = "rejected";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { RequiredKeys } from '../models';
|
|
2
|
+
import { type PromiseOptions } from './models';
|
|
3
|
+
export declare function getTimedPromise<Value>(promise: Promise<Value>, time: number, signal?: AbortSignal): Promise<Value>;
|
|
4
|
+
/**
|
|
5
|
+
* Create a promise that should be settled within a certain amount of time
|
|
6
|
+
* @param promise Promise to settle
|
|
7
|
+
* @param options Timed options
|
|
8
|
+
* @returns Timed promise
|
|
9
|
+
*/
|
|
10
|
+
export declare function timed<Value>(promise: Promise<Value>, options: RequiredKeys<PromiseOptions, 'time'>): Promise<Value>;
|
|
11
|
+
/**
|
|
12
|
+
* Create a promise that should be settled within a certain amount of time
|
|
13
|
+
* @param promise Promise to settle
|
|
14
|
+
* @param time How long to wait for _(in milliseconds; defaults to `0`)_
|
|
15
|
+
* @returns Timed promise
|
|
16
|
+
*/
|
|
17
|
+
export declare function timed<Value>(promise: Promise<Value>, time: number): Promise<Value>;
|
package/types/result/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { attemptPromise } from '../promise';
|
|
2
2
|
import { matchResult } from './match';
|
|
3
|
-
import type {
|
|
3
|
+
import type { ExtendedResult, Result } from './models';
|
|
4
4
|
import { attemptFlow } from './work/flow';
|
|
5
5
|
import { attemptPipe } from './work/pipe';
|
|
6
6
|
/**
|
|
@@ -49,38 +49,7 @@ export declare namespace attempt {
|
|
|
49
49
|
var pipe: typeof attemptPipe;
|
|
50
50
|
var promise: typeof attemptPromise;
|
|
51
51
|
}
|
|
52
|
-
/**
|
|
53
|
-
* Creates an extended error result
|
|
54
|
-
* @param error Error value
|
|
55
|
-
* @param original Original error
|
|
56
|
-
* @returns Error result
|
|
57
|
-
*/
|
|
58
|
-
export declare function error<E>(value: E, original: Error): ExtendedErr<E>;
|
|
59
|
-
/**
|
|
60
|
-
* Creates an error result
|
|
61
|
-
* @param error Error value
|
|
62
|
-
* @returns Error result
|
|
63
|
-
*/
|
|
64
|
-
export declare function error<E>(value: E): Err<E>;
|
|
65
|
-
/**
|
|
66
|
-
* Creates an ok result
|
|
67
|
-
* @param value Value
|
|
68
|
-
* @returns Ok result
|
|
69
|
-
*/
|
|
70
|
-
export declare function ok<Value>(value: Value): Ok<Value>;
|
|
71
|
-
/**
|
|
72
|
-
* Gets the value of an ok result _(or a default value)_
|
|
73
|
-
* @param value Result to unwrap
|
|
74
|
-
* @param defaultValue Default value
|
|
75
|
-
* @returns Value of the result _(or the default value)_
|
|
76
|
-
*/
|
|
77
|
-
export declare function unwrap<Value, E = Error>(value: Result<Value, E>, defaultValue: Value): Value;
|
|
78
|
-
/**
|
|
79
|
-
* Gets the value of an ok result _(or a default value)_
|
|
80
|
-
* @param value Result to unwrap
|
|
81
|
-
* @param defaultValue Default value
|
|
82
|
-
* @returns Value of the result _(or the default value)_
|
|
83
|
-
*/
|
|
84
|
-
export declare function unwrap(value: unknown, defaultValue: unknown): unknown;
|
|
85
52
|
export { isError, isOk, isResult } from '../internal/result';
|
|
53
|
+
export { toResult as fromPromise } from '../promise/misc';
|
|
54
|
+
export { error, ok, toPromise, unwrap } from './misc';
|
|
86
55
|
export type { Err, ExtendedErr, ExtendedResult, Ok, Result } from './models';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { AnyResult, Err, ExtendedErr, Ok, Result } from './models';
|
|
2
|
+
/**
|
|
3
|
+
* Creates an extended error result
|
|
4
|
+
* @param error Error value
|
|
5
|
+
* @param original Original error
|
|
6
|
+
* @returns Error result
|
|
7
|
+
*/
|
|
8
|
+
export declare function error<E>(value: E, original: Error): ExtendedErr<E>;
|
|
9
|
+
/**
|
|
10
|
+
* Creates an error result
|
|
11
|
+
* @param error Error value
|
|
12
|
+
* @returns Error result
|
|
13
|
+
*/
|
|
14
|
+
export declare function error<E>(value: E): Err<E>;
|
|
15
|
+
export declare function getError<E>(value: E, original?: Error): Err<E> | ExtendedErr<E>;
|
|
16
|
+
/**
|
|
17
|
+
* Creates an ok result
|
|
18
|
+
* @param value Value
|
|
19
|
+
* @returns Ok result
|
|
20
|
+
*/
|
|
21
|
+
export declare function ok<Value>(value: Value): Ok<Value>;
|
|
22
|
+
/**
|
|
23
|
+
* Converts a result to a promise
|
|
24
|
+
*
|
|
25
|
+
* Resolves if ok, rejects for error
|
|
26
|
+
* @param result Result to convert
|
|
27
|
+
* @returns Promised result
|
|
28
|
+
*/
|
|
29
|
+
export declare function toPromise<Value, E = Error>(callback: () => AnyResult<Value, E>): Promise<Value>;
|
|
30
|
+
/**
|
|
31
|
+
* Converts a result to a promise
|
|
32
|
+
*
|
|
33
|
+
* Resolves if ok, rejects for error
|
|
34
|
+
* @param result Result to convert
|
|
35
|
+
* @returns Promised result
|
|
36
|
+
*/
|
|
37
|
+
export declare function toPromise<Value, E = Error>(result: AnyResult<Value, E>): Promise<Value>;
|
|
38
|
+
/**
|
|
39
|
+
* Gets the value of an ok result _(or a default value)_
|
|
40
|
+
* @param value Result to unwrap
|
|
41
|
+
* @param defaultValue Default value
|
|
42
|
+
* @returns Value of the result _(or the default value)_
|
|
43
|
+
*/
|
|
44
|
+
export declare function unwrap<Value, E = Error>(value: Result<Value, E>, defaultValue: Value): Value;
|
|
45
|
+
/**
|
|
46
|
+
* Gets the value of an ok result _(or a default value)_
|
|
47
|
+
* @param value Result to unwrap
|
|
48
|
+
* @param defaultValue Default value
|
|
49
|
+
* @returns Value of the result _(or the default value)_
|
|
50
|
+
*/
|
|
51
|
+
export declare function unwrap(value: unknown, defaultValue: unknown): unknown;
|
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
var TOTAL = 10;
|
|
15
|
-
var TRIM_PART = 2;
|
|
16
|
-
var TRIM_TOTAL = 4;
|
|
17
|
-
var 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
|
-
export { frame_rate_default as default };
|
package/dist/promise.js
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
var CancelablePromise = class extends Promise {
|
|
2
|
-
#rejector;
|
|
3
|
-
constructor(executor) {
|
|
4
|
-
let rejector;
|
|
5
|
-
super((resolve, reject) => {
|
|
6
|
-
rejector = reject;
|
|
7
|
-
executor(resolve, reject);
|
|
8
|
-
});
|
|
9
|
-
this.#rejector = rejector;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Cancel the promise, rejecting it with an optional reason
|
|
13
|
-
* @param reason Optional reason for canceling the promise
|
|
14
|
-
*/
|
|
15
|
-
cancel(reason) {
|
|
16
|
-
this.#rejector(reason);
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
var PromiseTimeoutError = class extends Error {
|
|
20
|
-
constructor() {
|
|
21
|
-
super(MESSAGE_TIMEOUT);
|
|
22
|
-
this.name = ERROR_NAME;
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
async function attemptPromise(value, options) {
|
|
26
|
-
const isFunction = typeof value === "function";
|
|
27
|
-
if (!isFunction && !(value instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_ATTEMPT));
|
|
28
|
-
const { signal, time } = getPromiseOptions(options);
|
|
29
|
-
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
30
|
-
function abort() {
|
|
31
|
-
rejector(signal.reason);
|
|
32
|
-
}
|
|
33
|
-
async function handler(resolve, reject) {
|
|
34
|
-
try {
|
|
35
|
-
let result = isFunction ? value() : await value;
|
|
36
|
-
if (result instanceof Promise) result = await result;
|
|
37
|
-
settlePromise(abort, resolve, result, signal);
|
|
38
|
-
} catch (error) {
|
|
39
|
-
settlePromise(abort, reject, error, signal);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
let rejector;
|
|
43
|
-
signal?.addEventListener(EVENT_NAME, abort, ABORT_OPTIONS);
|
|
44
|
-
const promise = new Promise((resolve, reject) => {
|
|
45
|
-
rejector = reject;
|
|
46
|
-
handler(resolve, reject);
|
|
47
|
-
});
|
|
48
|
-
return time > 0 ? getTimed(promise, time, signal) : promise;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Create a cancelable promise
|
|
52
|
-
* @param executor Executor function for the promise
|
|
53
|
-
* @returns Cancelable promise
|
|
54
|
-
*/
|
|
55
|
-
function cancelable(executor) {
|
|
56
|
-
return new CancelablePromise(executor);
|
|
57
|
-
}
|
|
58
|
-
function delay(options) {
|
|
59
|
-
const { signal, time } = getPromiseOptions(options);
|
|
60
|
-
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
61
|
-
function abort() {
|
|
62
|
-
clearTimeout(timeout);
|
|
63
|
-
rejector(signal.reason);
|
|
64
|
-
}
|
|
65
|
-
signal?.addEventListener("abort", abort, ABORT_OPTIONS);
|
|
66
|
-
let rejector;
|
|
67
|
-
let timeout;
|
|
68
|
-
return new Promise((resolve, reject) => {
|
|
69
|
-
rejector = reject;
|
|
70
|
-
timeout = setTimeout(() => {
|
|
71
|
-
settlePromise(abort, resolve, void 0, signal);
|
|
72
|
-
}, time);
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
function getNumberOrDefault(value) {
|
|
76
|
-
return typeof value === "number" && value > 0 ? value : 0;
|
|
77
|
-
}
|
|
78
|
-
function getPromiseOptions(input) {
|
|
79
|
-
if (typeof input === "number") return { time: getNumberOrDefault(input) };
|
|
80
|
-
if (input instanceof AbortSignal) return {
|
|
81
|
-
signal: input,
|
|
82
|
-
time: 0
|
|
83
|
-
};
|
|
84
|
-
const options = typeof input === "object" && input !== null ? input : {};
|
|
85
|
-
return {
|
|
86
|
-
signal: options.signal instanceof AbortSignal ? options.signal : void 0,
|
|
87
|
-
time: getNumberOrDefault(options.time)
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
function getPromisesOptions(input) {
|
|
91
|
-
if (typeof input === "string") return { strategy: getStrategyOrDefault(input) };
|
|
92
|
-
if (input instanceof AbortSignal) return {
|
|
93
|
-
signal: input,
|
|
94
|
-
strategy: DEFAULT_STRATEGY
|
|
95
|
-
};
|
|
96
|
-
const options = typeof input === "object" && input !== null ? input : {};
|
|
97
|
-
return {
|
|
98
|
-
signal: options.signal instanceof AbortSignal ? options.signal : void 0,
|
|
99
|
-
strategy: getStrategyOrDefault(options.strategy)
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
function getStrategyOrDefault(value) {
|
|
103
|
-
return strategies.has(value) ? value : DEFAULT_STRATEGY;
|
|
104
|
-
}
|
|
105
|
-
async function getTimed(promise, time, signal) {
|
|
106
|
-
function abort() {
|
|
107
|
-
clearTimeout(timeout);
|
|
108
|
-
rejector(signal.reason);
|
|
109
|
-
}
|
|
110
|
-
signal?.addEventListener(EVENT_NAME, abort, ABORT_OPTIONS);
|
|
111
|
-
let rejector;
|
|
112
|
-
let timeout;
|
|
113
|
-
return Promise.race([promise, new Promise((_, reject) => {
|
|
114
|
-
rejector = reject;
|
|
115
|
-
timeout = setTimeout(() => {
|
|
116
|
-
settlePromise(abort, reject, new PromiseTimeoutError(), signal);
|
|
117
|
-
}, time);
|
|
118
|
-
})]).then((value) => {
|
|
119
|
-
clearTimeout(timeout);
|
|
120
|
-
signal?.removeEventListener(EVENT_NAME, abort);
|
|
121
|
-
return value;
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
function handleResult(status, parameters) {
|
|
125
|
-
const { abort, complete, data, handlers, index, signal, value } = parameters;
|
|
126
|
-
if (signal?.aborted ?? false) return;
|
|
127
|
-
if (!complete && status === TYPE_REJECTED) {
|
|
128
|
-
settlePromise(abort, handlers.reject, value, signal);
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
data.result[index] = !complete ? value : status === TYPE_FULFILLED ? {
|
|
132
|
-
status,
|
|
133
|
-
value
|
|
134
|
-
} : {
|
|
135
|
-
status,
|
|
136
|
-
reason: value
|
|
137
|
-
};
|
|
138
|
-
if (index === data.last) settlePromise(abort, handlers.resolve, data.result, signal);
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Is the value a fulfilled promise result?
|
|
142
|
-
* @param value Value to check
|
|
143
|
-
* @returns `true` if the value is a fulfilled promise result, `false` otherwise
|
|
144
|
-
*/
|
|
145
|
-
function isFulfilled(value) {
|
|
146
|
-
return isType(value, TYPE_FULFILLED);
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Is the value a rejected promise result?
|
|
150
|
-
* @param value Value to check
|
|
151
|
-
* @returns `true` if the value is a rejected promise result, `false` otherwise
|
|
152
|
-
*/
|
|
153
|
-
function isRejected(value) {
|
|
154
|
-
return isType(value, TYPE_REJECTED);
|
|
155
|
-
}
|
|
156
|
-
function isType(value, type) {
|
|
157
|
-
return typeof value === "object" && value !== null && value.status === type;
|
|
158
|
-
}
|
|
159
|
-
async function promises(items, options) {
|
|
160
|
-
const { signal, strategy } = getPromisesOptions(options);
|
|
161
|
-
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
162
|
-
if (!Array.isArray(items)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_PROMISES));
|
|
163
|
-
const actual = items.filter((item) => item instanceof Promise);
|
|
164
|
-
const { length } = actual;
|
|
165
|
-
if (length === 0) return actual;
|
|
166
|
-
const complete = strategy === DEFAULT_STRATEGY;
|
|
167
|
-
function abort() {
|
|
168
|
-
handlers.reject(signal.reason);
|
|
169
|
-
}
|
|
170
|
-
signal?.addEventListener("abort", abort, ABORT_OPTIONS);
|
|
171
|
-
const data = {
|
|
172
|
-
last: length - 1,
|
|
173
|
-
result: []
|
|
174
|
-
};
|
|
175
|
-
let handlers;
|
|
176
|
-
return new Promise((resolve, reject) => {
|
|
177
|
-
handlers = {
|
|
178
|
-
reject,
|
|
179
|
-
resolve
|
|
180
|
-
};
|
|
181
|
-
for (let index = 0; index < length; index += 1) actual[index].then((value) => handleResult(TYPE_FULFILLED, {
|
|
182
|
-
abort,
|
|
183
|
-
complete,
|
|
184
|
-
data,
|
|
185
|
-
handlers,
|
|
186
|
-
index,
|
|
187
|
-
signal,
|
|
188
|
-
value
|
|
189
|
-
})).catch((reason) => handleResult(TYPE_REJECTED, {
|
|
190
|
-
abort,
|
|
191
|
-
complete,
|
|
192
|
-
data,
|
|
193
|
-
handlers,
|
|
194
|
-
index,
|
|
195
|
-
signal,
|
|
196
|
-
value: reason
|
|
197
|
-
}));
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
function settlePromise(aborter, settler, value, signal) {
|
|
201
|
-
signal?.removeEventListener(EVENT_NAME, aborter);
|
|
202
|
-
settler(value);
|
|
203
|
-
}
|
|
204
|
-
async function timed(promise, options) {
|
|
205
|
-
if (!(promise instanceof Promise)) return Promise.reject(new TypeError(MESSAGE_EXPECTATION_TIMED));
|
|
206
|
-
const { signal, time } = getPromiseOptions(options);
|
|
207
|
-
if (signal?.aborted ?? false) return Promise.reject(signal.reason);
|
|
208
|
-
return time > 0 ? getTimed(promise, time, signal) : promise;
|
|
209
|
-
}
|
|
210
|
-
var ABORT_OPTIONS = { once: true };
|
|
211
|
-
var DEFAULT_STRATEGY = "complete";
|
|
212
|
-
var ERROR_NAME = "PromiseTimeoutError";
|
|
213
|
-
var EVENT_NAME = "abort";
|
|
214
|
-
var MESSAGE_EXPECTATION_ATTEMPT = "Attempt expected a function or a promise";
|
|
215
|
-
var MESSAGE_EXPECTATION_PROMISES = "Promises expected an array of promises";
|
|
216
|
-
var MESSAGE_EXPECTATION_TIMED = "Timed function expected a Promise";
|
|
217
|
-
var MESSAGE_TIMEOUT = "Promise timed out";
|
|
218
|
-
var strategies = new Set(["complete", "first"]);
|
|
219
|
-
var TYPE_FULFILLED = "fulfilled";
|
|
220
|
-
var TYPE_REJECTED = "rejected";
|
|
221
|
-
export { CancelablePromise, PromiseTimeoutError, attemptPromise, cancelable, delay, isFulfilled, isRejected, promises, timed };
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
// #region Functions
|
|
2
|
-
|
|
3
|
-
function calculate(): Promise<number> {
|
|
4
|
-
return new Promise(resolve => {
|
|
5
|
-
const values: number[] = [];
|
|
6
|
-
|
|
7
|
-
let last: DOMHighResTimeStamp;
|
|
8
|
-
|
|
9
|
-
function step(now: DOMHighResTimeStamp): void {
|
|
10
|
-
if (last != null) {
|
|
11
|
-
values.push(now - last);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
last = now;
|
|
15
|
-
|
|
16
|
-
if (values.length >= TOTAL) {
|
|
17
|
-
const median =
|
|
18
|
-
values
|
|
19
|
-
.sort()
|
|
20
|
-
.slice(TRIM_PART, -TRIM_PART)
|
|
21
|
-
.reduce((first, second) => first + second, 0) /
|
|
22
|
-
(values.length - TRIM_TOTAL);
|
|
23
|
-
|
|
24
|
-
resolve(median);
|
|
25
|
-
} else {
|
|
26
|
-
requestAnimationFrame(step);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
requestAnimationFrame(step);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
// #endregion
|
|
35
|
-
|
|
36
|
-
// #region Variables
|
|
37
|
-
|
|
38
|
-
const TOTAL = 10;
|
|
39
|
-
|
|
40
|
-
const TRIM_PART = 2;
|
|
41
|
-
|
|
42
|
-
const TRIM_TOTAL = 4;
|
|
43
|
-
|
|
44
|
-
let FRAME_RATE_MS = 1000 / 60;
|
|
45
|
-
|
|
46
|
-
// #endregion
|
|
47
|
-
|
|
48
|
-
// #region Initialization
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* A calculated average of the refresh rate of the display _(in milliseconds)_
|
|
52
|
-
*/
|
|
53
|
-
calculate().then(value => {
|
|
54
|
-
FRAME_RATE_MS = value;
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
// #endregion
|
|
58
|
-
|
|
59
|
-
// #region Exports
|
|
60
|
-
|
|
61
|
-
export default FRAME_RATE_MS;
|
|
62
|
-
|
|
63
|
-
// #endregion
|