Skip to content

Commit

Permalink
Minor OO refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dex4er committed Jul 14, 2019
1 parent f0ca249 commit 9736d71
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/promise-readable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ export class PromiseReadable<TReadable extends ReadableStream> {

_errored?: Error

private readonly errorHandler: (err: Error) => void

constructor(readonly stream: TReadable) {
this.errorHandler = (err: Error) => {
this._errored = err
}

stream.on("error", this.errorHandler)
}

Expand Down Expand Up @@ -147,17 +141,6 @@ export class PromiseReadable<TReadable extends ReadableStream> {
return this
}

destroy(): void {
if (this.stream) {
if (this.errorHandler) {
this.stream.removeListener("error", this.errorHandler)
}
if (typeof this.stream.destroy === "function") {
this.stream.destroy()
}
}
}

once(event: "close" | "end" | "error"): Promise<void>
once(event: "open"): Promise<number>

Expand Down Expand Up @@ -239,6 +222,19 @@ export class PromiseReadable<TReadable extends ReadableStream> {
stream.on("error", errorHandler)
})
}

destroy(): void {
if (this.stream) {
this.stream.removeListener("error", this.errorHandler)
if (typeof this.stream.destroy === "function") {
this.stream.destroy()
}
}
}

private readonly errorHandler = (err: Error): void => {
this._errored = err
}
}

export default PromiseReadable

0 comments on commit 9736d71

Please sign in to comment.