Skip to content

Commit e346300

Browse files
authored
Merge pull request #6942 from video-dev/task/xhr-loader-replace-non-null-assertions
Replace non-null assertions in xhr-loader and gate evoking of loader callbacks
2 parents 6ce0f6c + 9e7cdeb commit e346300

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

src/utils/fetch-loader.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ class FetchLoader implements Loader<LoaderContext> {
9393
stats.loading.start = self.performance.now();
9494

9595
const initParams = getRequestParameters(context, this.controller.signal);
96-
const onProgress: LoaderOnProgress<LoaderContext> | undefined =
97-
callbacks.onProgress;
9896
const isArrayBuffer = context.responseType === 'arraybuffer';
9997
const LENGTH = isArrayBuffer ? 'byteLength' : 'length';
10098
const { maxTimeToFirstByteMs, maxLoadTimeMs } = config.loadPolicy;
@@ -109,8 +107,10 @@ class FetchLoader implements Loader<LoaderContext> {
109107
? maxTimeToFirstByteMs
110108
: maxLoadTimeMs;
111109
this.requestTimeout = self.setTimeout(() => {
112-
this.abortInternal();
113-
callbacks.onTimeout(stats, context, this.response);
110+
if (this.callbacks) {
111+
this.abortInternal();
112+
this.callbacks.onTimeout(stats, context, this.response);
113+
}
114114
}, config.timeout);
115115

116116
const fetchPromise = isPromise(this.request)
@@ -127,8 +127,10 @@ class FetchLoader implements Loader<LoaderContext> {
127127
config.timeout = maxLoadTimeMs;
128128
this.requestTimeout = self.setTimeout(
129129
() => {
130-
this.abortInternal();
131-
callbacks.onTimeout(stats, context, this.response);
130+
if (this.callbacks) {
131+
this.abortInternal();
132+
this.callbacks.onTimeout(stats, context, this.response);
133+
}
132134
},
133135
maxLoadTimeMs - (first - stats.loading.start),
134136
);
@@ -145,6 +147,7 @@ class FetchLoader implements Loader<LoaderContext> {
145147

146148
stats.total = getContentLength(response.headers) || stats.total;
147149

150+
const onProgress = this.callbacks?.onProgress;
148151
if (onProgress && Number.isFinite(config.highWaterMark)) {
149152
return this.loadProgressively(
150153
response,
@@ -184,11 +187,12 @@ class FetchLoader implements Loader<LoaderContext> {
184187
code: response.status,
185188
};
186189

190+
const onProgress = this.callbacks?.onProgress;
187191
if (onProgress && !Number.isFinite(config.highWaterMark)) {
188192
onProgress(stats, context, responseData, response);
189193
}
190194

191-
callbacks.onSuccess(loaderResponse, stats, context, response);
195+
this.callbacks?.onSuccess(loaderResponse, stats, context, response);
192196
})
193197
.catch((error) => {
194198
self.clearTimeout(this.requestTimeout);
@@ -199,7 +203,7 @@ class FetchLoader implements Loader<LoaderContext> {
199203
// when destroying, 'error' itself can be undefined
200204
const code: number = !error ? 0 : error.code || 0;
201205
const text: string = !error ? null : error.message;
202-
callbacks.onError(
206+
this.callbacks?.onError(
203207
{ code, text },
204208
context,
205209
error ? error.details : null,

src/utils/xhr-loader.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class XhrLoader implements Loader<LoaderContext> {
113113
})
114114
.catch((error: Error) => {
115115
// IE11 throws an exception on xhr.open if attempting to access an HTTP resource over HTTPS
116-
this.callbacks!.onError(
116+
this.callbacks?.onError(
117117
{ code: xhr.status, text: error.message },
118118
context,
119119
xhr,
@@ -220,23 +220,16 @@ class XhrLoader implements Loader<LoaderContext> {
220220
stats.loaded = stats.total = len;
221221
stats.bwEstimate =
222222
(stats.total * 8000) / (stats.loading.end - stats.loading.first);
223-
if (!this.callbacks) {
224-
return;
225-
}
226-
const onProgress = this.callbacks.onProgress;
223+
const onProgress = this.callbacks?.onProgress;
227224
if (onProgress) {
228225
onProgress(stats, context, data, xhr);
229226
}
230-
if (!this.callbacks) {
231-
return;
232-
}
233227
const response: LoaderResponse = {
234228
url: xhr.responseURL,
235229
data: data,
236230
code: status,
237231
};
238-
239-
this.callbacks.onSuccess(response, stats, context, xhr);
232+
this.callbacks?.onSuccess(response, stats, context, xhr);
240233
return;
241234
}
242235
}
@@ -254,7 +247,7 @@ class XhrLoader implements Loader<LoaderContext> {
254247
this.retry(retryConfig);
255248
} else {
256249
logger.error(`${status} while loading ${context.url}`);
257-
this.callbacks!.onError(
250+
this.callbacks?.onError(
258251
{ code: status, text: xhr.statusText },
259252
context,
260253
xhr,

0 commit comments

Comments
 (0)