refactor: UpdateService auf schlüsselbasierte Aktionen umstellen

- Arrays durch schlüsselbasierte Objekte für asynchrone und synchrone Aktionen ersetzt.
- Methoden aktualisiert, um Schlüssel zur Verwaltung von Aktionen zu verwenden und Änderungs-Listener auszulösen.
- `executeAll` angepasst, um Aktionen aus Objekten zu verarbeiten.
This commit is contained in:
Developer 02 2024-08-06 16:09:01 +02:00
parent e00c113bee
commit 6bf606b738

View File

@ -1,5 +1,4 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Action } from 'rxjs/internal/scheduler/Action';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -8,15 +7,15 @@ export class UpdateService {
constructor() { } constructor() { }
private async_actions: Array<() => Promise<void>> = []; private async_actions: { [key: string]: () => Promise<void> } = {};
private actions: Array<() => void> = []; private actions: { [key: string]: () => void } = {};
get asyncCount(): number { get asyncCount(): number {
return this.async_actions.length; return Object.keys(this.async_actions).length;
} }
get syncCount(): number { get syncCount(): number {
return this.actions.length; return Object.keys(this.actions).length;
} }
get totalCount(): number { get totalCount(): number {
@ -28,25 +27,32 @@ export class UpdateService {
} }
//add - remove //add - remove
addAsync(action: () => Promise<void>): void { setAsync(key: string, action: () => Promise<void>): void {
this.async_actions.push(action); const keyWasPresent = this.async_actions.hasOwnProperty(key);
this.executeAllChangeListeners(); this.async_actions[key] = action;
if (!keyWasPresent)
this.executeAllChangeListeners();
} }
add(action: () => void): void { set(key: string, action: () => void): void {
this.actions.push(action); const keyWasPresent = this.actions.hasOwnProperty(key);
this.executeAllChangeListeners(); this.actions[key] = action;
if (!keyWasPresent)
this.executeAllChangeListeners();
} }
removeAll(): void { removeAll(): void {
this.async_actions = []; this.async_actions = {};
this.actions = {};
this.executeAllChangeListeners(); this.executeAllChangeListeners();
} }
//execution //execution
async executeAll(): Promise<void> { async executeAll(): Promise<void> {
await Promise.all(this.async_actions.map(action => action())); await Promise.all(Object.values(this.async_actions).map(action => action()));
this.actions.forEach(action => action()); Object.values(this.actions).forEach(action => action());
this.removeAll(); this.removeAll();
} }