I want to add an Event Listener to my Ionic2 project, but only once of course. How to make sure it is only instantiated once. I came up with this, but it feels wrong. We load all services in app.module.ts. I construct/instantiate this service in our BackEnd Service, it gets called many times.
import {Injectable} from "@angular/core";
@Injectable()
export class AppPausedService {
private appPaused = false;
private alreadyInstantiated = false;
constructor() {
if (!this.alreadyInstantiated) {
this.alreadyInstantiated = true;
document.addEventListener("pause", () => {
this.appPaused = true;
}, false);
document.addEventListener("resume", () => {
this.appPaused = false;
}, false);
}
}
isPaused () {
return this.appPaused;
}
}