Skip to content

Commit

Permalink
ci
Browse files Browse the repository at this point in the history
  • Loading branch information
tachibana-shin committed Aug 14, 2023
1 parent 3437c63 commit d274986
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 119 deletions.
5 changes: 4 additions & 1 deletion src/apis/nettruyen/runs/get-list-chapters.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { CURL } from "../const"
import { parsePath } from "src/apis/__helpers__/parsePath"

import { CURL } from "../const"

// eslint-disable-next-line camelcase
export default async function (manga_id: number) {
const { data } = await get(
// eslint-disable-next-line camelcase
`${CURL}/Comic/Services/ComicService.asmx/ProcessChapterList?comicId=${manga_id}`
)

Expand Down
104 changes: 66 additions & 38 deletions src/components/library/LibraryTabOffline.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
listEpRemove =
listEpRemove.length === list?.size
? []
: [...new Set([...listEpRemove, ...list.keys()])]
: [...new Set([...listEpRemove, ...(list?.keys() ?? [])])]
"
>
<Icon icon="solar:check-circle-linear" class="size-1.5em mr-1" />
Expand Down Expand Up @@ -155,8 +155,8 @@
<li class="px-1 py-1 col-3">
<q-btn
no-caps
:outline="!list.has(data.id)"
:disable="list.has(data.id)"
:outline="!list?.has(data.id)"
:disable="list?.has(data.id)"
class="bg-gray-400 bg-opacity-10 w-full text-light-200 text-opacity-90 text-weight-regular"
:class="{
'text-blue': epsSelected.has(data),
Expand All @@ -169,7 +169,7 @@
>
{{ parseFloat(data.name) }}
<Icon
v-if="list.has(data.id)"
v-if="list?.has(data.id)"
icon="iconoir:check"
class="absolute top-1 left-1 size-1.2em text-green-400"
/>
Expand All @@ -184,7 +184,9 @@
<q-btn no-caps stack unelevated class="min-w-15% text-weight-regular">
<Icon icon="solar:download-minimalistic-bold" class="size-1.5em" />
Download
<q-badge floating>{{ list.size }}</q-badge>
<q-badge v-if="list && list.size > 0" floating>{{
list.size
}}</q-badge>
</q-btn>
<q-btn
no-caps
Expand Down Expand Up @@ -222,28 +224,32 @@
</template>

<script lang="ts" setup>
import type { UnwrapRef } from "vue"
import { SERVERS } from "src/apis/nettruyen/parsers/truyen-tranh/[slug]/[ep-id]"
import GetListChapters from "src/apis/nettruyen/runs/get-list-chapters"
import SlugChapChap from "src/apis/nettruyen/runs/truyen-tranh/[slug]-chap-[chap]"
import { SERVERS } from "src/apis/nettruyen/parsers/truyen-tranh/[slug]/[ep-id]"
import type { MetaEpisodeOnDisk } from "src/logic/download-manager"

const IDMStore = useIDMStore()
const $q = useQuasar()

IDMStore.runLoadInMemory()

const metaMangaShowInfo = ref<(typeof IDMStore.listMangaSorted)[0]>()
const listEpDownloaded = ref<Awaited<ReturnType<typeof getListEpisodes>>>()
const listEpDownloaded = ref<
{
ref: Awaited<ReturnType<typeof getListEpisodes>>[0]
}[]
>()
watch(
metaMangaShowInfo,
async (meta) => {
console.log(meta)
if (meta) {
listEpDownloaded.value = shallowReactive(
await getListEpisodes(meta.manga_id).catch(() => [])
)
).map((ref) => ({ ref }))
} else {
listEpDownloaded.value = void 0
listEpDownloaded.value = undefined
}
},
{ immediate: true }
Expand All @@ -260,28 +266,41 @@ const list = computed(() => {
if (!listEpDownloading.value || !listEpDownloaded.value) return

return new Map(
[
...(listEpDownloaded.value.map((ref) => ({ ref })) ?? []),
...(listEpDownloading.value as UnwrapRef<
ReturnType<typeof createTaskDownloadEpisode>
>[]),
]
[...(listEpDownloaded.value ?? []), ...listEpDownloading.value]
.sort((a, b) => b.ref.start_download_at - a.ref.start_download_at)
.map((item) => [item.ref.ep_id, item])
)
) as Map<
number,
{
ref: Awaited<ReturnType<typeof getListEpisodes>>[0]
downloading?: boolean
stop?: () => void
}
>
})

