Skip to content

Commit

Permalink
#379 spinner signals
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarc committed Mar 29, 2024
1 parent c919e70 commit cadcd66
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
9 changes: 3 additions & 6 deletions frontend/src/app/shared/spinner/spinner.component.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { AsyncPipe } from '@angular/common';
import { inject } from '@angular/core';
import { ChangeDetectionStrategy } from '@angular/core';
import { Component } from '@angular/core';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { debounceTime } from 'rxjs/operators';
import { SpinnerService } from './spinner.service';

@Component({
selector: 'kpn-spinner',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
@if (showSpinner$ | async) {
@if (service.showSpinner()) {
<mat-spinner diameter="40" />
}
`,
standalone: true,
imports: [MatProgressSpinnerModule, AsyncPipe],
imports: [MatProgressSpinnerModule],
})
export class SpinnerComponent {
private readonly spinnerService = inject(SpinnerService);
protected readonly showSpinner$ = this.spinnerService.spinnerState$.pipe(debounceTime(300));
protected readonly service = inject(SpinnerService);
}
12 changes: 4 additions & 8 deletions frontend/src/app/shared/spinner/spinner.service.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { Injectable } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { List } from 'immutable';
import { finalize } from 'rxjs';
import { of } from 'rxjs';
import { Observable } from 'rxjs';
import { BehaviorSubject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
import { concatMap } from 'rxjs/operators';
import { tap } from 'rxjs/operators';

@Injectable({
providedIn: 'root',
})
export class SpinnerService {
readonly spinnerState$: Observable<boolean>;
private readonly _spinnerState$ = new BehaviorSubject<boolean>(false);
private activeActions: List<string> = List();

constructor() {
this.spinnerState$ = this._spinnerState$.asObservable();
}
private readonly _spinnerState$ = new BehaviorSubject<boolean>(false);
readonly showSpinner = toSignal(this._spinnerState$.pipe(debounceTime(300)));

showUntilCompleted<T>(obs$: Observable<T>, action: string): Observable<T> {
return of(null).pipe(
Expand All @@ -32,14 +30,12 @@ export class SpinnerService {
if (this._spinnerState$.value !== true) {
this._spinnerState$.next(true);
}
// console.log(`spinner start ${action} - activeActions = ${this.activeActions}, spinnerState=${this._spinnerState}`);
}

end(action: string): void {
this.activeActions = this.activeActions.filter((a) => a !== action);
if (this.activeActions.isEmpty() && this._spinnerState$.value !== false) {
this._spinnerState$.next(false);
}
// console.log(`spinner end ${action} - activeActions = ${this.activeActions}, spinnerState=${this._spinnerState}`);
}
}

0 comments on commit cadcd66

Please sign in to comment.