Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

history function is calling immediately on play #49

Merged
merged 2 commits into from
Apr 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 92 additions & 14 deletions src/components/BrtPlayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -737,6 +738,11 @@ const props = defineProps<{
fetchSeason: (season: string) => Promise<void>
progressWatchStore: ProgressWatchStore
}>()
const uidChap = computed(() => {
const uid = `${props.currentSeason}/${props.currentChap ?? ""}` // 255 byte

return uid
})

// ===== setup effect =====

Expand Down Expand Up @@ -842,7 +848,7 @@ watch(
if ((err as Error)?.message !== "NOT_RESET") console.error(err)
}

progressRestored = `${currentSeason}/${currentChap}`
progressRestored = uidChap.value
}
},
{ immediate: true }
Expand Down Expand Up @@ -968,6 +974,33 @@ watch(
{ immediate: true }
)

const seasonMetaCreated = new Set<string>()

async function createSeason(): Promise<boolean> {
// 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",
Expand All @@ -978,6 +1011,38 @@ const emit = defineEmits<{
}
): void
}>()

const storeFirstSaving = new Set<string>()
{
// 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(
Expand All @@ -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
Expand All @@ -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 &&
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down