async function resume(item) {
async function resume(
item:
| Awaited<ReturnType<typeof IDMStore.download>>
| {
ref: MetaEpisodeOnDisk
}
) {
if (!metaMangaShowInfo.value || !listEpDownloaded.value) return

try {
const { ref } = await IDMStore.resumeDownload(metaMangaShowInfo.value, item)
const result = await IDMStore.resumeDownload(metaMangaShowInfo.value, item)

listEpDownloaded.value.splice(
listEpDownloaded.value.findIndex((item) => item.ep_id === ref.ep_id) >>>
0,
listEpDownloaded.value.findIndex(
(item) => item.ref.ep_id === result.ref.ep_id
) >>> 0,
1,
ref
result
)
} catch (err) {
if (err?.message === "user_paused") return
if ((err as Error | undefined)?.message === "user_paused") return
// eslint-disable-next-line functional/no-throw-statement
throw err
}
}
Expand All @@ -295,24 +314,28 @@ async function remove() {
removing.value = true

const meta = metaMangaShowInfo.value
// eslint-disable-next-line camelcase
const { manga_id } = meta
await Promise.allSettled(
// eslint-disable-next-line camelcase
listEpRemove.value.map(async (ep_id) => {
await IDMStore.deleteEpisode(manga_id, ep_id)
})
)

const storeTask = IDMStore.queue.get(manga_id)
if (storeTask?.size > 0)
if (storeTask && storeTask?.size > 0)
// eslint-disable-next-line camelcase
listEpRemove.value.forEach((ep_id) => {
// clear
storeTask.delete(ep_id)
})
listEpDownloaded.value = listEpDownloaded.value.filter((item) => {
return !listEpRemove.value.includes(item.ep_id)
})
if (listEpDownloaded.value)
listEpDownloaded.value = listEpDownloaded.value.filter((item) => {
return !listEpRemove.value.includes(item.ref.ep_id)
})

if (list.size === 0) {
if (list.value?.size === 0) {
IDMStore.listMangaSorted.splice(
IDMStore.listMangaSorted.indexOf(meta) >>> 0,
1
Expand All @@ -330,7 +353,7 @@ const epsSelected = shallowReactive<
>(new Set())

watch(showDownloadMore, async (state) => {
allEpisodes.value = void 0
allEpisodes.value = undefined
epsSelected.clear()

if (!state || !list.value) return
Expand Down Expand Up @@ -367,22 +390,27 @@ async function download() {

downloading.value = true
for (const ep of epsSelected) {
const { pages } = await SlugChapChap(
const conf = await SlugChapChap(
ep.path.split("/").slice(2).join("/"),
false
)
// eslint-disable-next-line promise/catch-or-return
IDMStore.download(metaMangaShowInfo.value, {
path: ep.path,
ep_id: ep.id,
ep_name: ep.name,
pages: pages.map((item) => SERVERS[0].parse(item)),
}).then(({ ref }) => {
listEpDownloaded.value.splice(
listEpDownloaded.value.findIndex((item) => item.ep_id === ref.ep_id) >>>
0,
1,
ref
)
pages: conf.pages.map((item) => SERVERS[0].parse(item, conf)),
}).then((result) => {
if (listEpDownloaded.value)
listEpDownloaded.value.splice(
listEpDownloaded.value.findIndex(
(item) => item.ref.ep_id === result.ref.ep_id
) >>> 0,
1,
result
)
// eslint-disable-next-line no-useless-return
return
})
}
downloading.value = false
Expand Down
29 changes: 13 additions & 16 deletions src/logic/download-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("download-manager", () => {
metaManga,
metaEp
)
expect(ref.value.downloaded).toBe(0)
expect(ref.downloaded).toBe(0)
expect(downloading.value).toBeFalsy()

const watcher = vi.fn()
Expand Down Expand Up @@ -148,7 +148,7 @@ describe("download-manager", () => {
expect(await readFile("poster/" + hashIDManga, Encoding.UTF8)).toBe(
manga_image
)
expect(watcher.mock.calls.length).toBe(9)
expect(watcher.mock.calls.length).toBe(8)
})

test("should forcibly stopped while downloading", async () => {
Expand All @@ -171,7 +171,7 @@ describe("download-manager", () => {
metaManga,
metaEp
)
expect(ref.value.downloaded).toBe(0)
expect(ref.downloaded).toBe(0)
expect(downloading.value).toBeFalsy()

await start().catch(() => null)
Expand Down Expand Up @@ -239,7 +239,7 @@ describe("download-manager", () => {
metaManga,
metaEp
)
expect(ref.value.downloaded).toBe(0)
expect(ref.downloaded).toBe(0)
expect(downloading.value).toBeFalsy()

await start().catch(() => null)
Expand Down Expand Up @@ -294,7 +294,7 @@ describe("download-manager", () => {
start: start2,
downloading: dl2,
} = createTaskDownloadEpisode(metaManga, metaEp)
expect(ref2.value.downloaded).toBe(0)
expect(ref2.downloaded).toBe(0)
expect(dl2.value).toBeFalsy()

await start2().catch(() => null)
Expand Down Expand Up @@ -355,30 +355,27 @@ describe("download-manager", () => {
metaEp
)
expect(downloading.value).toBe(false)
expect(ref.value.downloaded).toBe(0)
expect(ref.downloaded).toBe(0)

const watcher = vi.fn()
watch(ref, watcher, { deep: true })

start()
await sleep(1_000)

await sleep(300)
expect(downloading.value).toBe(true)
expect(ref.value.downloaded).toBeGreaterThanOrEqual(0)

await sleep(1_500)
expect(ref.downloaded).toBeGreaterThanOrEqual(1)

stop()
await sleep(500)

expect(downloading.value).toBe(false)
expect(ref.value.downloaded).toBeGreaterThanOrEqual(1)

resume()
expect(ref.downloaded).toBeGreaterThanOrEqual(1)

const promise = resume()
expect(downloading.value).toBe(true)
expect(ref.value.downloaded).toBeGreaterThanOrEqual(1)
expect(ref.downloaded).toBeGreaterThanOrEqual(1)

await start()
await promise

// check hash file page
expect(await readdir(`files/${hashIDManga}/${hashIDEp}`)).toEqual([
Expand Down
Loading

0 comments on commit d274986

Please sign in to comment.