fix(core): update

This commit is contained in:
2023-11-12 22:34:55 +01:00
parent 91392e8bd5
commit a400a0a04c
6 changed files with 94 additions and 74 deletions

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartstream',
version: '3.0.18',
version: '3.0.19',
description: 'simplifies access to node streams'
}

View File

@ -129,23 +129,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
}
}
public notBackpressured = true;
public get backpressured(): boolean {
return !this.notBackpressured;
}
public push(chunkArg?: TOutput | null): boolean {
const result = super.push(chunkArg);
if (!result && this.handleBackpressure) {
this.notBackpressured = false;
this.pause();
// Listen for 'drain' event to resume
this.once('drain', () => {
this.notBackpressured = true;
this.resume(); // Resume the source of data
});
}
return result;
}
private asyncWritePromiseObjectmap = new plugins.lik.ObjectMap<Promise<any>>();
// Ensure the _write method types the chunk as TInput and encodes TOutput
public async _write(chunk: TInput, encoding: string, callback: (error?: Error | null) => void) {
@ -162,26 +146,39 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
};
try {
const writeDeferred = plugins.smartpromise.defer();
this.asyncWritePromiseObjectmap.add(writeDeferred.promise);
const modifiedChunk = await this.writeFunction(chunk, tools);
if (modifiedChunk) {
this.push(modifiedChunk)
if (this.backpressured && this.handleBackpressure) {
this.once('drain', () => {
callback();
});
const drainDeferred = plugins.smartpromise.defer();
this.once('drain', () => {
drainDeferred.resolve();
});
const canPushMore = this.push(modifiedChunk);
if (!canPushMore) {
await drainDeferred.promise;
console.log('jojojo');
callback();
writeDeferred.resolve();
} else {
callback();
}
writeDeferred.resolve();
}
} else {
callback();
writeDeferred.resolve();
}
writeDeferred.resolve();
writeDeferred.promise.then(() => {
this.asyncWritePromiseObjectmap.remove(writeDeferred.promise);
});
} catch (err) {
callback(err);
}
return this.notBackpressured;
}
public async _final(callback: (error?: Error | null) => void) {
await Promise.all(this.asyncWritePromiseObjectmap.getArray());
if (this.finalFunction) {
const tools: IStreamTools = {
truncate: () => callback(),
@ -194,10 +191,10 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
this.push(finalChunk);
}
} catch (err) {
this.push(null);
callback(err);
return;
}
} else {
// nothing here
}
this.push(null);
callback();

View File

@ -4,8 +4,9 @@ import * as stream from 'stream';
export { stream };
// pushrocks scope
import * as lik from '@push.rocks/lik';
import * as smartpromise from '@push.rocks/smartpromise';
import * as smartrx from '@push.rocks/smartrx';
export { smartpromise, smartrx };
export { lik, smartpromise, smartrx };