Skip to content

Commit

Permalink
Merge pull request #6942 from video-dev/task/xhr-loader-replace-non-n…
Browse files Browse the repository at this point in the history
…ull-assertions

Replace non-null assertions in xhr-loader and gate evoking of loader callbacks
  • Loading branch information
robwalch authored Jan 6, 2025
2 parents 6ce0f6c + 9e7cdeb commit e346300
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
20 changes: 12 additions & 8 deletions src/utils/fetch-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ class FetchLoader implements Loader<LoaderContext> {
stats.loading.start = self.performance.now();

const initParams = getRequestParameters(context, this.controller.signal);
const onProgress: LoaderOnProgress<LoaderContext> | undefined =
callbacks.onProgress;
const isArrayBuffer = context.responseType === 'arraybuffer';
const LENGTH = isArrayBuffer ? 'byteLength' : 'length';
const { maxTimeToFirstByteMs, maxLoadTimeMs } = config.loadPolicy;
Expand All @@ -109,8 +107,10 @@ class FetchLoader implements Loader<LoaderContext> {
? maxTimeToFirstByteMs
: maxLoadTimeMs;
this.requestTimeout = self.setTimeout(() => {
this.abortInternal();
callbacks.onTimeout(stats, context, this.response);
if (this.callbacks) {
this.abortInternal();
this.callbacks.onTimeout(stats, context, this.response);
}
}, config.timeout);

const fetchPromise = isPromise(this.request)
Expand All @@ -127,8 +127,10 @@ class FetchLoader implements Loader<LoaderContext> {
config.timeout = maxLoadTimeMs;
this.requestTimeout = self.setTimeout(
() => {
this.abortInternal();
callbacks.onTimeout(stats, context, this.response);
if (this.callbacks) {
this.abortInternal();
this.callbacks.onTimeout(stats, context, this.response);
}
},
maxLoadTimeMs - (first - stats.loading.start),
);
Expand All @@ -145,6 +147,7 @@ class FetchLoader implements Loader<LoaderContext> {

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

const onProgress = this.callbacks?.onProgress;
if (onProgress && Number.isFinite(config.highWaterMark)) {
return this.loadProgressively(
response,
Expand Down Expand Up @@ -184,11 +187,12 @@ class FetchLoader implements Loader<LoaderContext> {
code: response.status,
};

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

callbacks.onSuccess(loaderResponse, stats, context, response);
this.callbacks?.onSuccess(loaderResponse, stats, context, response);
})
.catch((error) => {
self.clearTimeout(this.requestTimeout);
Expand All @@ -199,7 +203,7 @@ class FetchLoader implements Loader<LoaderContext> {
// when destroying, 'error' itself can be undefined
const code: number = !error ? 0 : error.code || 0;
const text: string = !error ? null : error.message;
callbacks.onError(
this.callbacks?.onError(
{ code, text },
context,
error ? error.details : null,
Expand Down
15 changes: 4 additions & 11 deletions src/utils/xhr-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class XhrLoader implements Loader<LoaderContext> {
})
.catch((error: Error) => {
// IE11 throws an exception on xhr.open if attempting to access an HTTP resource over HTTPS
this.callbacks!.onError(
this.callbacks?.onError(
{ code: xhr.status, text: error.message },
context,
xhr,
Expand Down Expand Up @@ -220,23 +220,16 @@ class XhrLoader implements Loader<LoaderContext> {
stats.loaded = stats.total = len;
stats.bwEstimate =
(stats.total * 8000) / (stats.loading.end - stats.loading.first);
if (!this.callbacks) {
return;
}
const onProgress = this.callbacks.onProgress;
const onProgress = this.callbacks?.onProgress;
if (onProgress) {
onProgress(stats, context, data, xhr);
}
if (!this.callbacks) {
return;
}
const response: LoaderResponse = {
url: xhr.responseURL,
data: data,
code: status,
};

this.callbacks.onSuccess(response, stats, context, xhr);
this.callbacks?.onSuccess(response, stats, context, xhr);
return;
}
}
Expand All @@ -254,7 +247,7 @@ class XhrLoader implements Loader<LoaderContext> {
this.retry(retryConfig);
} else {
logger.error(`${status} while loading ${context.url}`);
this.callbacks!.onError(
this.callbacks?.onError(
{ code: status, text: xhr.statusText },
context,
xhr,
Expand Down

0 comments on commit e346300

Please sign in to comment.