@byloth/core 1.4.0 → 1.5.0-rc.1

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.
@@ -0,0 +1,73 @@
1
+ import type { FulfilledHandler, PromiseExecutor, RejectedHandler } from "../../types.js";
2
+ import { TimeoutException } from "../exceptions/index.js";
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 | E>;
11
+
12
+ public constructor(executor: PromiseExecutor<T, E>, timeout: number)
13
+ {
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): E =>
26
+ {
27
+ this._isPending = false;
28
+ this._isRejected = true;
29
+
30
+ return 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);
70
+ }
71
+
72
+ public get [Symbol.toStringTag]() { return "TimedPromise"; }
73
+ }
@@ -1,4 +1,4 @@
1
- import type { GeneratorFunction, Iteratee, Reducer } from "../types.js";
1
+ import type { GeneratorFunction, Iteratee, TypeGuardIteratee, Reducer } from "../types.js";
2
2
 
3
3
  export default class SmartIterator<T, R = void, N = undefined> implements Iterator<T, R, N>
4
4
  {
@@ -61,6 +61,8 @@ export default class SmartIterator<T, R = void, N = undefined> implements Iterat
61
61
  }
62
62
  }
63
63
 
64
+ public filter(predicate: Iteratee<T, boolean>): SmartIterator<T, R>;
65
+ public filter<S extends T>(predicate: TypeGuardIteratee<T, S>): SmartIterator<T, S>;
64
66
  public filter(predicate: Iteratee<T, boolean>): SmartIterator<T, R>
65
67
  {
66
68
  const iterator = this._iterator;
@@ -1,4 +1,4 @@
1
- import Exception from "./exception.js";
1
+ import { ReferenceException } from "./exceptions/index.js";
2
2
 
3
3
  export default class Subscribers<P extends unknown[] = [], R = void, T extends (...args: P) => R = (...args: P) => R>
4
4
  {
@@ -18,7 +18,7 @@ export default class Subscribers<P extends unknown[] = [], R = void, T extends (
18
18
  const index = this._subscribers.indexOf(subscriber);
19
19
  if (index < 0)
20
20
  {
21
- throw new Exception("Unable to remove the requested subscriber. It was not found.");
21
+ throw new ReferenceException("Unable to remove the requested subscriber. It was not found.");
22
22
  }
23
23
 
24
24
  this._subscribers.splice(index, 1);
package/src/types.ts CHANGED
@@ -4,6 +4,8 @@ export type Constructor<T extends object, P extends unknown[] = any[]> = new (..
4
4
  export type GeneratorFunction<T, R = void, N = undefined> = () => Generator<T, R, N>;
5
5
  export type AsyncGeneratorFunction<T, R = void, N = undefined> = () => AsyncGenerator<T, R, N>;
6
6
  export type Iteratee<T, R = void> = (value: T, index: number) => R;
7
+ export type TypeGuardIteratee<T, R extends T> = (value: T, index: number) => value is R;
8
+
7
9
  export type Reducer<T, A> = (accumulator: A, value: T, index: number) => A;
8
10
 
9
11
  export type MaybePromise<T> = T | PromiseLike<T>;
File without changes