Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Created February 8, 2020 03:43
Show Gist options
  • Save alexytiger/e4937fc9d8d0785b5b3cc68847ab1d83 to your computer and use it in GitHub Desktop.
Save alexytiger/e4937fc9d8d0785b5b3cc68847ab1d83 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { Overlay, OverlayRef } from '@angular/cdk/overlay';
import { ComponentPortal } from '@angular/cdk/portal';
import { MatSpinner } from '@angular/material';
import { Observable, Subject } from 'rxjs';
import { scan, map, distinctUntilChanged } from 'rxjs/operators';
@Injectable({
providedIn: 'root',
})
export class SpinnerOverlayService {
private spinnerTopRef: OverlayRef;
private spin$: Subject<number> = new Subject();
constructor( private overlay: Overlay ) {
this.spinnerTopRef = this.overlay.create({
hasBackdrop: true,
positionStrategy: this.overlay.position()
.global()
.centerHorizontally()
.centerVertically()
});
this.spin$
.asObservable()
.pipe(
scan((acc, next) => {
if (!next) {
return 0;
}
return (acc + next) >= 0 ? acc + next : 0;
}, 0),
map(val => val > 0),
distinctUntilChanged()
)
.subscribe(
(res) => {
if (res) {
this.spinnerTopRef.attach(new ComponentPortal(MatSpinner));
} else if (this.spinnerTopRef.hasAttached()) {
this.spinnerTopRef.detach();
}
}
);
}
show = () => this.spin$.next(1);
hide = () => this.spin$.next(-1);
reset = () => this.spin$.next(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment