Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlika committed Feb 4, 2024
1 parent d736a0a commit b393962
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
31 changes: 17 additions & 14 deletions packages/p2p-media-loader-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ export class Core<TStream extends Stream = Stream> {
}

hasSegment(segmentLocalId: string): boolean {
const segment = StreamUtils.getSegmentFromStreamsMap(
this.streams,
segmentLocalId,
);
return !!segment;
return !!StreamUtils.getSegmentFromStreamsMap(this.streams, segmentLocalId);
}

getStream(streamLocalId: string): StreamWithSegments<TStream> | undefined {
Expand All @@ -64,6 +60,7 @@ export class Core<TStream extends Stream = Stream> {

addStreamIfNoneExists(stream: TStream): void {
if (this.streams.has(stream.localId)) return;

this.streams.set(stream.localId, {
...stream,
segments: new Map<string, Segment>(),
Expand All @@ -82,7 +79,9 @@ export class Core<TStream extends Stream = Stream> {
const segment = { ...s, stream };
stream.segments.set(segment.localId, segment);
});

removeSegmentIds?.forEach((id) => stream.segments.delete(id));

this.mainStreamLoader?.updateStream(stream);
this.secondaryStreamLoader?.updateStream(stream);
}
Expand All @@ -91,13 +90,15 @@ export class Core<TStream extends Stream = Stream> {
if (!this.manifestResponseUrl) {
throw new Error("Manifest response url is not defined");
}

if (!this.segmentStorage) {
this.segmentStorage = new SegmentsMemoryStorage(
this.manifestResponseUrl,
this.settings,
);
await this.segmentStorage.initialize();
}

const segment = this.identifySegment(segmentLocalId);
const loader = this.getStreamHybridLoader(segment);
void loader.loadSegment(segment, callbacks);
Expand Down Expand Up @@ -157,6 +158,7 @@ export class Core<TStream extends Stream = Stream> {
if (!this.manifestResponseUrl) {
throw new Error("Manifest response url is not defined");
}

const createNewHybridLoader = (manifestResponseUrl: string) => {
if (!this.segmentStorage?.isInitialized) {
throw new Error("Segment storage is not initialized");
Expand All @@ -171,14 +173,15 @@ export class Core<TStream extends Stream = Stream> {
this.eventHandlers,
);
};
const streamTypeLoaderKeyMap = {
main: "mainStreamLoader",
secondary: "secondaryStreamLoader",
} as const;
const { type } = segment.stream;
const loaderKey = streamTypeLoaderKeyMap[type];

return (this[loaderKey] =
this[loaderKey] ?? createNewHybridLoader(this.manifestResponseUrl));

if (segment.stream.type === "main") {
this.mainStreamLoader ??= createNewHybridLoader(this.manifestResponseUrl);
return this.mainStreamLoader;
} else {
this.secondaryStreamLoader ??= createNewHybridLoader(
this.manifestResponseUrl,
);
return this.secondaryStreamLoader;
}
}
}
26 changes: 9 additions & 17 deletions packages/p2p-media-loader-core/src/requests/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,23 +315,15 @@ class FailedRequestAttempts {
}
}

const requestInnerErrorTypes = ["abort", "bytes-receiving-timeout"] as const;

const httpRequestErrorTypes = [
"http-error",
"http-bytes-mismatch",
"http-unexpected-status-code",
] as const;

const peerRequestErrorTypes = [
"peer-response-bytes-mismatch",
"peer-segment-absent",
"peer-closed",
] as const;

export type RequestInnerErrorType = (typeof requestInnerErrorTypes)[number];
export type HttpRequestErrorType = (typeof httpRequestErrorTypes)[number];
export type PeerRequestErrorType = (typeof peerRequestErrorTypes)[number];
export type RequestInnerErrorType = "abort" | "bytes-receiving-timeout";
export type HttpRequestErrorType =
| "http-error"
| "http-bytes-mismatch"
| "http-unexpected-status-code";
export type PeerRequestErrorType =
| "peer-response-bytes-mismatch"
| "peer-segment-absent"
| "peer-closed";

type RequestErrorType =
| RequestInnerErrorType
Expand Down

0 comments on commit b393962

Please sign in to comment.