Skip to content

Commit

Permalink
Minor changes that doesn't effect Collection module (#52)
Browse files Browse the repository at this point in the history
* Removed unnecessary try catch that hindered unknown error to propagate
up in ListCollection, IterableCollection and AsyncIterableCollection.

I made AsyncIterableCollection return LazyPromise, abortableIterable,
timeoutIterable and made it use TimeSpan
class.

I made AsyncCollection contract use PromiseLike instead of Promise type.

* Renamed the timeout method to takeUntilTimeout and abort method to takeUntilAbort of IAsyncCollection contract

* Added changeset file
  • Loading branch information
yousif-khalil-abdulkarim authored Dec 24, 2024
1 parent 993f7c9 commit 790a76c
Show file tree
Hide file tree
Showing 68 changed files with 1,432 additions and 3,311 deletions.
9 changes: 9 additions & 0 deletions .changeset/curvy-ligers-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@daiso-tech/core": minor
---

# IAsyncCollection and AsyncIterableCollection
Changed IAsyncCollection contract to use PromiseLike instead of Promise.
Changed AsyncIterableCollection to use LazyPromise instead of Promise.
Removed all try catches catched unknown errors.
Renamed timeout to takeUntilTimeout, and abort to takeUntilAbort.
4 changes: 2 additions & 2 deletions src/_shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type EnsureType<TValue, TType> =
export type RecordItem<TKey, TValue> = [key: TKey, value: TValue];

export type Lazyable<TValue> = TValue | (() => TValue);
export type Promisable<TValue> = TValue | Promise<TValue>;
export type Promisable<TValue> = TValue | PromiseLike<TValue>;
export type AsyncLazyable<TValue> = TValue | (() => Promisable<TValue>);

export type AsyncIterableValue<TInput> =
Expand All @@ -18,7 +18,7 @@ export type AsyncIterableValue<TInput> =
export type AnyFunction = (...parameters: unknown[]) => unknown;

export type IInitizable = {
init(): Promise<void>;
init(): PromiseLike<void>;
};
export type Func<TArgs extends unknown[], TReturn> = (
...args_: TArgs
Expand Down
1 change: 1 addition & 0 deletions src/async/_module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "@/async/_shared";
export * from "@/async/abortable/_module";
export * from "@/async/abortable-iterable/_module";
export * from "@/async/backof-policies/_module";
Expand Down
4 changes: 1 addition & 3 deletions src/collection/async-iterable-collection/_shared/_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "@/collection/async-iterable-collection/_shared/async-collapse-ite
export * from "@/collection/async-iterable-collection/_shared/async-count-by-iterable";
export * from "@/collection/async-iterable-collection/_shared/async-cross-join-iterable";
export * from "@/collection/async-iterable-collection/_shared/async-entries-iterable";
export * from "@/collection/async-iterable-collection/_shared/async-error-handler-iterable";
export * from "@/collection/async-iterable-collection/_shared/async-filter-iterable";
export * from "@/collection/async-iterable-collection/_shared/async-flat-map-iterable";
export * from "@/collection/async-iterable-collection/_shared/async-group-by-iterable";
Expand All @@ -30,6 +31,3 @@ export * from "@/collection/async-iterable-collection/_shared/async-unique-itera
export * from "@/collection/async-iterable-collection/_shared/async-update-iterable";
export * from "@/collection/async-iterable-collection/_shared/async-when-iterable";
export * from "@/collection/async-iterable-collection/_shared/async-zip-iterable";
export * from "@/collection/async-iterable-collection/_shared/async-timeout-iterable";
export * from "@/collection/async-iterable-collection/_shared/async-delay-iterable";
export * from "@/collection/async-iterable-collection/_shared/async-abort-iterable";

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
CollectionError,
type IAsyncCollection,
UnexpectedCollectionError,
} from "@/contracts/collection/_module";
import { type IAsyncCollection } from "@/contracts/collection/_module";
import { type AsyncIterableValue } from "@/_shared/types";

