diff --git a/docs/FSAsyncIterable.html b/docs/FSAsyncIterable.html index 392fafc..877f3ee 100644 --- a/docs/FSAsyncIterable.html +++ b/docs/FSAsyncIterable.html @@ -66,7 +66,7 @@ @@ -236,7 +236,7 @@

- filter(callback) + filter(callback, parallel)

@@ -272,6 +272,8 @@
Parameters:
+ Default + Description @@ -293,12 +295,42 @@
Parameters:
+ + + + async function (must return Promise). + + + + + parallel + + + + + + + + + + + + 1 + + + + + number of callbacks runs in parallel + + + + @@ -342,7 +374,7 @@
Parameters:

View Source - fs-async-iterable.class.ts, line 30 + fs-async-iterable.class.ts, line 57

@@ -521,7 +553,7 @@
Parameters:

View Source - fs-async-iterable.class.ts, line 62 + fs-async-iterable.class.ts, line 116

@@ -558,7 +590,7 @@

- map(callback) + map(callback, parallel)

@@ -594,6 +626,8 @@
Parameters:
+ Default + Description @@ -615,12 +649,42 @@
Parameters:
+ + + + async function (must return Promise), which transform input item to output item + + + + + parallel + + + + + + + + + + + + 1 + + + + + number of callbacks runs in parallel + + + + @@ -664,7 +728,7 @@
Parameters:

View Source - fs-async-iterable.class.ts, line 17 + fs-async-iterable.class.ts, line 18

@@ -831,7 +895,7 @@
Parameters:

View Source - fs-async-iterable.class.ts, line 50 + fs-async-iterable.class.ts, line 104

@@ -932,7 +996,7 @@

View Source - fs-async-iterable.class.ts, line 82 + fs-async-iterable.class.ts, line 136

diff --git a/docs/FSDir.html b/docs/FSDir.html index 0560306..d79c37c 100644 --- a/docs/FSDir.html +++ b/docs/FSDir.html @@ -66,7 +66,7 @@ diff --git a/docs/FSDirent.html b/docs/FSDirent.html index a813d20..bdc8ec2 100644 --- a/docs/FSDirent.html +++ b/docs/FSDirent.html @@ -66,7 +66,7 @@ diff --git a/docs/FSFile.html b/docs/FSFile.html index 3efbe95..a6cfa9f 100644 --- a/docs/FSFile.html +++ b/docs/FSFile.html @@ -66,7 +66,7 @@ diff --git a/docs/FSFileHash.html b/docs/FSFileHash.html index e47bbc6..766c20f 100644 --- a/docs/FSFileHash.html +++ b/docs/FSFileHash.html @@ -66,7 +66,7 @@ diff --git a/docs/FSFileRead.html b/docs/FSFileRead.html index 8cf808b..6b87ea9 100644 --- a/docs/FSFileRead.html +++ b/docs/FSFileRead.html @@ -66,7 +66,7 @@ diff --git a/docs/FSIterable.html b/docs/FSIterable.html index a8d487c..a417b0d 100644 --- a/docs/FSIterable.html +++ b/docs/FSIterable.html @@ -66,7 +66,7 @@ diff --git a/docs/PromiseLikeWriteStream.html b/docs/PromiseLikeWriteStream.html new file mode 100644 index 0000000..c282e72 --- /dev/null +++ b/docs/PromiseLikeWriteStream.html @@ -0,0 +1,196 @@ + + + + + + + + PromiseLikeWriteStream + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + +
+
+
+ +
+
+
+

Interface

+

PromiseLikeWriteStream

+
+ + + + + +
+ +
+ +

PromiseLikeWriteStream

+ + +
+ +
+
+ + +
Contains methods that write to file
+ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

+ View Source + + fs-file.write.class.ts, line 66 + +

+ +
+ + + + +
+ + + + + + + + + + + + + + + + + + + + +
+ +
+ + + + +
+ + + +
+
+
+
+ + + + + + \ No newline at end of file diff --git a/docs/fs-async-iterable.class.ts.html b/docs/fs-async-iterable.class.ts.html index c91cc66..7fa8955 100644 --- a/docs/fs-async-iterable.class.ts.html +++ b/docs/fs-async-iterable.class.ts.html @@ -68,7 +68,7 @@ @@ -89,23 +89,49 @@

fs-async-iterable.class.ts

