feat: continue where left off on batches

fix: subtitle errors, worker errors
This commit is contained in:
ThaUnknown 2022-08-18 20:01:48 +02:00
parent 723ef9d8d8
commit 6f1a19ab47
5 changed files with 27 additions and 19 deletions

View file

@ -1,6 +1,6 @@
{
"name": "Miru",
"version": "3.0.6",
"version": "3.1.0",
"author": "ThaUnknown_ <ThaUnknown@users.noreply.github.com>",
"main": "src/index.js",
"homepage": "https://github.com/ThaUnknown/miru#readme",

View file

@ -70,7 +70,8 @@ function handleMedia ({ media, episode, parseObject }) {
}
async function handleFiles (files) {
let videoFiles = []
if (!files?.length) return processed.set(files)
const videoFiles = []
const otherFiles = []
for (const file of files) {
if (videoRx.test(file.name)) {
@ -88,24 +89,32 @@ async function handleFiles (files) {
const nowPlaying = get(media)
if (nowPlaying?.media) videoFiles = videoFiles.filter(file => file.media?.media?.id === nowPlaying.media.id)
const filtered = nowPlaying?.media && videoFiles.filter(file => file.media?.media?.id && file.media?.media?.id === nowPlaying.media.id)
videoFiles.sort((a, b) => a.media.episode - b.media.episode)
const result = (filtered?.length && filtered) || videoFiles
if (!videoFiles.length) {
processed.set(files)
result.sort((a, b) => a.media.episode - b.media.episode)
processed.set([...result, ...otherFiles])
await tick()
if (nowPlaying?.episode && filtered.length) {
let file = videoFiles.find(({ media }) => media.episode === nowPlaying.episode)
if (!file) file = videoFiles.find(({ media }) => media.episode === 1)
playFile(file || 0)
} else {
processed.set([...videoFiles, ...otherFiles])
if (nowPlaying?.episode) {
let file = videoFiles.find(({ media }) => media.episode === nowPlaying.episode)
if (!file) file = videoFiles.find(({ media }) => media.episode === 1)
await tick()
playFile(file || 0)
}
const max = highest(videoFiles, (file) => file?.media?.media?.id)
const res = max.media?.media && result.find(({ media }) => media.episode === (max.media.media.mediaListEntry?.progress + 1 || 1) && media.media?.id === max.media.media?.id)
playFile(res || videoFiles.find(({ media }) => media.episode === 1) || 0)
}
}
const highest = (arr = [], mapfn = () => {}) => arr.reduce((acc, el) => {
const mapped = mapfn(el)
acc.sums[mapped] = (acc.sums[mapped] || 0) + 1
acc.max = acc.sums[mapfn(acc.max)] > acc.sums[mapped] ? acc.max : el
return acc
}, { sums: {} }).max
files.subscribe((files = []) => {
handleFiles(files)
return noop

View file

@ -24,7 +24,7 @@ w2gEmitter.on('setindex', ({ detail }) => {
})
export function playFile (file) {
if (typeof value === 'number') {
if (!isNaN(file)) {
handleCurrent(videos?.[file])
} else {
handleCurrent(file)
@ -153,7 +153,6 @@ function updateFiles (files) {
if (files?.length) {
videos = files.filter(file => videoRx.test(file.name))
if (videos?.length) {
handleCurrent(videos[0])
if (subs) {
subs.files = files || []
subs.findSubtitleFiles(current)
@ -775,7 +774,7 @@ function toggleDropdown ({ target }) {
let completed = false
function checkCompletion () {
if (!completed && duration && video?.readyState && duration - 180 < currentTime) {
if (!completed && duration && currentTime && video?.readyState && duration - 180 < currentTime) {
if (media?.media?.episodes || media?.media?.nextAiringEpisode?.episode) {
if (media.media.episodes || media.media.nextAiringEpisode?.episode > media.episode) {
completed = true

View file

@ -50,7 +50,7 @@ export default class Subtitles {
const { subtitle, trackNumber } = detail
if (this.selected) {
const string = JSON.stringify(subtitle)
if (!this._tracksString[trackNumber]?.has(string)) {
if (this._tracksString[trackNumber] && !this._tracksString[trackNumber].has(string)) {
this._tracksString[trackNumber].add(string)
const assSub = this.constructSub(subtitle, this.headers[trackNumber].type !== 'ass', this.tracks[trackNumber].length)
this.tracks[trackNumber].push(assSub)

View file

@ -110,7 +110,7 @@ class TorrentClient extends WebTorrent {
this.parsed = false
if (data) {
this.current = this?.get(data.data.infoHash)?.files.find(file => file.path === data.data.path)
if (this.current.name.endsWith('.mkv')) {
if (this.current?.name.endsWith('.mkv')) {
if (this.current.done) this.parseSubtitles()
this.current.on('done', this.parseSubtitles.bind(this))
this.parseFonts(this.current)