/**
Expand All @@ -20,33 +16,23 @@ export class AsyncChunkIterable<TInput>
) {}

async *[Symbol.asyncIterator](): AsyncIterator<IAsyncCollection<TInput>> {
try {
const array: TInput[] = [];
let currentChunkSize = 0;
let isFirstIteration = true;
for await (const item of this.collection) {
currentChunkSize %= this.chunkSize;
const isFilled = currentChunkSize === 0;
if (!isFirstIteration && isFilled) {
yield this.makeCollection(array);
array.length = 0;
}
array.push(item);
currentChunkSize++;
isFirstIteration = false;
}
const hasRest = currentChunkSize !== 0;
if (hasRest) {
const array: TInput[] = [];
let currentChunkSize = 0;
let isFirstIteration = true;
for await (const item of this.collection) {
currentChunkSize %= this.chunkSize;
const isFilled = currentChunkSize === 0;
if (!isFirstIteration && isFilled) {
yield this.makeCollection(array);
array.length = 0;
}
} catch (error: unknown) {
if (error instanceof CollectionError) {
throw error;
}
throw new UnexpectedCollectionError(
`Unexpected error "${String(error)}" occured`,
error,
);
array.push(item);
currentChunkSize++;
isFirstIteration = false;
}
const hasRest = currentChunkSize !== 0;
if (hasRest) {
yield this.makeCollection(array);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
type AsyncPredicate,
CollectionError,
type IAsyncCollection,
UnexpectedCollectionError,
} from "@/contracts/collection/_module";
import { type AsyncIterableValue } from "@/_shared/types";

Expand All @@ -22,34 +20,20 @@ export class AsyncChunkWhileIterable<TInput>
) {}

async *[Symbol.asyncIterator](): AsyncIterator<IAsyncCollection<TInput>> {
try {
const array: TInput[] = [];
for await (const [index, item] of this.collection.entries()) {
if (index === 0) {
array.push(item);
} else if (
await this.predicateFn(
item,
index,
this.makeCollection(array),
)
) {
array.push(item);
} else {
yield this.makeCollection(array);
array.length = 0;
array.push(item);
}
const array: TInput[] = [];
for await (const [index, item] of this.collection.entries()) {
if (index === 0) {
array.push(item);
} else if (
await this.predicateFn(item, index, this.makeCollection(array))
) {
array.push(item);
} else {
yield this.makeCollection(array);
array.length = 0;
array.push(item);
}
yield this.makeCollection(array);
} catch (error: unknown) {
if (error instanceof CollectionError) {
throw error;
}
throw new UnexpectedCollectionError(
`Unexpected error "${String(error)}" occured`,
error,
);
}
yield this.makeCollection(array);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { isAsyncIterable, isIterable } from "@/collection/_shared";
import {
type AsyncCollapse,
CollectionError,
type IAsyncCollection,
UnexpectedCollectionError,
} from "@/contracts/collection/_module";

/**
Expand All @@ -15,23 +13,13 @@ export class AsyncCollapseIterable<TInput>
constructor(private collection: IAsyncCollection<TInput>) {}

async *[Symbol.asyncIterator](): AsyncIterator<AsyncCollapse<TInput>> {
try {
for await (const item of this.collection) {
if (isIterable<TInput>(item) || isAsyncIterable<TInput>(item)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
yield* item as any;
} else {
yield item as AsyncCollapse<TInput>;
}
for await (const item of this.collection) {
if (isIterable<TInput>(item) || isAsyncIterable<TInput>(item)) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
yield* item as any;
} else {
yield item as AsyncCollapse<TInput>;
}
} catch (error: unknown) {
if (error instanceof CollectionError) {
throw error;
}
throw new UnexpectedCollectionError(
`Unexpected error "${String(error)}" occured`,
error,
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import {
type AsyncMap,
CollectionError,
type IAsyncCollection,
UnexpectedCollectionError,
} from "@/contracts/collection/_module";
import { type RecordItem } from "@/_shared/types";
/**
Expand All @@ -25,27 +23,17 @@ export class AsyncCountByIterable<TInput, TOutput = TInput>
async *[Symbol.asyncIterator](): AsyncIterator<
RecordItem<TOutput, number>
> {
try {
const map = new Map<TOutput, number>();
for await (const [index, item] of this.collection.entries()) {
const key = await this.callback(item, index, this.collection);
if (!map.has(key)) {
map.set(key, 0);
}
const counter = map.get(key);
if (counter !== undefined) {
map.set(key, counter + 1);
}
const map = new Map<TOutput, number>();
for await (const [index, item] of this.collection.entries()) {
const key = await this.callback(item, index, this.collection);
if (!map.has(key)) {
map.set(key, 0);
}
yield* map;
} catch (error: unknown) {
if (error instanceof CollectionError) {
throw error;
const counter = map.get(key);
if (counter !== undefined) {
map.set(key, counter + 1);
}
throw new UnexpectedCollectionError(
`Unexpected error "${String(error)}" occured`,
error,
);
}
yield* map;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import type { CrossJoinResult } from "@/contracts/collection/_module";
import {
CollectionError,
type IAsyncCollection,
UnexpectedCollectionError,
} from "@/contracts/collection/_module";
import { type IAsyncCollection } from "@/contracts/collection/_module";
import { type AsyncIterableValue } from "@/_shared/types";
import { isIterable } from "@/collection/_module";

Expand All @@ -24,52 +20,38 @@ export class AsyncCrossJoinIterable<TInput, TExtended>
async *[Symbol.asyncIterator](): AsyncIterator<
CrossJoinResult<TInput, TExtended>
> {
try {
const combinations = (
await this.makeCollection([
this.collection,
this.makeCollection(this.iterable),
] as IAsyncCollection<TInput | TExtended>[]).reduce<
IAsyncCollection<Array<TInput | TExtended>>
>(
async (a, b) => {
return await a
.map((x) => {
return b.map((y) => {
return [...x, y];
});
})
.reduce<
IAsyncCollection<Array<TInput | TExtended>>
>(
(c, b) => c.append(b),
this.makeCollection<Array<TInput | TExtended>>(
[],
),
);
},
this.makeCollection([[] as Array<TInput | TExtended>]),
)
).map((combination) => {
// Flatting the array
return combination.reduce<Array<TInput | TExtended>>((a, b) => {
return [...a, ...(isIterable(b) ? b : [b])] as Array<
TInput | TExtended
>;
}, []);
});
const combinations = (
await this.makeCollection([
this.collection,
this.makeCollection(this.iterable),
] as IAsyncCollection<TInput | TExtended>[]).reduce<
IAsyncCollection<Array<TInput | TExtended>>
>(
async (a, b) => {
return await a
.map((x) => {
return b.map((y) => {
return [...x, y];
});
})
.reduce<IAsyncCollection<Array<TInput | TExtended>>>(
(c, b) => c.append(b),
this.makeCollection<Array<TInput | TExtended>>([]),
);
},
this.makeCollection([[] as Array<TInput | TExtended>]),
)
).map((combination) => {
// Flatting the array
return combination.reduce<Array<TInput | TExtended>>((a, b) => {
return [...a, ...(isIterable(b) ? b : [b])] as Array<
TInput | TExtended
>;
}, []);
});

yield* combinations as AsyncIterable<
CrossJoinResult<TInput, TExtended>
>;
} catch (error: unknown) {
if (error instanceof CollectionError) {
throw error;
}
throw new UnexpectedCollectionError(
`Unexpected error "${String(error)}" occured`,
error,
);
}
yield* combinations as AsyncIterable<
CrossJoinResult<TInput, TExtended>
>;
}
}

This file was deleted.

Loading

0 comments on commit 790a76c

Please sign in to comment.