Service Worker does not work when using setInterval in AppComponent

0

I am using Angular 6. I have a problem with service worker. I want to check in my whole application when there is internet to execute a function, for which I create a setInterval inside the constructor of my AppComponent but doing tests I notice that this prevents the SW from being registered. Otherwise, if I comment the lines that contain the interval, the service worker is registered.

AppComponent:

import { Component, AfterViewInit, OnInit } from '@angular/core';
import { PromptUpdateService } from './prompt-update.service';
import { LogUpdateService } from './log-update.service';
import { LocalStorageService } from './local-storage.service';
import { SwUpdate } from "../../node_modules/@angular/service-worker";
import { UpdateService } from './update.service';   

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, AfterViewInit {

    title = 'Violeta';
    ingresar = true;
    test = "2018-07-07";

    constructor(private logUpdateService: LogUpdateService, private update: UpdateService, private promptUpdateService: PromptUpdateService, private localStorageService: LocalStorageService) {
        let bOffline = this.localStorageService.getSession("offline");
        if (bOffline === null || bOffline === undefined || bOffline === '' || bOffline.value === false) {
            this.localStorageService.setSession("offline", { "value": false });
        }       

        this.activarSincronizacion();
        this.activarActualizaciones();
    }

    ngOnInit() {
    }

    revisarConexion(){
        if (navigator.onLine) {
            this.update.setbOffONLine(true);
            this.localStorageService.subirDatos();
            this.localStorageService.actualizarTablas();
        }
        else {
            this.update.setbOffONLine(false);
        }
    }

    activarActualizaciones(){
        setInterval(() => {
            this.update.revisarActualizaciones();
        }, 1000);
    }

    activarSincronizacion(){
        setInterval(() => {
            if(this.update.getBROffline()){
                this.revisarConexion();
            }
        }, 1000 * 60 * 60);
    }

Update.service.ts

import { Injectable } from '@angular/core';
import { MatSnackBar } from "@angular/material";
import { SwUpdate } from '@angular/service-worker';

@Injectable()
export class UpdateService {

  private bOffONLine = true;
  private bROffline = false; 
  private bActualizaciones : boolean;
  constructor(private swUpdate: SwUpdate, public snackBar: MatSnackBar) {
      this.bActualizaciones = false;
  }

  revisarActualizaciones(){
    console.log("revisando actualizaciones");
    this.swUpdate.available.subscribe(evt => {
      this.swUpdate.activateUpdate().then(() => 
            this.anunciarActualizacion()            
         );
    });
  }

  setbOffONLine(bOffONLine){
    this.bOffONLine = bOffONLine;
  }

  anunciarActualizacion(){
    console.log("hay una actualización");
    this.bActualizaciones = true;
  }

  abrirAlerta(mensaje: string) {
    this.snackBar.open(mensaje, "OK", {
      duration: 500
    });
  }

  setBROffline(bROffline){
    this.bROffline = bROffline;
  }

  getBROffline(){
    return this.bROffline;
  }
}
    
asked by Diana Hurtado 17.12.2018 в 23:58
source

0 answers