Don't regenerate groups if not needed

This commit is contained in:
Ajay
2022-12-11 15:20:05 -05:00
parent a5d91c9b57
commit 7eb6b3a79d

View File

@@ -62,6 +62,7 @@ class PreviewBar {
originalChapterBar: HTMLElement; originalChapterBar: HTMLElement;
originalChapterBarBlocks: NodeListOf<HTMLElement>; originalChapterBarBlocks: NodeListOf<HTMLElement>;
chapterMargin: number; chapterMargin: number;
lastRenderedSegments: PreviewBarSegment[];
unfilteredChapterGroups: ChapterGroup[]; unfilteredChapterGroups: ChapterGroup[];
chapterGroups: ChapterGroup[]; chapterGroups: ChapterGroup[];
@@ -308,6 +309,8 @@ class PreviewBar {
this.container.appendChild(bar); this.container.appendChild(bar);
} }
console.log(this.segments)
console.trace()
this.createChaptersBar(this.segments.sort((a, b) => a.segment[0] - b.segment[0])); this.createChaptersBar(this.segments.sort((a, b) => a.segment[0] - b.segment[0]));
if (chapterChevron) { if (chapterChevron) {
@@ -356,8 +359,10 @@ class PreviewBar {
return; return;
} }
// Merge overlapping chapters if (segments !== this.lastRenderedSegments) {
this.unfilteredChapterGroups = this.createChapterRenderGroups(segments); // Merge overlapping chapters
this.unfilteredChapterGroups = this.createChapterRenderGroups(segments);
}
if (segments.every((segments) => segments.source === SponsorSourceType.YouTube) if (segments.every((segments) => segments.source === SponsorSourceType.YouTube)
|| (!Config.config.renderSegmentsAsChapters || (!Config.config.renderSegmentsAsChapters
@@ -368,24 +373,28 @@ class PreviewBar {
return; return;
} }
const filteredSegments = segments?.filter((segment) => this.chapterFilter(segment)); if (segments !== this.lastRenderedSegments) {
if (filteredSegments) { this.lastRenderedSegments = segments;
let groups = this.unfilteredChapterGroups;
if (filteredSegments.length !== segments.length) {
groups = this.createChapterRenderGroups(filteredSegments);
}
this.chapterGroups = groups.filter((segment) => this.chapterGroupFilter(segment));
if (groups.length !== this.chapterGroups.length) { const filteredSegments = segments?.filter((segment) => this.chapterFilter(segment));
// Fix missing sections due to filtered segments if (filteredSegments) {
for (let i = 1; i < this.chapterGroups.length; i++) { let groups = this.unfilteredChapterGroups;
if (this.chapterGroups[i].segment[0] !== this.chapterGroups[i - 1].segment[1]) { if (filteredSegments.length !== segments.length) {
this.chapterGroups[i - 1].segment[1] = this.chapterGroups[i].segment[0] groups = this.createChapterRenderGroups(filteredSegments);
}
this.chapterGroups = groups.filter((segment) => this.chapterGroupFilter(segment));
if (groups.length !== this.chapterGroups.length) {
// Fix missing sections due to filtered segments
for (let i = 1; i < this.chapterGroups.length; i++) {
if (this.chapterGroups[i].segment[0] !== this.chapterGroups[i - 1].segment[1]) {
this.chapterGroups[i - 1].segment[1] = this.chapterGroups[i].segment[0]
}
} }
} }
} else {
this.chapterGroups = this.unfilteredChapterGroups;
} }
} else {
this.chapterGroups = this.unfilteredChapterGroups;
} }
if (!this.chapterGroups || this.chapterGroups.length <= 0) { if (!this.chapterGroups || this.chapterGroups.length <= 0) {