@@ -81,7 +81,7 @@ export default defineComponent({
8181 videoSearchQuery : '' ,
8282
8383 promptOpen : false ,
84- deletedPlaylistItemIds : [ ] ,
84+ toBeDeletedPlaylistItemIds : [ ] ,
8585 // Present = shown
8686 undoToastAbortController : null ,
8787 }
@@ -193,7 +193,7 @@ export default defineComponent({
193193 const playlistItems = this . getPlaylistItemsWithDuration ( )
194194 return getSortedPlaylistItems ( playlistItems , this . sortOrder , this . currentLocale )
195195 }
196- return getSortedPlaylistItems ( this . playlistItems , this . sortOrder , this . currentLocale )
196+ return getSortedPlaylistItems ( this . shownPlaylistItems , this . sortOrder , this . currentLocale )
197197 } ,
198198 visiblePlaylistItems : function ( ) {
199199 if ( ! this . isUserPlaylistRequested ) {
@@ -245,14 +245,34 @@ export default defineComponent({
245245 return this . sortByValues
246246 } ,
247247 totalPlaylistDuration ( ) {
248- const totalSeconds = this . playlistItems . reduce ( ( acc , video ) => {
248+ const totalSeconds = this . shownPlaylistItems . reduce ( ( acc , video ) => {
249249 if ( typeof video . lengthSeconds !== 'number' ) {
250250 return NaN
251251 }
252252 return acc + video . lengthSeconds
253253 } , 0 )
254254 return totalSeconds
255255 } ,
256+ noPlaylistItemsPendingDeletion ( ) {
257+ return this . toBeDeletedPlaylistItemIds . length === 0
258+ } ,
259+ shownPlaylistItems ( ) {
260+ if ( this . noPlaylistItemsPendingDeletion ) {
261+ return this . playlistItems
262+ }
263+
264+ return this . playlistItems . filter ( ( v ) => ! this . toBeDeletedPlaylistItemIds . includes ( v . playlistItemId ) )
265+ } ,
266+ shownPlaylistItemCount ( ) {
267+ return this . shownPlaylistItems . length
268+ } ,
269+ shownVideoCount ( ) {
270+ if ( this . isUserPlaylistRequested ) {
271+ return this . shownPlaylistItemCount
272+ }
273+
274+ return this . videoCount
275+ } ,
256276 } ,
257277 watch : {
258278 $route ( ) {
@@ -273,14 +293,16 @@ export default defineComponent({
273293 // Re-fetch from local store when current user playlist updated
274294 this . getPlaylistInfoDebounce ( )
275295 } ,
276- selectedUserPlaylistVideoCount ( ) {
296+ async selectedUserPlaylistVideoCount ( ) {
277297 // Monitoring `selectedUserPlaylistVideos` makes this function called
278298 // Even when the same array object is returned
279299 // So length is monitored instead
280300 // Assuming in user playlist video cannot be swapped without length change
281301
282302 // Re-fetch from local store when current user playlist videos updated
283- this . getPlaylistInfoDebounce ( )
303+ // MUST NOT use `getPlaylistInfoDebounce` as it will cause delay in data update
304+ // Causing deleted videos to reappear for one frame
305+ this . getPlaylistInfo ( )
284306 } ,
285307 } ,
286308 created : function ( ) {
@@ -433,7 +455,6 @@ export default defineComponent({
433455 this . firstVideoPlaylistItemId = ''
434456 }
435457 this . viewCount = 0
436- this . videoCount = playlist . videos . length
437458 const dateString = new Date ( playlist . lastUpdatedAt )
438459 this . lastUpdated = dateString . toLocaleDateString ( this . currentLocale , { year : 'numeric' , month : 'short' , day : 'numeric' } )
439460 this . channelName = ''
@@ -453,7 +474,7 @@ export default defineComponent({
453474 } ,
454475
455476 getPlaylistItemsWithDuration ( ) {
456- const modifiedPlaylistItems = deepCopy ( this . playlistItems )
477+ const modifiedPlaylistItems = deepCopy ( this . shownPlaylistItems )
457478 let anyVideoMissingDuration = false
458479 modifiedPlaylistItems . forEach ( video => {
459480 if ( videoDurationPresent ( video ) ) { return }
@@ -491,10 +512,10 @@ export default defineComponent({
491512 this . isLoadingMore = true
492513
493514 nextTick ( ( ) => {
494- if ( this . userPlaylistVisibleLimit + 100 < this . videoCount ) {
515+ if ( this . userPlaylistVisibleLimit + 100 < this . shownVideoCount ) {
495516 this . userPlaylistVisibleLimit += 100
496517 } else {
497- this . userPlaylistVisibleLimit = this . videoCount
518+ this . userPlaylistVisibleLimit = this . shownVideoCount
498519 }
499520
500521 this . isLoadingMore = false
@@ -599,33 +620,26 @@ export default defineComponent({
599620
600621 removeVideoFromPlaylist : function ( videoId , playlistItemId ) {
601622 try {
602- const playlistItems = [ ] . concat ( this . playlistItems )
603- const tempPlaylistItems = [ ] . concat ( this . playlistItems )
604-
605623 const videoIndex = this . playlistItems . findIndex ( ( video ) => {
606624 return video . videoId === videoId && video . playlistItemId === playlistItemId
607625 } )
608626
609627 if ( videoIndex !== - 1 ) {
610- this . deletedPlaylistItemIds . push ( this . playlistItems [ videoIndex ] . playlistItemId )
611- playlistItems . splice ( videoIndex , 1 )
612- this . playlistItems = playlistItems
613- this . videoCount = playlistItems . length
628+ this . toBeDeletedPlaylistItemIds . push ( this . playlistItems [ videoIndex ] . playlistItemId )
614629
615630 // Only show toast when no existing toast shown
616631 if ( this . undoToastAbortController == null ) {
617632 this . undoToastAbortController = new AbortController ( )
633+ const timeoutMs = 5 * 1000
618634 const actualRemoveVideosTimeout = setTimeout ( ( ) => {
619635 this . removeToBeDeletedVideosSometimes ( )
620- } , 5000 )
636+ } , timeoutMs )
621637 showToast (
622638 this . $t ( 'User Playlists.SinglePlaylistView.Toast["Video has been removed. Click here to undo."]' ) ,
623- 5000 ,
639+ timeoutMs ,
624640 ( ) => {
625- this . playlistItems = tempPlaylistItems
626- this . videoCount = tempPlaylistItems . length
627641 clearTimeout ( actualRemoveVideosTimeout )
628- this . deletedPlaylistItemIds = [ ]
642+ this . toBeDeletedPlaylistItemIds = [ ]
629643 this . undoToastAbortController = null
630644 } ,
631645 this . undoToastAbortController . signal ,
@@ -638,15 +652,15 @@ export default defineComponent({
638652 }
639653 } ,
640654
641- removeToBeDeletedVideosSometimes ( ) {
655+ async removeToBeDeletedVideosSometimes ( ) {
642656 if ( this . isLoading ) { return }
643657
644- if ( this . deletedPlaylistItemIds . length > 0 ) {
645- this . removeVideos ( {
658+ if ( this . toBeDeletedPlaylistItemIds . length > 0 ) {
659+ await this . removeVideos ( {
646660 _id : this . playlistId ,
647- playlistItemIds : this . deletedPlaylistItemIds ,
661+ playlistItemIds : this . toBeDeletedPlaylistItemIds ,
648662 } )
649- this . deletedPlaylistItemIds = [ ]
663+ this . toBeDeletedPlaylistItemIds = [ ]
650664 this . undoToastAbortController ?. abort ( )
651665 this . undoToastAbortController = null
652666 }
0 commit comments