From f932b13812ebbb7465e05e953746f59fc09c7228 Mon Sep 17 00:00:00 2001 From: ThrRip Date: Thu, 19 Dec 2024 06:13:23 +0800 Subject: [PATCH] admin: Refactor the change publishing process --- packages/admin/app/pages/playlist.vue | 50 ++++++++++++--------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/packages/admin/app/pages/playlist.vue b/packages/admin/app/pages/playlist.vue index 25ab32c..e03a5c4 100644 --- a/packages/admin/app/pages/playlist.vue +++ b/packages/admin/app/pages/playlist.vue @@ -99,33 +99,29 @@ function backendPlaylistPublishChanges (changesIds: Set) { }) ) - function cleanup (changesId: Song['$id'], newId?: Song['$id']) { + function succeed (changesId: Song['$id'], newId?: Song['$id']) { + const publishIndex = backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId) // @ts-expect-error - backendPlaylistPublishChangesState.value[ - backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId) - ].state = 'succeeded' - if (newId) { - // @ts-expect-error - backendPlaylistPublishChangesState.value[ - backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId) - ].newId = newId - } - else { - // @ts-expect-error - backendPlaylistPublishChangesState.value[ - backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId) - ].old = backendPlaylist.value.find(song => song.$id === changesId) - } + backendPlaylistPublishChangesState.value[publishIndex].state = 'succeeded' + // @ts-expect-error + if (newId) { backendPlaylistPublishChangesState.value[publishIndex].newId = newId } } function fail (changesId: Song['$id'], error: AppwriteException) { - const changesPublishStateIndex = backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId) + const publishIndex = backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId) // @ts-expect-error - backendPlaylistPublishChangesState.value[changesPublishStateIndex].state = 'failed' + backendPlaylistPublishChangesState.value[publishIndex].state = 'failed' // @ts-expect-error - backendPlaylistPublishChangesState.value[changesPublishStateIndex].error = error + backendPlaylistPublishChangesState.value[publishIndex].error = error } - function backendPlaylistRefreshIfAllPublishingFinished () { - if (backendPlaylistPublishChangesState.value.find(_ => _.state === 'processing')) { return } + function cleanup (changesId: Song['$id']) { + const publishIndex = backendPlaylistPublishChangesState.value.findIndex(_ => _.$id === changesId) + // @ts-expect-error + if (!backendPlaylistPublishChangesState.value[publishIndex].newId) { + // @ts-expect-error + backendPlaylistPublishChangesState.value[publishIndex].old = + backendPlaylist.value.find(song => song.$id === changesId) + } + if (backendPlaylistPublishChangesState.value.findIndex(_ => _.state === 'processing') !== -1) { return } backendPlaylistRefresh() .then(() => { backendPlaylistPublishChangesState.value.forEach((_, index) => { @@ -151,8 +147,8 @@ function backendPlaylistPublishChanges (changesIds: Set) { delete changes.$id backendDatabases.createDocument('home', 'playlist', ID.unique(), changes, [Permission.read(Role.any())]) // Save the ID generated by the backend for correct identifying of changes after the final refresh - .then(res => cleanup(changesId, res.$id), err => fail(changesId, err)) - .finally(backendPlaylistRefreshIfAllPublishingFinished) + .then(res => succeed(changesId, res.$id), err => fail(changesId, err)) + .finally(() => cleanup(changesId)) } else @@ -160,16 +156,16 @@ function backendPlaylistPublishChanges (changesIds: Set) { if (backendPlaylist.value.find(song => song.$id === changes.$id) && Object.keys(changes).length > 1) { delete changes.$id backendDatabases.updateDocument('home', 'playlist', changesId, changes) - .then(() => cleanup(changesId), err => fail(changesId, err)) - .finally(backendPlaylistRefreshIfAllPublishingFinished) + .then(() => succeed(changesId), err => fail(changesId, err)) + .finally(() => cleanup(changesId)) } else // Delete song if (backendPlaylist.value.find(song => song.$id === changes.$id) && Object.keys(changes).length === 1) { backendDatabases.deleteDocument('home', 'playlist', changesId) - .then(() => cleanup(changesId), err => fail(changesId, err)) - .finally(backendPlaylistRefreshIfAllPublishingFinished) + .then(() => succeed(changesId), err => fail(changesId, err)) + .finally(() => cleanup(changesId)) } }) }