From 2175fdc15f6ea89f77cfef3ab30fc4064b7f556f Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 6 Aug 2024 16:23:39 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20UpdateService=20mit=20Z=C3=A4hler-?= =?UTF-8?q?=C3=84nderungs-Listener=20erweitern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `UpdateEvent`-Enum für Ereignistypen hinzugefügt. - `countChangeListeners` eingeführt, um zählerbezogene Änderungen zu verwalten. - Methoden aktualisiert, um `countChangeListeners` auszulösen und Statusänderungen effizienter zu handhaben. - Methodennamen auf `executeCountChangeListeners` geändert. --- .../components/nav-menu/nav-menu.component.ts | 10 ++--- .../src/app/services/update.service.ts | 39 +++++++++++-------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/DigitalData.UserManager.API/ClientApp/user_manager_ui/src/app/components/nav-menu/nav-menu.component.ts b/DigitalData.UserManager.API/ClientApp/user_manager_ui/src/app/components/nav-menu/nav-menu.component.ts index ba9bd64..bed1ab5 100644 --- a/DigitalData.UserManager.API/ClientApp/user_manager_ui/src/app/components/nav-menu/nav-menu.component.ts +++ b/DigitalData.UserManager.API/ClientApp/user_manager_ui/src/app/components/nav-menu/nav-menu.component.ts @@ -8,8 +8,8 @@ import { ColorModeBttnComponent } from '../common/color-mode-bttn/color-mode-btt import { MatIconModule } from '@angular/material/icon'; import { RefreshService } from '../../services/refresh.service'; import { CreationService } from '../../services/creation.service'; -import { UpdateService } from '../../services/update.service'; -import {MatBadgeModule} from '@angular/material/badge'; +import { UpdateService, UpdateEvent } from '../../services/update.service'; +import { MatBadgeModule } from '@angular/material/badge'; @Component({ standalone: true, @@ -26,16 +26,16 @@ export class NavMenuComponent { updateActCount: number; - constructor(private dialog: MatDialog, private authService: AuthenticationService, public refreshService: RefreshService, public creationService : CreationService, public updateService: UpdateService) { + constructor(private dialog: MatDialog, private authService: AuthenticationService, public refreshService: RefreshService, public creationService: CreationService, public updateService: UpdateService) { this.authService.isAuthenticated().then().catch() this.updateActCount = this.updateService.totalCount; - this.updateService.addChangeListener(() => { + this.updateService.addChangeListener(UpdateEvent.CountChange, () => { this.updateActCount = updateService.totalCount; }) } get isDarkTheme(): boolean { - return (typeof window !== 'undefined')?(localStorage.getItem('theme') === 'dark'):true; + return (typeof window !== 'undefined') ? (localStorage.getItem('theme') === 'dark') : true; } collapse() { diff --git a/DigitalData.UserManager.API/ClientApp/user_manager_ui/src/app/services/update.service.ts b/DigitalData.UserManager.API/ClientApp/user_manager_ui/src/app/services/update.service.ts index 95e878b..48324e2 100644 --- a/DigitalData.UserManager.API/ClientApp/user_manager_ui/src/app/services/update.service.ts +++ b/DigitalData.UserManager.API/ClientApp/user_manager_ui/src/app/services/update.service.ts @@ -26,30 +26,29 @@ export class UpdateService { return this.totalCount > 0; } - //add - remove + //add - remove - executeA setAsync(key: string, action: () => Promise): void { const keyWasPresent = this.async_actions.hasOwnProperty(key); this.async_actions[key] = action; - if (!keyWasPresent) - this.executeAllChangeListeners(); + this.executeCountChangeListeners(); } set(key: string, action: () => void): void { const keyWasPresent = this.actions.hasOwnProperty(key); this.actions[key] = action; - if (!keyWasPresent) - this.executeAllChangeListeners(); + this.executeCountChangeListeners(); } removeAll(): void { - this.async_actions = {}; - this.actions = {}; - this.executeAllChangeListeners(); + if (this.any) { + this.async_actions = {}; + this.actions = {}; + this.executeCountChangeListeners(); + } } - //execution async executeAll(): Promise { await Promise.all(Object.values(this.async_actions).map(action => action())); Object.values(this.actions).forEach(action => action()); @@ -57,17 +56,25 @@ export class UpdateService { } //change listeners - private changeListeners: Array<() => void> = new Array(); + //count + private countChangeListeners: Array<() => void> = new Array(); + private executeCountChangeListeners() { + this.countChangeListeners.forEach(e => e()); + } - addChangeListener(action: () => void) { - this.changeListeners.push(action) + addChangeListener(eventType: UpdateEvent, action: () => void) { + switch (eventType) { + case UpdateEvent.CountChange: + this.countChangeListeners.push(action) + break; + } } removeAllChangeListeners() { - this.changeListeners = [] + this.countChangeListeners = [] } +} - private executeAllChangeListeners() { - this.changeListeners.forEach(e => e()); - } +export enum UpdateEvent { + CountChange } \ No newline at end of file