From b3b71c8ccf362657373dd6b3392a0d9c126661ca Mon Sep 17 00:00:00 2001 From: Alex Karpov Date: Fri, 20 Sep 2024 16:47:19 +0300 Subject: [PATCH 1/2] NAS-131130: Refactor `subscribeToInstalledAppsUpdates` in `installed-apps-store.service` --- .../store/installed-apps-store.service.ts | 167 +++++++++--------- 1 file changed, 85 insertions(+), 82 deletions(-) diff --git a/src/app/pages/apps/store/installed-apps-store.service.ts b/src/app/pages/apps/store/installed-apps-store.service.ts index e97e92517eb..074aba09399 100644 --- a/src/app/pages/apps/store/installed-apps-store.service.ts +++ b/src/app/pages/apps/store/installed-apps-store.service.ts @@ -68,88 +68,6 @@ export class InstalledAppsStore extends ComponentStore imple }); } - private handleRemovedApps(updatedAppName: string, allApps: AvailableApp[]): AvailableApp[] { - return allApps.map((app) => { - if (app.name === updatedAppName) { - return { ...app, installed: false } as AvailableApp; - } - return app; - }); - } - - private subscribeToInstalledAppsUpdates(): void { - if (this.installedAppsSubscription) { - return; - } - - // TODO: Messy. Refactor. - this.installedAppsSubscription = this.appsService.getInstalledAppsUpdates().pipe( - tap(() => this.patchState({ isLoading: true })), - tap((apiEvent: ApiEvent) => { - if (apiEvent.msg === IncomingApiMessageType.Removed) { - this.patchState((state: InstalledAppsState): InstalledAppsState => { - return { - ...state, - installedApps: state.installedApps.filter((app) => app.name !== apiEvent.id.toString()), - }; - }); - this.appsStore.patchState((state) => { - return { - ...state, - availableApps: this.handleRemovedApps(apiEvent.id as string, state.availableApps), - recommendedApps: this.handleRemovedApps(apiEvent.id as string, state.recommendedApps), - latestApps: this.handleRemovedApps(apiEvent.id as string, state.latestApps), - }; - }); - } - }), - filter((apiEvent) => { - if (apiEvent.msg === IncomingApiMessageType.Removed) { - this.patchState({ isLoading: false }); - } - return apiEvent.msg !== IncomingApiMessageType.Removed; - }), - tap((apiEvent) => { - const app = apiEvent.fields; - if (!app) { - return; - } - this.patchState((state: InstalledAppsState): InstalledAppsState => { - if (apiEvent.msg === IncomingApiMessageType.Added) { - return { - ...state, - installedApps: [...state.installedApps, app], - }; - } - return { - ...state, - installedApps: state.installedApps.map((installedApp) => { - if (installedApp.name === apiEvent.id) { - return { ...installedApp, ...app }; - } - return installedApp; - }), - }; - }); - - const updateApps = (appsToUpdate: AvailableApp[]): AvailableApp[] => appsToUpdate.map((availableApp) => { - return availableApp.name === app.id ? { ...availableApp, installed: true } : availableApp; - }); - - this.appsStore.patchState((state) => { - return { - ...state, - availableApps: updateApps(state.availableApps), - recommendedApps: updateApps(state.recommendedApps), - latestApps: updateApps(state.latestApps), - }; - }); - }), - tap(() => this.patchState({ isLoading: false })), - untilDestroyed(this), - ).subscribe(); - } - private loadInstalledApps(): Observable { return this.dockerStore.isLoading$.pipe( withLatestFrom(this.dockerStore.isDockerStarted$), @@ -177,4 +95,89 @@ export class InstalledAppsStore extends ComponentStore imple untilDestroyed(this), ); } + + private subscribeToInstalledAppsUpdates(): void { + if (this.installedAppsSubscription) { + return; + } + + this.installedAppsSubscription = this.appsService.getInstalledAppsUpdates().pipe( + tap((apiEvent: ApiEvent) => { + this.handleApiEvent(apiEvent); + this.patchState({ isLoading: false }); + }), + untilDestroyed(this), + ).subscribe(); + } + + private handleApiEvent(apiEvent: ApiEvent): void { + switch (apiEvent.msg) { + case IncomingApiMessageType.Removed: + this.handleRemovedEvent(apiEvent); + break; + case IncomingApiMessageType.Added: + case IncomingApiMessageType.Changed: + this.handleAddedOrUpdatedEvent(apiEvent); + break; + default: + console.error('Unknown API event type (installed-apps-store.service):', apiEvent.msg); + break; + } + } + + private handleRemovedEvent(apiEvent: ApiEvent): void { + const appId = apiEvent.id.toString(); + + this.patchState((state: InstalledAppsState): InstalledAppsState => ({ + ...state, + installedApps: state.installedApps.filter((app) => app.name !== appId), + })); + + const updateApps = (updatedAppName: string, allApps: AvailableApp[]): AvailableApp[] => { + return allApps.map((app) => { + if (app.name === updatedAppName) { + return { ...app, installed: false }; + } + return app; + }); + }; + + this.appsStore.patchState((state) => ({ + ...state, + availableApps: updateApps(appId, state.availableApps), + recommendedApps: updateApps(appId, state.recommendedApps), + latestApps: updateApps(appId, state.latestApps), + })); + } + + private handleAddedOrUpdatedEvent(apiEvent: ApiEvent): void { + const app = apiEvent.fields; + if (!app) { + return; + } + + this.patchState((state: InstalledAppsState): InstalledAppsState => { + if (apiEvent.msg === IncomingApiMessageType.Added) { + return { ...state, installedApps: [...state.installedApps, app] }; + } + + return { + ...state, + installedApps: state.installedApps.map( + (installedApp) => (installedApp.name === apiEvent.id ? { ...installedApp, ...app } : installedApp), + ), + }; + }); + + const updateApps = (apps: AvailableApp[]): AvailableApp[] => apps.map( + (availableApp) => (availableApp.name === app.id ? { ...availableApp, installed: true } : availableApp), + ); + + this.appsStore.patchState((state) => ({ + ...state, + availableApps: updateApps(state.availableApps), + recommendedApps: updateApps(state.recommendedApps), + latestApps: updateApps(state.latestApps), + })); + } } From 4494a11a1a21db5f2da2630f668853144c5aa2ec Mon Sep 17 00:00:00 2001 From: Alex Karpov Date: Thu, 26 Sep 2024 13:51:04 +0300 Subject: [PATCH 2/2] NAS-131130: PR update --- src/app/pages/apps/store/installed-apps-store.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/pages/apps/store/installed-apps-store.service.ts b/src/app/pages/apps/store/installed-apps-store.service.ts index 074aba09399..b6057fc075e 100644 --- a/src/app/pages/apps/store/installed-apps-store.service.ts +++ b/src/app/pages/apps/store/installed-apps-store.service.ts @@ -120,7 +120,7 @@ export class InstalledAppsStore extends ComponentStore imple this.handleAddedOrUpdatedEvent(apiEvent); break; default: - console.error('Unknown API event type (installed-apps-store.service):', apiEvent.msg); + console.error('Unknown API event type'); break; } }