Skip to content

Commit

Permalink
fix: improve type handling for onSegment events (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaDemchenko authored Jan 24, 2025
1 parent e53acf8 commit 8e3b613
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions packages/p2p-media-loader-core/src/requests/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
RequestError,
RequestAbortErrorType,
SegmentWithStream,
Segment,
} from "../types.js";
import * as StreamUtils from "../utils/stream.js";
import * as Utils from "../utils/utils.js";
Expand Down Expand Up @@ -52,6 +53,17 @@ export type RequestStatus =
| "failed"
| "aborted";

function mapSegmentWithStreamToSegment(segment: SegmentWithStream): Segment {
return {
runtimeId: segment.runtimeId,
externalId: segment.externalId,
url: segment.url,
byteRange: segment.byteRange,
startTime: segment.startTime,
endTime: segment.endTime,
};
}

export class Request {
private currentAttempt?: RequestAttempt;
private _failedAttempts = new FailedRequestAttempts();
Expand Down Expand Up @@ -187,7 +199,7 @@ export class Request {
);

this.onSegmentStart({
segment: this.segment,
segment: mapSegmentWithStreamToSegment(this.segment),
downloadSource: requestData.downloadSource,
peerId:
requestData.downloadSource === "p2p" ? requestData.peerId : undefined,
Expand All @@ -209,7 +221,7 @@ export class Request {
);
this._abortRequestCallback?.(new RequestError("abort"));
this.onSegmentAbort({
segment: this.segment,
segment: mapSegmentWithStreamToSegment(this.segment),
downloadSource: this.currentAttempt?.downloadSource,
peerId:
this.currentAttempt?.downloadSource === "p2p"
Expand Down Expand Up @@ -237,7 +249,7 @@ export class Request {
error,
});
this.onSegmentError({
segment: this.segment,
segment: mapSegmentWithStreamToSegment(this.segment),
error,
downloadSource: this.currentAttempt.downloadSource,
peerId:
Expand All @@ -264,7 +276,7 @@ export class Request {
error,
});
this.onSegmentError({
segment: this.segment,
segment: mapSegmentWithStreamToSegment(this.segment),
error,
downloadSource: this.currentAttempt.downloadSource,
peerId:
Expand Down

0 comments on commit 8e3b613

Please sign in to comment.