@byloth/core 1.5.0-rc.2 → 1.5.0-rc.3
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/core.js +255 -248
- package/dist/core.js.map +1 -1
- package/dist/core.umd.cjs +2 -2
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +3 -2
- package/src/models/exceptions/index.ts +9 -0
- package/src/models/index.ts +1 -1
- package/src/models/promises/deferred-promise.ts +65 -76
- package/src/models/promises/smart-promise.ts +22 -45
- package/src/models/promises/timed-promise.ts +28 -51
- package/src/utils/math.ts +11 -3
|
@@ -1,73 +1,50 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { MaybePromise, PromiseExecutor } from "../../types.js";
|
|
2
2
|
import { TimeoutException } from "../exceptions/index.js";
|
|
3
3
|
|
|
4
|
-
export default class TimedPromise<T = void
|
|
4
|
+
export default class TimedPromise<T = void> extends Promise<T>
|
|
5
5
|
{
|
|
6
6
|
protected _isPending: boolean;
|
|
7
7
|
protected _isFulfilled: boolean;
|
|
8
8
|
protected _isRejected: boolean;
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
public constructor(executor: PromiseExecutor<T, E>, timeout: number)
|
|
10
|
+
public constructor(executor: PromiseExecutor<T>, timeout?: number)
|
|
13
11
|
{
|
|
14
|
-
|
|
15
|
-
this._isFulfilled = false;
|
|
16
|
-
this._isRejected = false;
|
|
17
|
-
|
|
18
|
-
const _onFulfilled = (result: T): T =>
|
|
12
|
+
super((resolve, reject) =>
|
|
19
13
|
{
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
const _resolve = (result: MaybePromise<T>) =>
|
|
15
|
+
{
|
|
16
|
+
this._isPending = false;
|
|
17
|
+
this._isFulfilled = true;
|
|
22
18
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
19
|
+
clearTimeout(_timeoutId);
|
|
20
|
+
resolve(result);
|
|
21
|
+
};
|
|
22
|
+
const _reject = (reason: unknown) =>
|
|
23
|
+
{
|
|
24
|
+
this._isPending = false;
|
|
25
|
+
this._isRejected = true;
|
|
29
26
|
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
clearTimeout(_timeoutId);
|
|
28
|
+
reject(reason);
|
|
29
|
+
};
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
const _timeoutId = setTimeout(() =>
|
|
32
|
+
{
|
|
33
|
+
_reject(new TimeoutException("The operation has timed out."));
|
|
34
|
+
|
|
35
|
+
}, timeout);
|
|
37
36
|
|
|
38
|
-
|
|
37
|
+
executor(_resolve, _reject);
|
|
38
|
+
});
|
|
39
39
|
|
|
40
|
-
this.
|
|
41
|
-
|
|
40
|
+
this._isPending = true;
|
|
41
|
+
this._isFulfilled = false;
|
|
42
|
+
this._isRejected = false;
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
public get isPending(): boolean { return this._isPending; }
|
|
45
46
|
public get isFulfilled(): boolean { return this._isFulfilled; }
|
|
46
47
|
public get isRejected(): boolean { return this._isRejected; }
|
|
47
48
|
|
|
48
|
-
public then(onFulfilled?: null): Promise<T | E>;
|
|
49
|
-
public then<F = T | E>(onFulfilled: FulfilledHandler<T | E, F>, onRejected?: null): Promise<F>;
|
|
50
|
-
public then<F = T, R = E>(
|
|
51
|
-
onFulfilled: FulfilledHandler<T, F>,
|
|
52
|
-
onRejected: RejectedHandler<E, R>): Promise<F | R>;
|
|
53
|
-
public then<F = T, R = E>(
|
|
54
|
-
onFulfilled?: FulfilledHandler<T, F> | null,
|
|
55
|
-
onRejected?: RejectedHandler<E, R> | null): Promise<F | R>
|
|
56
|
-
{
|
|
57
|
-
return this._promise.then(onFulfilled as FulfilledHandler<T | E, F>, onRejected);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public catch(onRejected?: null): Promise<T | E>;
|
|
61
|
-
public catch<R = E>(onRejected: RejectedHandler<E, R>): Promise<T | R>;
|
|
62
|
-
public catch<R = E>(onRejected?: RejectedHandler<E, R> | null): Promise<T | E | R>
|
|
63
|
-
{
|
|
64
|
-
return this._promise.catch(onRejected);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
public finally(onFinally?: (() => void) | null): Promise<T | E>
|
|
68
|
-
{
|
|
69
|
-
return this._promise.finally(onFinally);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
49
|
public get [Symbol.toStringTag]() { return "TimedPromise"; }
|
|
73
50
|
}
|
package/src/utils/math.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ValueException } from "../models/exceptions/index.js";
|
|
1
2
|
import { zip } from "./iterator.js";
|
|
2
3
|
|
|
3
4
|
export function average<T extends number>(values: Iterable<T>): number;
|
|
@@ -7,26 +8,33 @@ export function average<T extends number>(values: Iterable<T>, weights?: Iterabl
|
|
|
7
8
|
if (weights === undefined)
|
|
8
9
|
{
|
|
9
10
|
let _sum = 0;
|
|
10
|
-
let
|
|
11
|
+
let _index = 0;
|
|
11
12
|
|
|
12
13
|
for (const value of values)
|
|
13
14
|
{
|
|
14
15
|
_sum += value;
|
|
15
|
-
|
|
16
|
+
_index += 1;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
if (_index === 0) { throw new ValueException("You must provide at least one value."); }
|
|
20
|
+
|
|
21
|
+
return _sum / _index;
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
let _sum = 0;
|
|
22
25
|
let _count = 0;
|
|
26
|
+
let _index = 0;
|
|
23
27
|
|
|
24
28
|
for (const [value, weight] of zip(values, weights))
|
|
25
29
|
{
|
|
26
30
|
_sum += value * weight;
|
|
27
31
|
_count += weight;
|
|
32
|
+
_index += 1;
|
|
28
33
|
}
|
|
29
34
|
|
|
35
|
+
if (_index === 0) { throw new ValueException("You must provide at least one value and weight."); }
|
|
36
|
+
if (_count === 0) { throw new ValueException("The sum of weights must be greater than zero."); }
|
|
37
|
+
|
|
30
38
|
return _sum / _count;
|
|
31
39
|
}
|
|
32
40
|
|