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:
parent
6bf606b738
commit
2175fdc15f
@ -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() {
|
||||
|
||||
@ -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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user