refactor: UpdateService mit Zähler-Änderungs-Listener erweitern

- `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.
This commit is contained in:
Developer 02 2024-08-06 16:23:39 +02:00
parent 6bf606b738
commit 2175fdc15f
2 changed files with 28 additions and 21 deletions

View File

@ -8,8 +8,8 @@ import { ColorModeBttnComponent } from '../common/color-mode-bttn/color-mode-btt
import { MatIconModule } from '@angular/material/icon'; import { MatIconModule } from '@angular/material/icon';
import { RefreshService } from '../../services/refresh.service'; import { RefreshService } from '../../services/refresh.service';
import { CreationService } from '../../services/creation.service'; import { CreationService } from '../../services/creation.service';
import { UpdateService } from '../../services/update.service'; import { UpdateService, UpdateEvent } from '../../services/update.service';
import {MatBadgeModule} from '@angular/material/badge'; import { MatBadgeModule } from '@angular/material/badge';
@Component({ @Component({
standalone: true, standalone: true,
@ -26,16 +26,16 @@ export class NavMenuComponent {
updateActCount: number; 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.authService.isAuthenticated().then().catch()
this.updateActCount = this.updateService.totalCount; this.updateActCount = this.updateService.totalCount;
this.updateService.addChangeListener(() => { this.updateService.addChangeListener(UpdateEvent.CountChange, () => {
this.updateActCount = updateService.totalCount; this.updateActCount = updateService.totalCount;
}) })
} }
get isDarkTheme(): boolean { get isDarkTheme(): boolean {
return (typeof window !== 'undefined')?(localStorage.getItem('theme') === 'dark'):true; return (typeof window !== 'undefined') ? (localStorage.getItem('theme') === 'dark') : true;
} }
collapse() { collapse() {

View File

@ -26,30 +26,29 @@ export class UpdateService {
return this.totalCount > 0; return this.totalCount > 0;
} }
//add - remove //add - remove - executeA
setAsync(key: string, action: () => Promise<void>): void { setAsync(key: string, action: () => Promise<void>): void {
const keyWasPresent = this.async_actions.hasOwnProperty(key); const keyWasPresent = this.async_actions.hasOwnProperty(key);
this.async_actions[key] = action; this.async_actions[key] = action;
if (!keyWasPresent) if (!keyWasPresent)
this.executeAllChangeListeners(); this.executeCountChangeListeners();
} }
set(key: string, action: () => void): void { set(key: string, action: () => void): void {
const keyWasPresent = this.actions.hasOwnProperty(key); const keyWasPresent = this.actions.hasOwnProperty(key);
this.actions[key] = action; this.actions[key] = action;
if (!keyWasPresent) if (!keyWasPresent)
this.executeAllChangeListeners(); this.executeCountChangeListeners();
} }
removeAll(): void { removeAll(): void {
this.async_actions = {}; if (this.any) {
this.actions = {}; this.async_actions = {};
this.executeAllChangeListeners(); this.actions = {};
this.executeCountChangeListeners();
}
} }
//execution
async executeAll(): Promise<void> { async executeAll(): Promise<void> {
await Promise.all(Object.values(this.async_actions).map(action => action())); await Promise.all(Object.values(this.async_actions).map(action => action()));
Object.values(this.actions).forEach(action => action()); Object.values(this.actions).forEach(action => action());
@ -57,17 +56,25 @@ export class UpdateService {
} }
//change listeners //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) { addChangeListener(eventType: UpdateEvent, action: () => void) {
this.changeListeners.push(action) switch (eventType) {
case UpdateEvent.CountChange:
this.countChangeListeners.push(action)
break;
}
} }
removeAllChangeListeners() { removeAllChangeListeners() {
this.changeListeners = [] this.countChangeListeners = []
} }
}
private executeAllChangeListeners() { export enum UpdateEvent {
this.changeListeners.forEach(e => e()); CountChange
}
} }