From 9097ff5bc121c91fb7409f4242e135d6dd78651d Mon Sep 17 00:00:00 2001 From: 4gray Date: Thu, 21 May 2026 19:45:07 +0300 Subject: [PATCH] fix(pwa): align xtream recent clearing --- .../unified-recent-data.service.spec.ts | 8 ++++++-- .../collection/unified-recent-data.service.ts | 14 ++++++++++---- .../src/lib/with-recent-items.feature.spec.ts | 17 +++++++++++++++++ .../data-access/src/lib/with-recent-items.ts | 15 ++++----------- 4 files changed, 37 insertions(+), 17 deletions(-) diff --git a/libs/portal/shared/data-access/src/lib/collection/unified-recent-data.service.spec.ts b/libs/portal/shared/data-access/src/lib/collection/unified-recent-data.service.spec.ts index b570432a6..59526df28 100644 --- a/libs/portal/shared/data-access/src/lib/collection/unified-recent-data.service.spec.ts +++ b/libs/portal/shared/data-access/src/lib/collection/unified-recent-data.service.spec.ts @@ -403,13 +403,17 @@ describe('UnifiedRecentDataService', () => { value: undefined, configurable: true, }); - playlistsService.getAllPlaylists.mockReturnValue( + store.select.mockReturnValue( of([ { _id: 'xtream-1', title: 'Xtream PWA', serverUrl: 'https://xtream.example.com', - }, + } satisfies Partial, + ]) + ); + playlistsService.getAllPlaylists.mockReturnValue( + of([ { _id: 'm3u-1', title: 'M3U List', diff --git a/libs/portal/shared/data-access/src/lib/collection/unified-recent-data.service.ts b/libs/portal/shared/data-access/src/lib/collection/unified-recent-data.service.ts index de428b516..3111261bb 100644 --- a/libs/portal/shared/data-access/src/lib/collection/unified-recent-data.service.ts +++ b/libs/portal/shared/data-access/src/lib/collection/unified-recent-data.service.ts @@ -191,6 +191,16 @@ export class UnifiedRecentDataService { if (window.electron) { await this.dbService.clearGlobalRecentlyViewed(); + } else { + const allMeta = await this.getAllMeta(); + await Promise.all( + allMeta + .filter((playlist) => playlist._id) + .filter((playlist) => this.isXtreamPlaylist(playlist)) + .map((playlist) => + this.xtreamDataSource.clearRecentItems(playlist._id) + ) + ); } const playlists = (await firstValueFrom( this.playlistsService.getAllPlaylists() @@ -198,10 +208,6 @@ export class UnifiedRecentDataService { await Promise.all( playlists.map(async (playlist) => { - if (!window.electron && this.isXtreamPlaylist(playlist)) { - await this.xtreamDataSource.clearRecentItems(playlist._id); - } - if (this.isPlaylistBackedRecentPlaylist(playlist)) { const updatedPlaylist = await firstValueFrom( this.playlistsService.clearPlaylistRecentlyViewed( diff --git a/libs/portal/xtream/data-access/src/lib/with-recent-items.feature.spec.ts b/libs/portal/xtream/data-access/src/lib/with-recent-items.feature.spec.ts index d1d63d17a..ea2698fc8 100644 --- a/libs/portal/xtream/data-access/src/lib/with-recent-items.feature.spec.ts +++ b/libs/portal/xtream/data-access/src/lib/with-recent-items.feature.spec.ts @@ -335,4 +335,21 @@ describe('withRecentItems', () => { }), ]); }); + + it('removes recent items through the active data source in Electron mode', async () => { + store.removeRecentItem({ itemId: 3941697, playlistId: 'playlist-1' }); + await new Promise((resolve) => setTimeout(resolve, 0)); + + expect(dataSource.removeRecentItem).toHaveBeenCalledWith( + 3941697, + 'playlist-1' + ); + expect(databaseService.removeRecentItem).not.toHaveBeenCalled(); + expect(store.recentItems()).toEqual([ + expect.objectContaining({ + id: 3941697, + title: 'Krypton', + }), + ]); + }); }); diff --git a/libs/portal/xtream/data-access/src/lib/with-recent-items.ts b/libs/portal/xtream/data-access/src/lib/with-recent-items.ts index 5e7ed411b..8d7fda351 100644 --- a/libs/portal/xtream/data-access/src/lib/with-recent-items.ts +++ b/libs/portal/xtream/data-access/src/lib/with-recent-items.ts @@ -195,17 +195,10 @@ export const withRecentItems = function () { }>( pipe( switchMap(async ({ itemId, playlistId }) => { - if (window.electron) { - await dbService.removeRecentItem( - itemId, - playlistId - ); - } else { - await dataSource.removeRecentItem( - itemId, - playlistId - ); - } + await dataSource.removeRecentItem( + itemId, + playlistId + ); // Reload recent items to update UI const items = await dataSource.getRecentItems(playlistId);