From b6574cb36267e3b40ad88e66ffa13f05eb9bddde Mon Sep 17 00:00:00 2001 From: Tachibana Shin Date: Tue, 18 Apr 2023 04:45:24 +0000 Subject: [PATCH 1/2] cherry from https://github.com/anime-vsub/desktop-web/pull/98/commits/dca9505f1b243d64aa77f9e6c024ab917aed19a6#diff-8fa4b52909f895e8cda060d2035234e0a42ca2c7d3f8f8de1b35a056537bf199 --- src/components/BrtPlayer.vue | 106 ++++++++++++++++++++++++++++++----- src/constants.ts | 1 + 2 files changed, 93 insertions(+), 14 deletions(-) diff --git a/src/components/BrtPlayer.vue b/src/components/BrtPlayer.vue index d8ea4458..ead6bf28 100644 --- a/src/components/BrtPlayer.vue +++ b/src/components/BrtPlayer.vue @@ -677,6 +677,7 @@ import { } from "quasar" import { useMemoControl } from "src/composibles/memo-control" import { + CONFIRMATION_TIME_IS_ACTUALLY_WATCHING, C_URL, DELAY_SAVE_VIEWING_PROGRESS, playbackRates, @@ -737,6 +738,11 @@ const props = defineProps<{ fetchSeason: (season: string) => Promise progressWatchStore: ProgressWatchStore }>() +const uidChap = computed(() => { + const uid = `${props.currentSeason}/${props.currentChap ?? ""}` // 255 byte + + return uid +}) // ===== setup effect ===== @@ -842,7 +848,7 @@ watch( if ((err as Error)?.message !== "NOT_RESET") console.error(err) } - progressRestored = `${currentSeason}/${currentChap}` + progressRestored = uidChap.value } }, { immediate: true } @@ -968,6 +974,33 @@ watch( { immediate: true } ) +const seasonMetaCreated = new Set() + +async function createSeason(): Promise { + // eslint-disable-next-line camelcase + const { user_data } = authStore + const { currentSeason, nameCurrentChap: seasonName, poster } = props + + if (seasonMetaCreated.has(currentSeason)) return true + + if ( + // eslint-disable-next-line camelcase + !user_data || + !currentSeason || + typeof seasonName !== "string" || + !poster + ) + return false + console.log("set new season poster %s", poster) + await historyStore.createSeason(currentSeason, { + poster, + seasonName, + name: props.name, + }) + seasonMetaCreated.add(currentSeason) + return true +} + const emit = defineEmits<{ ( name: "cur-update", @@ -978,6 +1011,38 @@ const emit = defineEmits<{ } ): void }>() + +const storeFirstSaving = new Set() +{ + // eslint-disable-next-line functional/no-let, no-undef + let timeout: NodeJS.Timeout | number | null = null + // eslint-disable-next-line functional/no-let + let uidChapTimeout: string | null = null + onBeforeUnmount(() => { + if (timeout) clearTimeout(timeout) + }) + const watcher = watch(artPlaying, (artPlaying) => { + if (artPlaying) { + if (timeout) { + if (uidChapTimeout === uidChap.value) return + console.log("stop timeout add first saving because change chap") + clearTimeout(timeout) + } + timeout = setTimeout(() => { + console.log("allow first saving") + storeFirstSaving.add(uidChap.value) + }, CONFIRMATION_TIME_IS_ACTUALLY_WATCHING) + uidChapTimeout = uidChap.value + } else { + if (timeout) { + console.log("stop timeout add first saving") + clearTimeout(timeout) + uidChapTimeout = null + watcher() + } + } + }) +} // eslint-disable-next-line functional/no-let let processingSaveCurTimeIn: string | null = null const saveCurTimeToPer = throttle( @@ -988,7 +1053,8 @@ const saveCurTimeToPer = throttle( dur: number, nameCurrentChap: string ) => { - const uid = `${currentSeason}/${currentChap}` // 255 byte + const uid = uidChap.value // 255 byte + if (!(await createSeason())) return if (processingSaveCurTimeIn === uid) return // in progressing save this processingSaveCurTimeIn = uid @@ -999,19 +1065,28 @@ const saveCurTimeToPer = throttle( dur, name: nameCurrentChap, }) - .finally(() => (processingSaveCurTimeIn = null)) - .catch(() => console.warn("save viewing progress failed")) - - emit("cur-update", { - cur, - dur, - id: currentChap, - }) - console.log("save viewing progress") + .catch((err) => console.warn("save viewing progress failed: ", err)) + .finally(() => { + emit("cur-update", { + cur, + dur, + id: currentChap, + }) + console.log("save viewing progress") + + processingSaveCurTimeIn = null + }) }, DELAY_SAVE_VIEWING_PROGRESS ) - +const throttleEmitCurUpdate = throttle(() => { + if (props.currentChap) + emit("cur-update", { + cur: artCurrentTime.value, + dur: artDuration.value, + id: props.currentChap, + }) +}, DELAY_SAVE_VIEWING_PROGRESS) function onVideoTimeUpdate() { if ( artPlaying.value && @@ -1023,10 +1098,13 @@ function onVideoTimeUpdate() { } if (!progressRestored) return - if (!seasonReady) return if (!props.currentChap) return if (typeof props.nameCurrentChap !== "string") return + if (!storeFirstSaving.has(uidChap.value)) { + throttleEmitCurUpdate() + return console.log("bypass because not first saving") + } saveCurTimeToPer( props.currentSeason, props.currentChap, @@ -1292,7 +1370,7 @@ function remount(resetCurrentTime?: boolean) { if ( resetCurrentTime ? props.currentChap && - progressRestored === `${props.currentSeason}/${props.currentChap}` + progressRestored === uidChap.value : true ) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion diff --git a/src/constants.ts b/src/constants.ts index 1d4415ac..a73fb4ff 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -30,6 +30,7 @@ export const playbackRates = [ ] export const DELAY_SAVE_VIEWING_PROGRESS = 20_000 // x4 6s +export const CONFIRMATION_TIME_IS_ACTUALLY_WATCHING = DELAY_SAVE_VIEWING_PROGRESS / 4 export const REGEXP_OLD_HOST_CURL = /animevietsub\.(?:tv|cc|me)/i From b293fb21ae3bcb34efecbed8441b74e79ce5f51c Mon Sep 17 00:00:00 2001 From: Tachibana Shin <118260404+tachib-shin@users.noreply.github.com> Date: Tue, 18 Apr 2023 06:51:18 +0000 Subject: [PATCH 2/2] remove old code --- src/components/BrtPlayer.vue | 33 +-------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/src/components/BrtPlayer.vue b/src/components/BrtPlayer.vue index ead6bf28..5ee7da90 100644 --- a/src/components/BrtPlayer.vue +++ b/src/components/BrtPlayer.vue @@ -677,8 +677,8 @@ import { } from "quasar" import { useMemoControl } from "src/composibles/memo-control" import { - CONFIRMATION_TIME_IS_ACTUALLY_WATCHING, C_URL, + CONFIRMATION_TIME_IS_ACTUALLY_WATCHING, DELAY_SAVE_VIEWING_PROGRESS, playbackRates, } from "src/constants" @@ -943,37 +943,6 @@ function onVideoCanPlay() { activeTime = Date.now() } -// eslint-disable-next-line functional/no-let -let seasonReady = false -watch( - [ - () => authStore.user, - () => props.currentSeason, - () => props.nameCurrentSeason, - () => props.poster, - ], - // eslint-disable-next-line camelcase - async ([user_data, currentSeason, seasonName, poster]) => { - seasonReady = false - if ( - // eslint-disable-next-line camelcase - !user_data || - !currentSeason || - typeof seasonName !== "string" || - !poster - ) - return - console.log("set new season poster %s", poster) - await historyStore.createSeason(currentSeason, { - poster, - seasonName, - name: props.name, - }) - seasonReady = true - }, - { immediate: true } -) - const seasonMetaCreated = new Set() async function createSeason(): Promise {