@byloth/core 2.0.0-rc.1 → 2.0.0-rc.10
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 +1379 -894
- package/dist/core.js.map +1 -1
- package/dist/core.umd.cjs +3 -3
- package/dist/core.umd.cjs.map +1 -1
- package/package.json +8 -10
- package/src/helpers.ts +7 -0
- package/src/index.ts +19 -14
- package/src/models/aggregators/aggregated-async-iterator.ts +129 -81
- package/src/models/aggregators/aggregated-iterator.ts +128 -82
- package/src/models/aggregators/index.ts +1 -3
- package/src/models/aggregators/reduced-iterator.ts +118 -30
- package/src/models/aggregators/types.ts +15 -10
- package/src/models/callbacks/callable-object.ts +23 -0
- package/src/models/callbacks/index.ts +5 -0
- package/src/models/callbacks/publisher.ts +45 -0
- package/src/models/callbacks/switchable-callback.ts +108 -0
- package/src/models/callbacks/types.ts +1 -0
- package/src/models/exceptions/core.ts +3 -3
- package/src/models/exceptions/index.ts +13 -13
- package/src/models/game-loop.ts +9 -9
- package/src/models/index.ts +3 -6
- package/src/models/iterators/smart-async-iterator.ts +109 -23
- package/src/models/iterators/smart-iterator.ts +105 -12
- package/src/models/iterators/types.ts +17 -7
- package/src/models/json/json-storage.ts +2 -3
- package/src/models/json/types.ts +3 -1
- package/src/models/promises/deferred-promise.ts +1 -1
- package/src/models/promises/index.ts +3 -1
- package/src/models/promises/long-running-task.ts +294 -0
- package/src/models/promises/smart-promise.ts +6 -1
- package/src/models/promises/thenable.ts +97 -0
- package/src/models/promises/timed-promise.ts +1 -1
- package/src/models/promises/types.ts +2 -0
- package/src/models/timers/clock.ts +29 -7
- package/src/models/timers/countdown.ts +56 -20
- package/src/models/types.ts +12 -10
- package/src/utils/async.ts +9 -4
- package/src/utils/date.ts +3 -0
- package/src/utils/index.ts +1 -1
- package/src/utils/random.ts +4 -3
- package/src/models/aggregators/aggregator.ts +0 -46
- package/src/models/aggregators/async-aggregator.ts +0 -56
- package/src/models/publisher.ts +0 -39
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { KeyException, NotImplementedException, RuntimeException } from "../exceptions/index.js";
|
|
2
|
+
|
|
3
|
+
import CallableObject from "./callable-object.js";
|
|
4
|
+
import type { Callback } from "./types.js";
|
|
5
|
+
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
export default class SwitchableCallback<T extends Callback<any[], any> = Callback> extends CallableObject<T>
|
|
8
|
+
{
|
|
9
|
+
protected _callback: T;
|
|
10
|
+
protected _callbacks: Map<string, T>;
|
|
11
|
+
|
|
12
|
+
protected _isEnabled: boolean;
|
|
13
|
+
public get isEnabled(): boolean { return this._isEnabled; }
|
|
14
|
+
|
|
15
|
+
protected _key: string;
|
|
16
|
+
public get key(): string { return this._key; }
|
|
17
|
+
|
|
18
|
+
public readonly invoke: (...args: Parameters<T>) => ReturnType<T>;
|
|
19
|
+
|
|
20
|
+
public constructor()
|
|
21
|
+
{
|
|
22
|
+
const _default = () =>
|
|
23
|
+
{
|
|
24
|
+
throw new NotImplementedException(
|
|
25
|
+
"The `SwitchableCallback` has no callback defined yet. " +
|
|
26
|
+
"Did you forget to call the `register` method?"
|
|
27
|
+
);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
super();
|
|
31
|
+
|
|
32
|
+
this._callback = ((_default) as unknown) as T;
|
|
33
|
+
this._callbacks = new Map<string, T>();
|
|
34
|
+
|
|
35
|
+
this._isEnabled = true;
|
|
36
|
+
this._key = "";
|
|
37
|
+
|
|
38
|
+
this.invoke = (...args: Parameters<T>): ReturnType<T> => this._callback(...args);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public enable(): void
|
|
42
|
+
{
|
|
43
|
+
if (!(this._key))
|
|
44
|
+
{
|
|
45
|
+
throw new KeyException(
|
|
46
|
+
"The `SwitchableCallback` has no callback defined yet. " +
|
|
47
|
+
"Did you forget to call the `register` method?"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
if (this._isEnabled)
|
|
51
|
+
{
|
|
52
|
+
throw new RuntimeException("The `SwitchableCallback` is already enabled.");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this._callback = this._callbacks.get(this._key)!;
|
|
56
|
+
this._isEnabled = true;
|
|
57
|
+
}
|
|
58
|
+
public disable(): void
|
|
59
|
+
{
|
|
60
|
+
if (!(this._isEnabled))
|
|
61
|
+
{
|
|
62
|
+
throw new RuntimeException("The `SwitchableCallback` is already disabled.");
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
66
|
+
this._callback = (() => { }) as T;
|
|
67
|
+
this._isEnabled = false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
public register(key: string, callback: T): void
|
|
71
|
+
{
|
|
72
|
+
if (this._callbacks.size === 0)
|
|
73
|
+
{
|
|
74
|
+
this._key = key;
|
|
75
|
+
this._callback = callback;
|
|
76
|
+
}
|
|
77
|
+
else if (this._callbacks.has(key))
|
|
78
|
+
{
|
|
79
|
+
throw new KeyException(`The key '${key}' has already been used for another callback.`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this._callbacks.set(key, callback);
|
|
83
|
+
}
|
|
84
|
+
public unregister(key: string): void
|
|
85
|
+
{
|
|
86
|
+
if (!(this._callbacks.has(key)))
|
|
87
|
+
{
|
|
88
|
+
throw new KeyException(`The key '${key}' doesn't yet have any associated callback.`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
this._callbacks.delete(key);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
public switch(key: string): void
|
|
95
|
+
{
|
|
96
|
+
if (!(this._callbacks.has(key)))
|
|
97
|
+
{
|
|
98
|
+
throw new KeyException(`The key '${key}' doesn't yet have any associated callback.`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this._key = key;
|
|
102
|
+
|
|
103
|
+
if (this._isEnabled)
|
|
104
|
+
{
|
|
105
|
+
this._callback = this._callbacks.get(key)!;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type Callback<A extends unknown[] = [], R = void> = (...args: A) => R;
|
|
@@ -39,7 +39,7 @@ export default class Exception extends Error
|
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
public
|
|
42
|
+
public readonly [Symbol.toStringTag]: string = "Exception";
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
export class FatalErrorException extends Exception
|
|
@@ -55,7 +55,7 @@ export class FatalErrorException extends Exception
|
|
|
55
55
|
super(message, cause, name);
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
public
|
|
58
|
+
public readonly [Symbol.toStringTag]: string = "FatalErrorException";
|
|
59
59
|
}
|
|
60
60
|
export class NotImplementedException extends FatalErrorException
|
|
61
61
|
{
|
|
@@ -69,5 +69,5 @@ export class NotImplementedException extends FatalErrorException
|
|
|
69
69
|
super(message, cause, name);
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
public
|
|
72
|
+
public readonly [Symbol.toStringTag]: string = "NotImplementedException";
|
|
73
73
|
}
|
|
@@ -7,7 +7,7 @@ export class FileException extends Exception
|
|
|
7
7
|
super(message, cause, name);
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
-
public
|
|
10
|
+
public readonly [Symbol.toStringTag]: string = "FileException";
|
|
11
11
|
}
|
|
12
12
|
export class FileExistsException extends FileException
|
|
13
13
|
{
|
|
@@ -16,7 +16,7 @@ export class FileExistsException extends FileException
|
|
|
16
16
|
super(message, cause, name);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
public
|
|
19
|
+
public readonly [Symbol.toStringTag]: string = "FileExistsException";
|
|
20
20
|
}
|
|
21
21
|
export class FileNotFoundException extends FileException
|
|
22
22
|
{
|
|
@@ -25,7 +25,7 @@ export class FileNotFoundException extends FileException
|
|
|
25
25
|
super(message, cause, name);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
public
|
|
28
|
+
public readonly [Symbol.toStringTag]: string = "FileNotFoundException";
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
export class KeyException extends Exception
|
|
@@ -35,7 +35,7 @@ export class KeyException extends Exception
|
|
|
35
35
|
super(message, cause, name);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
public
|
|
38
|
+
public readonly [Symbol.toStringTag]: string = "KeyException";
|
|
39
39
|
}
|
|
40
40
|
export class NetworkException extends Exception
|
|
41
41
|
{
|
|
@@ -44,7 +44,7 @@ export class NetworkException extends Exception
|
|
|
44
44
|
super(message, cause, name);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
public
|
|
47
|
+
public readonly [Symbol.toStringTag]: string = "NetworkException";
|
|
48
48
|
}
|
|
49
49
|
export class PermissionException extends Exception
|
|
50
50
|
{
|
|
@@ -53,7 +53,7 @@ export class PermissionException extends Exception
|
|
|
53
53
|
super(message, cause, name);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
public
|
|
56
|
+
public readonly [Symbol.toStringTag]: string = "PermissionException";
|
|
57
57
|
}
|
|
58
58
|
export class ReferenceException extends Exception
|
|
59
59
|
{
|
|
@@ -62,7 +62,7 @@ export class ReferenceException extends Exception
|
|
|
62
62
|
super(message, cause, name);
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
public
|
|
65
|
+
public readonly [Symbol.toStringTag]: string = "ReferenceException";
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
export class RuntimeException extends Exception
|
|
@@ -72,7 +72,7 @@ export class RuntimeException extends Exception
|
|
|
72
72
|
super(message, cause, name);
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
public
|
|
75
|
+
public readonly [Symbol.toStringTag]: string = "RuntimeException";
|
|
76
76
|
}
|
|
77
77
|
export class EnvironmentException extends RuntimeException
|
|
78
78
|
{
|
|
@@ -81,7 +81,7 @@ export class EnvironmentException extends RuntimeException
|
|
|
81
81
|
super(message, cause, name);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
|
-
public
|
|
84
|
+
public readonly [Symbol.toStringTag]: string = "EnvironmentException";
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
export class TimeoutException extends Exception
|
|
@@ -91,7 +91,7 @@ export class TimeoutException extends Exception
|
|
|
91
91
|
super(message, cause, name);
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
public
|
|
94
|
+
public readonly [Symbol.toStringTag]: string = "TimeoutException";
|
|
95
95
|
}
|
|
96
96
|
export class TypeException extends Exception
|
|
97
97
|
{
|
|
@@ -100,7 +100,7 @@ export class TypeException extends Exception
|
|
|
100
100
|
super(message, cause, name);
|
|
101
101
|
}
|
|
102
102
|
|
|
103
|
-
public
|
|
103
|
+
public readonly [Symbol.toStringTag]: string = "TypeException";
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
export class ValueException extends Exception
|
|
@@ -110,7 +110,7 @@ export class ValueException extends Exception
|
|
|
110
110
|
super(message, cause, name);
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
public
|
|
113
|
+
public readonly [Symbol.toStringTag]: string = "ValueException";
|
|
114
114
|
}
|
|
115
115
|
export class RangeException extends ValueException
|
|
116
116
|
{
|
|
@@ -119,7 +119,7 @@ export class RangeException extends ValueException
|
|
|
119
119
|
super(message, cause, name);
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
public
|
|
122
|
+
public readonly [Symbol.toStringTag]: string = "RangeException";
|
|
123
123
|
}
|
|
124
124
|
|
|
125
125
|
export { Exception };
|
package/src/models/game-loop.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import type { Interval } from "../core/types.js";
|
|
1
2
|
import { isBrowser } from "../helpers.js";
|
|
2
|
-
import { TimeUnit } from "../utils/date.js";
|
|
3
3
|
|
|
4
4
|
import { FatalErrorException, RuntimeException } from "./exceptions/index.js";
|
|
5
5
|
|
|
6
6
|
export default class GameLoop
|
|
7
7
|
{
|
|
8
|
-
protected _handle?: number;
|
|
8
|
+
protected _handle?: number | Interval;
|
|
9
9
|
|
|
10
10
|
protected _startTime: number;
|
|
11
11
|
public get startTime(): number
|
|
@@ -27,7 +27,7 @@ export default class GameLoop
|
|
|
27
27
|
protected _start: () => void;
|
|
28
28
|
protected _stop: () => void;
|
|
29
29
|
|
|
30
|
-
public constructor(callback: FrameRequestCallback,
|
|
30
|
+
public constructor(callback: FrameRequestCallback, msIfNotBrowser = 40)
|
|
31
31
|
{
|
|
32
32
|
this._startTime = 0;
|
|
33
33
|
this._isRunning = false;
|
|
@@ -41,24 +41,22 @@ export default class GameLoop
|
|
|
41
41
|
this._handle = window.requestAnimationFrame(this._start);
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
this._stop = () => window.cancelAnimationFrame(this._handle
|
|
44
|
+
this._stop = () => window.cancelAnimationFrame(this._handle as number);
|
|
45
45
|
}
|
|
46
46
|
else
|
|
47
47
|
{
|
|
48
48
|
// eslint-disable-next-line no-console
|
|
49
49
|
console.warn(
|
|
50
50
|
"Not a browser environment detected. " +
|
|
51
|
-
`Using setInterval@${
|
|
51
|
+
`Using setInterval@${msIfNotBrowser}ms instead of requestAnimationFrame...`
|
|
52
52
|
);
|
|
53
53
|
|
|
54
54
|
this._start = () =>
|
|
55
55
|
{
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
this._handle = (setInterval(() => callback(this.elapsedTime), delay) as unknown) as number;
|
|
56
|
+
this._handle = setInterval(() => callback(this.elapsedTime), msIfNotBrowser);
|
|
59
57
|
};
|
|
60
58
|
|
|
61
|
-
this._stop = () => clearInterval(this._handle
|
|
59
|
+
this._stop = () => clearInterval(this._handle as Interval);
|
|
62
60
|
}
|
|
63
61
|
}
|
|
64
62
|
|
|
@@ -80,4 +78,6 @@ export default class GameLoop
|
|
|
80
78
|
this._handle = undefined;
|
|
81
79
|
this._isRunning = false;
|
|
82
80
|
}
|
|
81
|
+
|
|
82
|
+
public readonly [Symbol.toStringTag]: string = "GameLoop";
|
|
83
83
|
}
|
package/src/models/index.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
export {
|
|
2
|
-
Aggregator,
|
|
3
|
-
AsyncAggregator,
|
|
4
2
|
AggregatedIterator,
|
|
5
3
|
AggregatedAsyncIterator,
|
|
6
4
|
ReducedIterator
|
|
7
5
|
|
|
8
6
|
} from "./aggregators/index.js";
|
|
9
7
|
|
|
8
|
+
export { CallableObject, Publisher, SmartFunction, SwitchableCallback } from "./callbacks/index.js";
|
|
10
9
|
export {
|
|
11
10
|
Exception,
|
|
12
11
|
FatalErrorException,
|
|
@@ -30,10 +29,8 @@ import GameLoop from "./game-loop.js";
|
|
|
30
29
|
|
|
31
30
|
export { SmartIterator, SmartAsyncIterator } from "./iterators/index.js";
|
|
32
31
|
export { JSONStorage } from "./json/index.js";
|
|
33
|
-
export { DeferredPromise, SmartPromise, TimedPromise } from "./promises/index.js";
|
|
34
|
-
|
|
35
|
-
import Publisher from "./publisher.js";
|
|
32
|
+
export { DeferredPromise, LongRunningTask, SmartPromise, Thenable, TimedPromise } from "./promises/index.js";
|
|
36
33
|
|
|
37
34
|
export { Clock, Countdown } from "./timers/index.js";
|
|
38
35
|
|
|
39
|
-
export { GameLoop
|
|
36
|
+
export { GameLoop };
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
+
import AggregatedAsyncIterator from "../aggregators/aggregated-async-iterator.js";
|
|
1
2
|
import { ValueException } from "../exceptions/index.js";
|
|
2
3
|
|
|
3
4
|
import type {
|
|
4
|
-
AsyncGeneratorFunction,
|
|
5
5
|
GeneratorFunction,
|
|
6
|
+
AsyncGeneratorFunction,
|
|
7
|
+
MaybeAsyncGeneratorFunction,
|
|
6
8
|
MaybeAsyncIteratee,
|
|
7
9
|
MaybeAsyncReducer,
|
|
8
|
-
|
|
10
|
+
MaybeAsyncIterable,
|
|
11
|
+
MaybeAsyncIteratorLike,
|
|
9
12
|
MaybeAsyncTypeGuardIteratee
|
|
10
13
|
|
|
11
14
|
} from "./types.js";
|
|
@@ -23,8 +26,8 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
23
26
|
public constructor(iterator: AsyncIterator<T, R, N>);
|
|
24
27
|
public constructor(generatorFn: GeneratorFunction<T, R, N>);
|
|
25
28
|
public constructor(generatorFn: AsyncGeneratorFunction<T, R, N>);
|
|
26
|
-
public constructor(argument:
|
|
27
|
-
public constructor(argument:
|
|
29
|
+
public constructor(argument: MaybeAsyncIteratorLike<T, R, N> | MaybeAsyncGeneratorFunction<T, R, N>);
|
|
30
|
+
public constructor(argument: MaybeAsyncIteratorLike<T, R, N> | MaybeAsyncGeneratorFunction<T, R, N>)
|
|
28
31
|
{
|
|
29
32
|
if (argument instanceof Function)
|
|
30
33
|
{
|
|
@@ -94,13 +97,12 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
94
97
|
{
|
|
95
98
|
let index = 0;
|
|
96
99
|
|
|
97
|
-
// eslint-disable-next-line no-constant-condition
|
|
98
100
|
while (true)
|
|
99
101
|
{
|
|
100
102
|
const result = await this._iterator.next();
|
|
101
103
|
|
|
102
104
|
if (result.done) { return true; }
|
|
103
|
-
if (!(predicate(result.value, index))) { return false; }
|
|
105
|
+
if (!(await predicate(result.value, index))) { return false; }
|
|
104
106
|
|
|
105
107
|
index += 1;
|
|
106
108
|
}
|
|
@@ -109,13 +111,12 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
109
111
|
{
|
|
110
112
|
let index = 0;
|
|
111
113
|
|
|
112
|
-
// eslint-disable-next-line no-constant-condition
|
|
113
114
|
while (true)
|
|
114
115
|
{
|
|
115
116
|
const result = await this._iterator.next();
|
|
116
117
|
|
|
117
118
|
if (result.done) { return false; }
|
|
118
|
-
if (predicate(result.value, index)) { return true; }
|
|
119
|
+
if (await predicate(result.value, index)) { return true; }
|
|
119
120
|
|
|
120
121
|
index += 1;
|
|
121
122
|
}
|
|
@@ -136,7 +137,7 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
136
137
|
const result = await iterator.next();
|
|
137
138
|
|
|
138
139
|
if (result.done) { return result.value; }
|
|
139
|
-
if (predicate(result.value, index)) { yield result.value; }
|
|
140
|
+
if (await predicate(result.value, index)) { yield result.value; }
|
|
140
141
|
|
|
141
142
|
index += 1;
|
|
142
143
|
}
|
|
@@ -155,7 +156,7 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
155
156
|
const result = await iterator.next();
|
|
156
157
|
if (result.done) { return result.value; }
|
|
157
158
|
|
|
158
|
-
yield iteratee(result.value, index);
|
|
159
|
+
yield await iteratee(result.value, index);
|
|
159
160
|
|
|
160
161
|
index += 1;
|
|
161
162
|
}
|
|
@@ -176,7 +177,6 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
176
177
|
index += 1;
|
|
177
178
|
}
|
|
178
179
|
|
|
179
|
-
// eslint-disable-next-line no-constant-condition
|
|
180
180
|
while (true)
|
|
181
181
|
{
|
|
182
182
|
const result = await this._iterator.next();
|
|
@@ -188,6 +188,93 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
188
188
|
}
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
+
public flatMap<V>(iteratee: MaybeAsyncIteratee<T, MaybeAsyncIterable<V>>): SmartAsyncIterator<V, R>
|
|
192
|
+
{
|
|
193
|
+
const iterator = this._iterator;
|
|
194
|
+
|
|
195
|
+
return new SmartAsyncIterator<V, R>(async function* ()
|
|
196
|
+
{
|
|
197
|
+
let index = 0;
|
|
198
|
+
|
|
199
|
+
while (true)
|
|
200
|
+
{
|
|
201
|
+
const result = await iterator.next();
|
|
202
|
+
if (result.done) { return result.value; }
|
|
203
|
+
|
|
204
|
+
const elements = await iteratee(result.value, index);
|
|
205
|
+
|
|
206
|
+
for await (const element of elements)
|
|
207
|
+
{
|
|
208
|
+
yield element;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
index += 1;
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
public drop(count: number): SmartAsyncIterator<T, R | undefined>
|
|
217
|
+
{
|
|
218
|
+
const iterator = this._iterator;
|
|
219
|
+
|
|
220
|
+
return new SmartAsyncIterator<T, R | undefined>(async function* ()
|
|
221
|
+
{
|
|
222
|
+
let index = 0;
|
|
223
|
+
|
|
224
|
+
while (index < count)
|
|
225
|
+
{
|
|
226
|
+
const result = await iterator.next();
|
|
227
|
+
if (result.done) { return; }
|
|
228
|
+
|
|
229
|
+
index += 1;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
while (true)
|
|
233
|
+
{
|
|
234
|
+
const result = await iterator.next();
|
|
235
|
+
if (result.done) { return result.value; }
|
|
236
|
+
|
|
237
|
+
yield result.value;
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
public take(limit: number): SmartAsyncIterator<T, R | undefined>
|
|
242
|
+
{
|
|
243
|
+
const iterator = this._iterator;
|
|
244
|
+
|
|
245
|
+
return new SmartAsyncIterator<T, R | undefined>(async function* ()
|
|
246
|
+
{
|
|
247
|
+
let index = 0;
|
|
248
|
+
|
|
249
|
+
while (index < limit)
|
|
250
|
+
{
|
|
251
|
+
const result = await iterator.next();
|
|
252
|
+
if (result.done) { return result.value; }
|
|
253
|
+
|
|
254
|
+
yield result.value;
|
|
255
|
+
|
|
256
|
+
index += 1;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return;
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
public async find(predicate: MaybeAsyncIteratee<T, boolean>): Promise<T | undefined>
|
|
264
|
+
{
|
|
265
|
+
let index = 0;
|
|
266
|
+
|
|
267
|
+
while (true)
|
|
268
|
+
{
|
|
269
|
+
const result = await this._iterator.next();
|
|
270
|
+
|
|
271
|
+
if (result.done) { return; }
|
|
272
|
+
if (await predicate(result.value, index)) { return result.value; }
|
|
273
|
+
|
|
274
|
+
index += 1;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
191
278
|
public enumerate(): SmartAsyncIterator<[number, T], R>
|
|
192
279
|
{
|
|
193
280
|
return this.map((value, index) => [index, value]);
|
|
@@ -218,7 +305,6 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
218
305
|
{
|
|
219
306
|
let index = 0;
|
|
220
307
|
|
|
221
|
-
// eslint-disable-next-line no-constant-condition
|
|
222
308
|
while (true)
|
|
223
309
|
{
|
|
224
310
|
const result = await this._iterator.next();
|
|
@@ -231,7 +317,6 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
231
317
|
{
|
|
232
318
|
let index = 0;
|
|
233
319
|
|
|
234
|
-
// eslint-disable-next-line no-constant-condition
|
|
235
320
|
while (true)
|
|
236
321
|
{
|
|
237
322
|
const result = await this._iterator.next();
|
|
@@ -248,21 +333,22 @@ export default class SmartAsyncIterator<T, R = void, N = undefined> implements A
|
|
|
248
333
|
return this._iterator.next(...values);
|
|
249
334
|
}
|
|
250
335
|
|
|
251
|
-
public
|
|
336
|
+
public groupBy<K extends PropertyKey>(iteratee: MaybeAsyncIteratee<T, K>): AggregatedAsyncIterator<K, T>
|
|
252
337
|
{
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
// eslint-disable-next-line no-constant-condition
|
|
256
|
-
while (true)
|
|
338
|
+
return new AggregatedAsyncIterator(this.map(async (element, index) =>
|
|
257
339
|
{
|
|
258
|
-
const
|
|
259
|
-
if (result.done) { return elements; }
|
|
340
|
+
const key = await iteratee(element, index);
|
|
260
341
|
|
|
261
|
-
|
|
262
|
-
}
|
|
342
|
+
return [key, element] as [K, T];
|
|
343
|
+
}));
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
public toArray(): Promise<T[]>
|
|
347
|
+
{
|
|
348
|
+
return Array.fromAsync(this as AsyncIterable<T>);
|
|
263
349
|
}
|
|
264
350
|
|
|
265
|
-
public
|
|
351
|
+
public readonly [Symbol.toStringTag]: string = "SmartAsyncIterator";
|
|
266
352
|
|
|
267
353
|
public [Symbol.asyncIterator](): SmartAsyncIterator<T, R, N> { return this; }
|
|
268
354
|
}
|