fix AsyncEnumerable types
This commit is contained in:
55
src/async.ts
55
src/async.ts
@@ -9,8 +9,8 @@ import { MaybeAsyncPredicate, MaybeAsyncConverter, MaybeAsyncIterable, MaybeAsyn
|
|||||||
|
|
||||||
//#region interfaces
|
//#region interfaces
|
||||||
|
|
||||||
export interface AsyncEnumerable<TElement> extends AsyncIterable<TElement> {
|
export interface AsyncEnumerable<TElement> extends AsyncIterable<Awaited<TElement>> {
|
||||||
iterator(): AsyncIterator<TElement>;
|
iterator(): AsyncIterator<Awaited<TElement>>;
|
||||||
|
|
||||||
apply<TResult>(pipeline: (enumerable: AsyncEnumerable<TElement>) => TResult): TResult;
|
apply<TResult>(pipeline: (enumerable: AsyncEnumerable<TElement>) => TResult): TResult;
|
||||||
|
|
||||||
@@ -115,7 +115,6 @@ export interface AsyncEnumerable<TElement> extends AsyncIterable<TElement> {
|
|||||||
|
|
||||||
cached(): AsyncEnumerable<TElement>;
|
cached(): AsyncEnumerable<TElement>;
|
||||||
|
|
||||||
asArray(): Promise<TElement[]>;
|
|
||||||
toArray(): Promise<TElement[]>;
|
toArray(): Promise<TElement[]>;
|
||||||
toMap<TKey, TValue>(keySelector: MaybeAsyncConverter<TElement, TKey>, valueSelector: MaybeAsyncConverter<TElement, TValue>): Promise<Map<TKey, TValue>>;
|
toMap<TKey, TValue>(keySelector: MaybeAsyncConverter<TElement, TKey>, valueSelector: MaybeAsyncConverter<TElement, TValue>): Promise<Map<TKey, TValue>>;
|
||||||
toSet(): Promise<Set<TElement>>;
|
toSet(): Promise<Set<TElement>>;
|
||||||
@@ -150,7 +149,7 @@ export namespace AsyncEnumerable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function wrap<T>(iterable: MaybeAsyncIterable<T>): AsyncEnumerable<T> {
|
export function wrap<T>(iterable: MaybeAsyncIterable<T>): AsyncEnumerable<T> {
|
||||||
if (iterable instanceof BaseAsyncEnumerable) {
|
if (isAsyncEnumerable<T>(iterable)) {
|
||||||
return iterable;
|
return iterable;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,19 +168,19 @@ export namespace AsyncEnumerable {
|
|||||||
return EmptyAsyncEnumerable.INSTANCE;
|
return EmptyAsyncEnumerable.INSTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function single<T>(obj: T): AsyncEnumerable<T> {
|
export function single<T>(obj: T | PromiseLike<T>): AsyncEnumerable<T> {
|
||||||
return new WrappedObjectAsync(obj);
|
return new WrappedObjectAsync(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function array<T>(array: T[]): AsyncEnumerable<T> {
|
export function array<T>(array: (T | PromiseLike<T>)[]): AsyncEnumerable<T> {
|
||||||
return new WrappedArrayAsync(array);
|
return new WrappedArrayAsync(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function arrayLike<T>(arrayLike: ArrayLike<T>): AsyncEnumerable<T> {
|
export function arrayLike<T>(arrayLike: ArrayLike<(T | PromiseLike<T>)>): AsyncEnumerable<T> {
|
||||||
return new WrappedArrayLikeAsync(arrayLike);
|
return new WrappedArrayLikeAsync(arrayLike);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function of<T>(...elements: T[]): AsyncEnumerable<T> {
|
export function of<T>(...elements: (T | PromiseLike<T>)[]): AsyncEnumerable<T> {
|
||||||
switch (elements.length) {
|
switch (elements.length) {
|
||||||
case 0:
|
case 0:
|
||||||
return empty();
|
return empty();
|
||||||
@@ -222,7 +221,7 @@ export namespace AsyncEnumerable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (count < 0) {
|
if (count < 0) {
|
||||||
throw new RangeError();
|
throw new Error("count < 0");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count === 0) {
|
if (count === 0) {
|
||||||
@@ -236,7 +235,7 @@ export namespace AsyncEnumerable {
|
|||||||
return new RepeatAsyncEnumerable(value, count);
|
return new RepeatAsyncEnumerable(value, count);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isAsyncEnumerable<T = any>(obj: any): obj is Enumerable<T> {
|
export function isAsyncEnumerable<T = any>(obj: any): obj is AsyncEnumerable<T> {
|
||||||
return obj instanceof AsyncEnumerableMarker;
|
return obj instanceof AsyncEnumerableMarker;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -254,7 +253,7 @@ export abstract class BaseAsyncEnumerable<TElement> extends AsyncEnumerableMarke
|
|||||||
return this.iterator();
|
return this.iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract iterator(): AsyncIterator<TElement>;
|
abstract iterator(): AsyncIterator<Awaited<TElement>>;
|
||||||
|
|
||||||
apply<TResult>(pipeline: (enumerable: AsyncEnumerable<TElement>) => TResult): TResult {
|
apply<TResult>(pipeline: (enumerable: AsyncEnumerable<TElement>) => TResult): TResult {
|
||||||
return pipeline(this);
|
return pipeline(this);
|
||||||
@@ -854,10 +853,6 @@ export abstract class BaseAsyncEnumerable<TElement> extends AsyncEnumerableMarke
|
|||||||
return new CacheAsyncEnumerable<TElement>(this);
|
return new CacheAsyncEnumerable<TElement>(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
async asArray() {
|
|
||||||
return await this.toArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
async toArray() {
|
async toArray() {
|
||||||
const array: TElement[] = [];
|
const array: TElement[] = [];
|
||||||
|
|
||||||
@@ -1185,10 +1180,6 @@ class DelegatedAsyncEnumerable<TElement> extends AsyncEnumerableMarker implement
|
|||||||
return this.#enumerable.cached();
|
return this.#enumerable.cached();
|
||||||
}
|
}
|
||||||
|
|
||||||
asArray(): Promise<TElement[]> {
|
|
||||||
return this.#enumerable.asArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
toArray(): Promise<TElement[]> {
|
toArray(): Promise<TElement[]> {
|
||||||
return this.#enumerable.toArray();
|
return this.#enumerable.toArray();
|
||||||
}
|
}
|
||||||
@@ -1359,9 +1350,9 @@ class RepeatForeverAsyncEnumerable<T> extends BaseAsyncEnumerable<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class WrappedObjectAsync<T> extends BaseAsyncEnumerable<T> {
|
class WrappedObjectAsync<T> extends BaseAsyncEnumerable<T> {
|
||||||
readonly #obj: T;
|
readonly #obj: T | PromiseLike<T>;
|
||||||
|
|
||||||
constructor(obj: T) {
|
constructor(obj: T | PromiseLike<T>) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.#obj = obj;
|
this.#obj = obj;
|
||||||
@@ -1377,9 +1368,9 @@ class WrappedObjectAsync<T> extends BaseAsyncEnumerable<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class WrappedArrayAsync<T> extends BaseAsyncEnumerable<T> {
|
class WrappedArrayAsync<T> extends BaseAsyncEnumerable<T> {
|
||||||
readonly #array: T[];
|
readonly #array: (T | PromiseLike<T>)[];
|
||||||
|
|
||||||
constructor(array: T[]) {
|
constructor(array: (T | PromiseLike<T>)[]) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.#array = array;
|
this.#array = array;
|
||||||
@@ -1390,14 +1381,14 @@ class WrappedArrayAsync<T> extends BaseAsyncEnumerable<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override async *iterator() {
|
override async *iterator() {
|
||||||
yield* this.#array.values();
|
yield* this.#array;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class WrappedArrayLikeAsync<T> extends BaseAsyncEnumerable<T> {
|
class WrappedArrayLikeAsync<T> extends BaseAsyncEnumerable<T> {
|
||||||
readonly #arrayLike: ArrayLike<T>;
|
readonly #arrayLike: ArrayLike<T | PromiseLike<T>>;
|
||||||
|
|
||||||
constructor(arrayLike: ArrayLike<T>) {
|
constructor(arrayLike: ArrayLike<T | PromiseLike<T>>) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
this.#arrayLike = arrayLike;
|
this.#arrayLike = arrayLike;
|
||||||
@@ -1423,8 +1414,8 @@ class WrappedAsyncIterable<T> extends BaseAsyncEnumerable<T> {
|
|||||||
this.#iterable = iterable;
|
this.#iterable = iterable;
|
||||||
}
|
}
|
||||||
|
|
||||||
override iterator() {
|
override async *iterator() {
|
||||||
return this.#iterable[Symbol.asyncIterator]();
|
yield* this.#iterable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1437,8 +1428,8 @@ class GeneratorAsyncEnumerable<T> extends BaseAsyncEnumerable<T> {
|
|||||||
this.#generator = generator;
|
this.#generator = generator;
|
||||||
}
|
}
|
||||||
|
|
||||||
override iterator() {
|
override async *iterator() {
|
||||||
return this.#generator();
|
yield* this.#generator();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1483,10 +1474,6 @@ class WrappedEnumerable<T> extends BaseAsyncEnumerable<T> {
|
|||||||
return this.#enumerable.maxCount();
|
return this.#enumerable.maxCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
override async asArray() {
|
|
||||||
return this.#enumerable.asArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
override async *iterator() {
|
override async *iterator() {
|
||||||
yield* this.#enumerable;
|
yield* this.#enumerable;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user