Skip to content

Commit

Permalink
Don't pass next chapters to the popup (fix #9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spark-NF committed Mar 10, 2019
1 parent 15b0d17 commit 30d5505
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 30 deletions.
6 changes: 3 additions & 3 deletions src/common/Filter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IFilter, isValid } from "./Filter";

describe("isValid", () => {
it("Returns true for a valid filter", () => {
const novel: any = { next: [] };
const novel: any = { nextLength: 0 };
const filter: IFilter = {
operator: "eq",
value: 0,
Expand All @@ -13,7 +13,7 @@ describe("isValid", () => {
});

it("Returns true for an invalid filter", () => {
const novel: any = { next: [] };
const novel: any = { nextLength: 0 };
const filter: IFilter = {
operator: "lt",
value: 0,
Expand All @@ -24,7 +24,7 @@ describe("isValid", () => {
});

it("Works with 'days since first unread chapter' filters", () => {
const novel: any = { next: [{ date: new Date("2019-01-01") }] };
const novel: any = { next: { date: new Date("2019-01-01") }, nextLength: 1 };
const filter: IFilter = {
operator: "ge",
value: 1,
Expand Down
6 changes: 3 additions & 3 deletions src/common/Filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ function daysBetween(a: Date, b: Date): number {
function getVal(novel: IReadingListResult, what: IFilter["what"]): number {
switch (what) {
case "days_since_first_unread":
return novel.next.length > 0 && novel.next[0].date
? daysBetween(new Date(), novel.next[0].date)
return novel.nextLength > 0 && novel.next.date
? daysBetween(new Date(), novel.next.date)
: 0;

case "days_since_latest":
Expand All @@ -46,7 +46,7 @@ function getVal(novel: IReadingListResult, what: IFilter["what"]): number {

case "unread":
default:
return novel.next.length;
return novel.nextLength;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/common/NovelUpdatesClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ describe("NovelUpdatesClient", () => {
expect(novels[0].status.name).toBe("c1357");
expect(novels[0].status.html).toBe("c1357");
expect(novels[0].status.url).toBe("http://www.wuxiaworld.com/novel/martial-world/mw-chapter-1357");
expect(novels[0].next.map((c) => c.id)).toEqual([64956, 55539]); // ?
expect(novels[0].next.id).toEqual(64956);
expect(novels[0].latest.id).toBe(2310235);
expect(novels[0].latest.name).toBe("c2132");
expect(novels[0].latest.html).toBe("c2132");
Expand Down
19 changes: 10 additions & 9 deletions src/common/NovelUpdatesClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export interface IReadingListResult {
notes?: string;
};
status: IReadingListResultChapter;
next: IReadingListResultChapter[];
next?: IReadingListResultChapter;
nextLength: number;
latest: IReadingListResultChapter;
readingList: number;
manual?: boolean;
Expand Down Expand Up @@ -330,9 +331,8 @@ export class NovelUpdatesClient {
hasNotes: row.dataset.notes === "yes",
tags: row.dataset.tags.split(",").filter((t) => t.length > 0),
},
chapters: [] as IReadingListResultChapter[],
status,
next: [] as IReadingListResultChapter[],
nextLength: 0,
latest,
readingList: id,
};
Expand Down Expand Up @@ -360,7 +360,6 @@ export class NovelUpdatesClient {
}

await this.loadNextChapters(novel, row, chapters);

novels.push(novel);
}

Expand Down Expand Up @@ -388,7 +387,6 @@ export class NovelUpdatesClient {

// Refresh "next" and "chapters" members
const chapters = await this.getNovelChapters(novel);
novel.next.splice(0, novel.next.length);
await this.loadNextChapters(novel, row, chapters);

return true;
Expand All @@ -399,6 +397,9 @@ export class NovelUpdatesClient {
row: HTMLTableRowElement,
chapters: IReadingListResultChapter[],
): Promise<void> {
novel.next = undefined;
novel.nextLength = 0;

// Load and build next chapters if necessary
if (novel.status.id !== undefined && novel.status.id !== novel.latest.id) {

Expand All @@ -416,10 +417,10 @@ export class NovelUpdatesClient {

// Build the "next" object
const index = chapters.map((c) => c.id).indexOf(novel.status.id);
for (let i = index + 1; i < chapters.length; ++i) {
const chapter = chapters[i];
const fullChapter = chapter.id in fullNext ? fullNext[chapter.id] : chapter;
novel.next.push(fullChapter);
novel.nextLength = chapters.length - index - 1;
if (index + 1 < chapters.length) {
const chapter = chapters[index + 1];
novel.next = chapter.id in fullNext ? fullNext[chapter.id] : chapter;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/common/ReadChapterListener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ describe("ReadChapterListener", async () => {
hasNotes: false,
tags: [],
},
chapters,
status: chapters[0],
next: chapters.slice(1),
next: chapters[1],
nextLength: 2,
latest: chapters[chapters.length - 1],
readingList: 1,
}];

it("Ignores iframe navigation", async () => {
Expand Down
8 changes: 4 additions & 4 deletions src/common/ReadChapterListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export class ReadChapterListener extends WebNavigationListener {
const oldUrl = window.sessionStorage[tabId];
const readingList = await this.novelsGetter();
for (const novel of readingList) {
if (novel.next.length >= 1
if (novel.nextLength >= 1
&& novel.status.url
&& novel.next[0].url
&& novel.next.url
&& removeProtocol(novel.status.url) === removeProtocol(oldUrl)
&& removeProtocol(novel.next[0].url) === removeProtocol(data.url)
&& removeProtocol(novel.next.url) === removeProtocol(data.url)
) {
await this.callback(novel, novel.next[0]);
await this.callback(novel, novel.next);
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/popup/components/NovelRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<a :href="novel.url" target="_blank">
{{ novel.name }}
</a>
<span class="badge badge-primary" v-if="novel.next.length > 0">
{{ novel.next.length }}
<span class="badge badge-primary" v-if="novel.nextLength > 0">
{{ novel.nextLength }}
</span>
</td>
<td class="novel-loading" colspan="4" v-if="loadingMessage">
Expand All @@ -21,8 +21,8 @@
</select>
</td>
<td class="novel-chapter" v-if="!loadingMessage">
<span v-if="novel.next.length > 0">
<chapter-link :chapter="novel.next[0]" />
<span v-if="novel.nextLength > 0">
<chapter-link :chapter="novel.next" />
</span>
</td>
<td class="novel-chapter" v-if="!loadingMessage">
Expand Down Expand Up @@ -65,7 +65,7 @@ export default class NovelRow extends Vue {
optChapters: any = {};
get hasNew(): boolean {
return this.novel.next.length > 0;
return this.novel.nextLength > 0;
}
changeCurrentChapter() {
Expand Down Expand Up @@ -96,9 +96,10 @@ export default class NovelRow extends Vue {
}
async startEdition() {
this.optChapters = this.hasNew && this.novel.status.id !== undefined && this.novel.next.length > 0
? [this.novel.status].concat(this.novel.next)
: await background.client.getNovelChapters(this.novel);
const chapters = await background.client.getNovelChapters(this.novel);
this.optChapters = this.hasNew && this.novel.status.id !== undefined && this.novel.nextLength > 0
? chapters.slice(chapters.map((c) => c.id).indexOf(this.novel.status.id))
: chapters
this.editing = true;
(this.$refs.readSelect as HTMLSelectElement).focus();
Expand Down

0 comments on commit 30d5505

Please sign in to comment.