Skip to content

Commit

Permalink
Enable Prettier (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlika authored Apr 2, 2021
1 parent e9c73ad commit 65bfee0
Show file tree
Hide file tree
Showing 51 changed files with 3,049 additions and 2,805 deletions.
1 change: 1 addition & 0 deletions p2p-media-loader-core/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 120
[*.md]
trim_trailing_whitespace = false
[*.json]
Expand Down
3 changes: 2 additions & 1 deletion p2p-media-loader-core/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking"
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"prettier"
],
"env": {
"browser": true
Expand Down
8 changes: 4 additions & 4 deletions p2p-media-loader-core/lib/bandwidth-approximator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class BandwidthApproximator {

const interval = Math.min(SMOOTH_INTERVAL, timeStamp);
this.lastBandwidth.push(new NumberWithTime(this.currentBytesSum / interval, timeStamp));
}
};

// in bytes per millisecond
public getBandwidth = (timeStamp: number): number => {
Expand All @@ -53,13 +53,13 @@ export class BandwidthApproximator {
}

return maxBandwidth;
}
};

public getSmoothInterval = (): number => {
return SMOOTH_INTERVAL;
}
};

public getMeasureInterval = (): number => {
return MEASURE_INTERVAL;
}
};
}
64 changes: 30 additions & 34 deletions p2p-media-loader-core/lib/http-media-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ import { STEEmitter } from "./stringly-typed-event-emitter";
import { Segment } from "./loader-interface";
import { SegmentValidatorCallback, XhrSetupCallback, SegmentUrlBuilder } from "./hybrid-loader";

export class HttpMediaManager extends STEEmitter<
"segment-loaded" | "segment-error" | "bytes-downloaded"
> {

private xhrRequests = new Map<string, {xhr: XMLHttpRequest, segment: Segment}>();
export class HttpMediaManager extends STEEmitter<"segment-loaded" | "segment-error" | "bytes-downloaded"> {
private xhrRequests = new Map<string, { xhr: XMLHttpRequest; segment: Segment }>();
private failedSegments = new Map<string, number>();
private debug = Debug("p2pml:http-media-manager");

public constructor(readonly settings: {
httpFailedSegmentTimeout: number,
httpUseRanges: boolean,
segmentValidator?: SegmentValidatorCallback,
xhrSetup?: XhrSetupCallback
segmentUrlBuilder?: SegmentUrlBuilder
}) {
public constructor(
readonly settings: {
httpFailedSegmentTimeout: number;
httpUseRanges: boolean;
segmentValidator?: SegmentValidatorCallback;
xhrSetup?: XhrSetupCallback;
segmentUrlBuilder?: SegmentUrlBuilder;
}
) {
super();
}

Expand All @@ -45,9 +44,7 @@ export class HttpMediaManager extends STEEmitter<

this.cleanTimedOutFailedSegments();

const segmentUrl = this.settings.segmentUrlBuilder
? this.settings.segmentUrlBuilder(segment)
: segment.url;
const segmentUrl = this.settings.segmentUrlBuilder ? this.settings.segmentUrlBuilder(segment) : segment.url;

this.debug("http segment download", segmentUrl);

Expand All @@ -60,7 +57,7 @@ export class HttpMediaManager extends STEEmitter<
if (segment.range) {
xhr.setRequestHeader("Range", segment.range);
downloadedPieces = undefined; // TODO: process downloadedPieces for segments with range headers too
} else if ((downloadedPieces !== undefined) && this.settings.httpUseRanges) {
} else if (downloadedPieces !== undefined && this.settings.httpUseRanges) {
let bytesDownloaded = 0;
for (const piece of downloadedPieces) {
bytesDownloaded += piece.byteLength;
Expand All @@ -79,9 +76,9 @@ export class HttpMediaManager extends STEEmitter<
this.settings.xhrSetup(xhr, segmentUrl);
}

this.xhrRequests.set(segment.id, {xhr, segment});
this.xhrRequests.set(segment.id, { xhr, segment });
xhr.send();
}
};

public abort = (segment: Segment): void => {
const request = this.xhrRequests.get(segment.id);
Expand All @@ -91,29 +88,29 @@ export class HttpMediaManager extends STEEmitter<
this.xhrRequests.delete(segment.id);
this.debug("http segment abort", segment.id);
}
}
};

public isDownloading = (segment: Segment): boolean => {
return this.xhrRequests.has(segment.id);
}
};

public isFailed = (segment: Segment): boolean => {
const time = this.failedSegments.get(segment.id);
return time !== undefined && time > this.now();
}
};

public getActiveDownloads = (): ReadonlyMap<string, {segment: Segment}> => {
public getActiveDownloads = (): ReadonlyMap<string, { segment: Segment }> => {
return this.xhrRequests;
}
};

public getActiveDownloadsCount = (): number => {
return this.xhrRequests.size;
}
};

public destroy = (): void => {
this.xhrRequests.forEach(request => request.xhr.abort());
this.xhrRequests.forEach((request) => request.xhr.abort());
this.xhrRequests.clear();
}
};

private setupXhrEvents = (xhr: XMLHttpRequest, segment: Segment, downloadedPieces?: ArrayBuffer[]) => {
let prevBytesLoaded = 0;
Expand All @@ -125,14 +122,14 @@ export class HttpMediaManager extends STEEmitter<
});

xhr.addEventListener("load", async (event) => {
if ((xhr.status < 200) || (xhr.status >= 300)) {
if (xhr.status < 200 || xhr.status >= 300) {
this.segmentFailure(segment, event, xhr);
return;
}

let data = xhr.response as ArrayBuffer;

if ((downloadedPieces !== undefined) && (xhr.status === 206)) {
if (downloadedPieces !== undefined && xhr.status === 206) {
let bytesDownloaded = 0;
for (const piece of downloadedPieces) {
bytesDownloaded += piece.byteLength;
Expand Down Expand Up @@ -160,7 +157,7 @@ export class HttpMediaManager extends STEEmitter<
xhr.addEventListener("timeout", (event: unknown) => {
this.segmentFailure(segment, event, xhr);
});
}
};

private segmentDownloadFinished = async (segment: Segment, data: ArrayBuffer, xhr: XMLHttpRequest) => {
segment.responseUrl = xhr.responseURL === null ? undefined : xhr.responseURL;
Expand All @@ -177,15 +174,15 @@ export class HttpMediaManager extends STEEmitter<

this.xhrRequests.delete(segment.id);
this.emit("segment-loaded", segment, data);
}
};

private segmentFailure = (segment: Segment, error: unknown, xhr: XMLHttpRequest) => {
segment.responseUrl = xhr.responseURL === null ? undefined : xhr.responseURL;

this.xhrRequests.delete(segment.id);
this.failedSegments.set(segment.id, this.now() + this.settings.httpFailedSegmentTimeout);
this.emit("segment-error", segment, error);
}
};

private cleanTimedOutFailedSegments = () => {
const now = this.now();
Expand All @@ -197,9 +194,8 @@ export class HttpMediaManager extends STEEmitter<
}
});

candidates.forEach(id => this.failedSegments.delete(id));
}
candidates.forEach((id) => this.failedSegments.delete(id));
};

private now = () => performance.now();

}
Loading

0 comments on commit 65bfee0

Please sign in to comment.