fix(core): update

This commit is contained in:
2023-11-11 18:53:38 +01:00
parent 1f67bc0e1e
commit 3a2dc1c37e
4 changed files with 311 additions and 131 deletions

View File

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

View File

@ -15,6 +15,7 @@ export interface IStreamFinalFunction<rT> {
}
export interface SmartStreamOptions<TInput, TOutput> extends DuplexOptions {
handleBackpressure?: boolean;
readFunction?: () => Promise<void>;
writeFunction?: IStreamWriteFunction<TInput, TOutput>;
finalFunction?: IStreamFinalFunction<TOutput>;
@ -109,6 +110,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
// INSTANCE
private readFunction?: () => Promise<void>;
private handleBackpressure: boolean;
private writeFunction?: IStreamWriteFunction<TInput, TOutput>;
private finalFunction?: IStreamFinalFunction<TOutput>;
private observableSubscription?: plugins.smartrx.rxjs.Subscription;
@ -118,6 +120,7 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
this.readFunction = optionsArg?.readFunction;
this.writeFunction = optionsArg?.writeFunction;
this.finalFunction = optionsArg?.finalFunction;
this.handleBackpressure = optionsArg?.handleBackpressure ?? true;
}
public async _read(size: number): Promise<void> {
@ -143,8 +146,12 @@ export class SmartDuplex<TInput = any, TOutput = any> extends Duplex {
try {
const modifiedChunk = await this.writeFunction(chunk, tools);
if (modifiedChunk) {
if (!this.push(modifiedChunk)) {
// Handle backpressure if necessary
if (!this.push(modifiedChunk) && this.handleBackpressure) {
this.pause();
// Listen for 'drain' event to resume
this.once('drain', () => {
this.resume(); // Resume the source of data
});
}
}
callback();