Skip to content

Commit

Permalink
Update index.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed Oct 10, 2022
1 parent 623229f commit 6d1223b
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions source/core/index.ts
Expand Up @@ -182,6 +182,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {

// We need this because `this._request` if `undefined` when using cache
private _requestInitialized: boolean;
private readonly _removeListeners: () => void;

constructor(url: UrlType, options?: OptionsType, defaults?: DefaultsType) {
super({
Expand All @@ -200,6 +201,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
this._triggerRead = false;
this._cancelTimeouts = noop;
this._jobs = [];
this._removeListeners = noop;
this._flushed = false;
this._requestInitialized = false;
this._aborted = false;
Expand Down Expand Up @@ -251,9 +253,14 @@ export default class Request extends Duplex implements RequestEvents<Request> {
this.destroy(new AbortError(this));
}

this.options.signal?.addEventListener('abort', () => {
const abort = () => {
this.destroy(new AbortError(this));
});
};

this.options.signal?.addEventListener('abort', abort);
this._removeListeners = () => {
this.options.signal?.removeEventListener('abort', abort);
};

// Important! If you replace `body` in a handler with another stream, make sure it's readable first.
// The below is run only once.
Expand Down Expand Up @@ -508,6 +515,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
// Prevent further retries
this._stopRetry();
this._cancelTimeouts();
this._removeListeners();

if (this.options) {
const {body} = this.options;
Expand Down

0 comments on commit 6d1223b

Please sign in to comment.