I'm triying to override show and hide methods from NgxSpinner to prevent the short-time display spinners when the process lasts too little time.
This is my code in the class that extends NgxSpinnerService
import { Injectable } from '@angular/core';
import { NgxSpinnerService, Spinner } from 'ngx-spinner';
@Injectable({
providedIn: 'root'
})
export class SpinnerService extends NgxSpinnerService {
constructor() { super(); }
private onDisplay: boolean = false;
show (name?: string, spinner?: Spinner): Promise<unknown> {
this.onDisplay = true;
setTimeout(() => {
console.log(`showing ${name} | onDisplay ${this.onDisplay}`);
if (this.onDisplay) return super.show(name, spinner);
}, 300);
return null;
}
hide (name?: string, debounce?: number): Promise<unknown> {
this.onDisplay = false;
return super.hide(name, debounce);
}
}
And this is a fragment of the component from where I'm calling the method from
constructor(
private _graphService: GraphsService,
private _medicinesService: MedicinesServices,
private _notification: NotificationService,
private fb: FormBuilder,
private spinner: SpinnerService
) { }
ngOnInit (): void {
this.init();
}
private init () {
this.spinner.show('component');
this._medicinesService.getAllGrupedBy().subscribe(
(data) => {
...
this.loadData();
},
(error) => {
this._notification.showErrorToast(error.errorCode);
this.spinner.hide('component');
}
);
}
private loadData (): void {
this.spinner.show('component');
if (!this.selectedOption) this.selectedOption = this.options[0];
this.getData().subscribe(
(data) => {
...
this.spinner.hide('component');
},
(error) => {
this.spinner.hide('component');
this._notification.showErrorToast(error.errorCode);
}
);
}
HTML in the app.component.html
<div [ngClass]="{ 'loading-container': !isMobileScreen }">
<ngx-spinner [name]="spinnersConf.component.name" [fullScreen]="false" [bdColor]="spinnersConf.component.bgColor">
<p style="font-size: 20px; color: white">{{ spinnersConf.component.text | translate }}</p>
</ngx-spinner>
</div>
The console appears in the developers tool window, but the spinner won't show. However, if I call the show method from the NgxSpinnerService it will appear without problems.
Am I having any error on my service?