* This is util class not inteded to use directly, It used for iterate through dirents. */ export class FSAsyncIterable<T> implements AsyncIterable<T> { - constructor(private readonly iterable: AsyncGenerator<T>) {} + constructor(private readonly iterable: AsyncIterable<T>) {} [Symbol.asyncIterator]() { - return this.iterable; + return this.iterable[Symbol.asyncIterator](); } /** * Transform input items * @param callback - async function (must return Promise), * which transform input item to output item + * @param parallel - number of callbacks runs in parallel */ - map<P>(callback: (item: T) => Promise<P>) { + map<P>(callback: (item: T) => Promise<P>, parallel: number = 1) { const iterable = this.iterable; const mapAsyncGenerator = async function*() { + const pending = new Set<Promise<P>>(); + const finished = new Set<Promise<P>>(); for await (const item of iterable) { - yield await callback(item); + const current = callback(item); + pending.add(current); + current + .catch(() => { + /* do nothing */ + }) + .finally(() => { + pending.delete(current); + finished.add(current); + }); + if (pending.size >= parallel) await Promise.race(pending); + + for (const item of Array.from(finished)) { + yield item; + finished.delete(item); + } + } + while (pending.size > 0) { + await Promise.race(pending); + for (const item of Array.from(finished)) { + yield item; + finished.delete(item); + } } + yield* finished; }; return new FSAsyncIterable(mapAsyncGenerator()); } @@ -113,13 +139,41 @@

fs-async-iterable.class.ts

/** * Pass items only when callback returns true. * @param callback - async function (must return Promise). + * @param parallel - number of callbacks runs in parallel */ - filter(callback: (item: T) => Promise<boolean>) { + filter(callback: (item: T) => Promise<boolean>, parallel: number = 1) { const iterable = this.iterable; const filterAsyncGenerator = async function*() { + const pending = new Set<Promise<boolean>>(); + const finished = new Set<T>(); for await (const item of iterable) { - if (await callback(item)) yield item; + const current = callback(item); + pending.add(current); + current + .then(res => { + if (res) finished.add(item); + }) + .catch(() => { + /* do nothing */ + }) + .finally(() => { + pending.delete(current); + }); + if (pending.size >= parallel) await Promise.race(pending); + + for (const item of Array.from(finished)) { + yield item; + finished.delete(item); + } + } + while (pending.size > 0) { + await Promise.race(pending); + for (const item of Array.from(finished)) { + yield item; + finished.delete(item); + } } + yield* finished; }; return new FSAsyncIterable(filterAsyncGenerator()); } diff --git a/docs/fs-dir.class.ts.html b/docs/fs-dir.class.ts.html index 0dc5b02..05742f5 100644 --- a/docs/fs-dir.class.ts.html +++ b/docs/fs-dir.class.ts.html @@ -68,7 +68,7 @@ diff --git a/docs/fs-file.class.ts.html b/docs/fs-file.class.ts.html index 71c6888..f632835 100644 --- a/docs/fs-file.class.ts.html +++ b/docs/fs-file.class.ts.html @@ -68,7 +68,7 @@ diff --git a/docs/fs-file.hash.class.ts.html b/docs/fs-file.hash.class.ts.html index d7efeda..7ec7e37 100644 --- a/docs/fs-file.hash.class.ts.html +++ b/docs/fs-file.hash.class.ts.html @@ -68,7 +68,7 @@ diff --git a/docs/fs-file.read.class.ts.html b/docs/fs-file.read.class.ts.html index 0355615..6d4e90f 100644 --- a/docs/fs-file.read.class.ts.html +++ b/docs/fs-file.read.class.ts.html @@ -68,7 +68,7 @@ diff --git a/docs/fs-file.write.class.ts.html b/docs/fs-file.write.class.ts.html index 1883a5c..bb3817c 100644 --- a/docs/fs-file.write.class.ts.html +++ b/docs/fs-file.write.class.ts.html @@ -68,7 +68,7 @@ @@ -85,10 +85,12 @@

fs-file.write.class.ts

-
import { writeFile, createWriteStream, appendFile, WriteFileOptions } from 'fs';
+            
import { writeFile, createWriteStream, appendFile, WriteFileOptions, WriteStream } from 'fs';
 /**
  * Contains methods that write to file
  */
+export interface PromiseLikeWriteStream extends WriteStream, PromiseLike<void> {}
+
 export class FSFileWrite {
   constructor(public readonly path: string) {}
 
@@ -114,11 +116,20 @@ 

fs-file.write.class.ts

} /** - * Creates standart node fs WriteStream for this path. + * Creates fs WriteStream for this path. * @param options - node fs.createWriteStream options + * @returns thenable stream, which resolves on stream close */ createWriteStream(options?: SecondArgument<typeof createWriteStream>) { - return createWriteStream(this.path, options); + const stream = createWriteStream(this.path, options) as PromiseLikeWriteStream; + const promise = new Promise<void>((resolve, reject) => { + stream.on('close', () => { + resolve(); + }); + stream.on('error', err => reject(err)); + }); + stream.then = promise.then.bind(promise); + return stream; } /** diff --git a/docs/fs-iterable.class.ts.html b/docs/fs-iterable.class.ts.html index 3a35ae2..8750d96 100644 --- a/docs/fs-iterable.class.ts.html +++ b/docs/fs-iterable.class.ts.html @@ -68,7 +68,7 @@ diff --git a/docs/global.html b/docs/global.html index 5e9966f..c81c32f 100644 --- a/docs/global.html +++ b/docs/global.html @@ -66,7 +66,7 @@ diff --git a/docs/index.html b/docs/index.html index af18944..1a1c1c7 100644 --- a/docs/index.html +++ b/docs/index.html @@ -66,7 +66,7 @@ diff --git a/docs/index.ts.html b/docs/index.ts.html index 09df2d6..2529208 100644 --- a/docs/index.ts.html +++ b/docs/index.ts.html @@ -68,7 +68,7 @@ @@ -197,8 +197,8 @@

index.ts

throw Error(`Not found process.env[${envVariableName}] and fallback value didnt provided.`); }; /** - * Returns FSAsyncIterator that - * takes in a starting index and ending index then iterates + * Returns FSAsyncIterator that + * takes in a starting index and ending index then iterates * thru all integers from start to end * @param from - start index * @param to - end index