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