feat: UpdateService mit Änderungs-Listenern und zusätzlichen Funktionen erweitern
- Getter `any` hinzugefügt, um zu überprüfen, ob Aktionen vorhanden sind. - Methoden `addAsync` und `add` geändert, um Änderungs-Listener nach dem Hinzufügen von Aktionen zu benachrichtigen. - `changeListeners`-Array implementiert mit Methoden zum Hinzufügen und Entfernen von Listenern. - `executeAllChangeListeners` hinzugefügt, um alle registrierten Änderungs-Listener auszulösen. - Methode `removeAll` aktualisiert, um Änderungs-Listener nach dem Leeren der Aktionen zu benachrichtigen.
This commit is contained in:
parent
2eb107cc0a
commit
0fe9cb7126
@ -1,4 +1,5 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Action } from 'rxjs/internal/scheduler/Action';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@ -10,33 +11,57 @@ export class UpdateService {
|
||||
private async_actions: Array<() => Promise<void>> = [];
|
||||
private actions: Array<() => void> = [];
|
||||
|
||||
get asyncCount() {
|
||||
get asyncCount(): number {
|
||||
return this.async_actions.length;
|
||||
}
|
||||
|
||||
get syncCount() {
|
||||
get syncCount(): number {
|
||||
return this.actions.length;
|
||||
}
|
||||
|
||||
get totalCount() {
|
||||
get totalCount(): number {
|
||||
return this.asyncCount + this.syncCount;
|
||||
}
|
||||
|
||||
get any(): boolean {
|
||||
return this.totalCount > 0;
|
||||
}
|
||||
|
||||
//add - remove
|
||||
addAsync(action: () => Promise<void>): void {
|
||||
this.async_actions.push(action);
|
||||
this.executeAllChangeListeners();
|
||||
}
|
||||
|
||||
add(action: () => void): void {
|
||||
this.actions.push(action);
|
||||
this.executeAllChangeListeners();
|
||||
}
|
||||
|
||||
removeAll(): void {
|
||||
this.async_actions = [];
|
||||
this.executeAllChangeListeners();
|
||||
}
|
||||
|
||||
//execution
|
||||
async executeAll(): Promise<void> {
|
||||
await Promise.all(this.async_actions.map(action => action()));
|
||||
this.actions.forEach(action => action());
|
||||
this.removeAll();
|
||||
}
|
||||
|
||||
//change listeners
|
||||
private changeListeners: Array<(updateService: UpdateService) => void> = new Array();
|
||||
|
||||
addChangeListener(action: (updateService: UpdateService) => void) {
|
||||
this.changeListeners.push(action)
|
||||
}
|
||||
|
||||
removeAllChangeListeners() {
|
||||
this.changeListeners = []
|
||||
}
|
||||
|
||||
private executeAllChangeListeners() {
|
||||
this.changeListeners.forEach(e => e(this));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user