-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV: add debounce to component spinner
- Loading branch information
1 parent
a094ce6
commit ccfc777
Showing
2 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
webgui/webapp/src/app/components/component-spinner/component-spinner.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 45 additions & 2 deletions
47
webgui/webapp/src/app/components/component-spinner/component-spinner.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,55 @@ | ||
import { Component, Input } from '@angular/core'; | ||
import { AsyncPipe } from '@angular/common'; | ||
import { Component, Input, OnDestroy, OnInit } from '@angular/core'; | ||
import { OnChanges, SimpleChanges } from '@angular/core'; | ||
import { | ||
debounce, | ||
distinctUntilChanged, | ||
map, | ||
Observable, | ||
pairwise, | ||
startWith, | ||
Subject, | ||
timer, | ||
} from 'rxjs'; | ||
|
||
@Component({ | ||
selector: 'app-component-spinner', | ||
templateUrl: './component-spinner.component.html', | ||
styleUrls: ['./component-spinner.component.scss'], | ||
imports: [AsyncPipe], | ||
standalone: true, | ||
}) | ||
export class ComponentSpinnerComponent { | ||
export class ComponentSpinnerComponent implements OnChanges, OnInit, OnDestroy { | ||
@Input() | ||
loading: boolean = false; | ||
localLoading: Observable<boolean> = new Observable<boolean>(); | ||
private loadingSubject = new Subject<boolean>(); | ||
|
||
ngOnChanges(changes: SimpleChanges): void { | ||
if (changes['loading']) { | ||
this.loadingSubject.next(this.loading); | ||
} | ||
} | ||
|
||
/** | ||
* Lifecycle hook that is called after data-bound properties of a directive are initialized. | ||
* Initializes the localLoading observable by subscribing to the loadingSubject observable. | ||
* | ||
* The localLoading observable: | ||
* - Starts with a value of false. | ||
* - Emits values only when they change. | ||
* - Emits a value after a debounce period, which is 1000ms if the previous value was true, otherwise 0ms. | ||
* - Maps the emitted pair to the current value. | ||
*/ | ||
ngOnInit(): void { | ||
this.localLoading = this.loadingSubject.pipe( | ||
startWith(false), | ||
distinctUntilChanged(), | ||
pairwise(), | ||
debounce(([prev, _]) => (!!prev ? timer(1000) : timer(0))), | ||
map(([_, curr]) => curr), | ||
); | ||
} | ||
|
||
ngOnDestroy(): void {} | ||
} |