fix(core): update
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user