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 { 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() {

View File

@ -26,30 +26,29 @@ export class UpdateService {
return this.totalCount > 0;
}
//add - remove
//add - remove - executeA
setAsync(key: string, action: () => Promise<void>): 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<void> {
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
}