@byloth/core 1.5.0-rc.2 → 1.5.0-rc.4

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.
@@ -1,72 +1,30 @@
1
- import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "../../types.js";
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, E = unknown>
5
- {
6
- protected _isPending: boolean;
7
- protected _isFulfilled: boolean;
8
- protected _isRejected: boolean;
9
-
10
- protected _promise: Promise<T>;
4
+ import SmartPromise from "./smart-promise.js";
11
5
 
12
- public constructor(executor: PromiseExecutor<T, E>, timeout: number)
6
+ export default class TimedPromise<T = void> extends SmartPromise<T>
7
+ {
8
+ public constructor(executor: PromiseExecutor<T>, timeout?: number)
13
9
  {
14
- this._isPending = true;
15
- this._isFulfilled = false;
16
- this._isRejected = false;
17
-
18
- const _onFulfilled = (result: T): T =>
19
- {
20
- this._isPending = false;
21
- this._isFulfilled = true;
22
-
23
- return result;
24
- };
25
- const _onRejected = (reason: E): never =>
10
+ super((resolve, reject) =>
26
11
  {
27
- this._isPending = false;
28
- this._isRejected = true;
29
-
30
- throw reason;
31
- };
32
-
33
- const _executor = new Promise<T>(executor);
34
- const _timeout = new Promise<never>((_, reject) => setTimeout(() =>
35
- {
36
- reject(new TimeoutException("The operation has timed out."));
37
-
38
- }, timeout));
39
-
40
- this._promise = Promise.race([_executor, _timeout])
41
- .then(_onFulfilled, _onRejected);
42
- }
43
-
44
- public get isPending(): boolean { return this._isPending; }
45
- public get isFulfilled(): boolean { return this._isFulfilled; }
46
- public get isRejected(): boolean { return this._isRejected; }
47
-
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);
12
+ const _resolve = (result: MaybePromise<T>) =>
13
+ {
14
+ clearTimeout(_timeoutId);
15
+ resolve(result);
16
+ };
17
+ const _reject = (reason: unknown) =>
18
+ {
19
+ clearTimeout(_timeoutId);
20
+ reject(reason);
21
+ };
22
+
23
+ const _timeout = () => _reject(new TimeoutException("The operation has timed out."));
24
+ const _timeoutId = setTimeout(_timeout, timeout);
25
+
26
+ executor(_resolve, _reject);
27
+ });
70
28
  }
71
29
 
72
30
  public get [Symbol.toStringTag]() { return "TimedPromise"; }
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 _count = 0;
11
+ let _index = 0;
11
12
 
12
13
  for (const value of values)
13
14
  {
14
15
  _sum += value;
15
- _count += 1;
16
+ _index += 1;
16
17
  }
17
18
 
18
- return _sum / _count;
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