2019-05-03 17:39:48 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
2019-05-10 13:04:40 +02:00
|
|
|
import { SwUpdate, UpdateAvailableEvent } from '@angular/service-worker';
|
2019-05-03 17:39:48 +02:00
|
|
|
import { NotifyService } from '../core-services/notify.service';
|
2019-05-10 13:04:40 +02:00
|
|
|
import { Observable } from 'rxjs';
|
2019-05-03 17:39:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Service Worker updates using the SwUpdate service form angular.
|
|
|
|
*/
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class UpdateService {
|
|
|
|
private static NOTIFY_NAME = 'swCheckForUpdate';
|
|
|
|
|
2019-05-10 13:04:40 +02:00
|
|
|
/**
|
|
|
|
* @returns the updateSubscription
|
|
|
|
*/
|
|
|
|
public get updateObservable(): Observable<UpdateAvailableEvent> {
|
|
|
|
return this.swUpdate.available;
|
|
|
|
}
|
|
|
|
|
2019-05-03 17:39:48 +02:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* Listens to available updates
|
|
|
|
*
|
|
|
|
* @param swUpdate Service Worker update service
|
|
|
|
* @param matSnackBar Currently to show that an update is available
|
|
|
|
*/
|
2019-05-10 13:04:40 +02:00
|
|
|
public constructor(private swUpdate: SwUpdate, private notify: NotifyService) {
|
2019-05-03 17:39:48 +02:00
|
|
|
// Listen on requests from other users to check for updates.
|
|
|
|
this.notify.getMessageObservable(UpdateService.NOTIFY_NAME).subscribe(() => {
|
|
|
|
this.checkForUpdate();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-10 13:04:40 +02:00
|
|
|
/**
|
|
|
|
* Manually applies the update if one was found
|
|
|
|
*/
|
|
|
|
public applyUpdate(): void {
|
|
|
|
this.swUpdate.activateUpdate().then(() => {
|
|
|
|
document.location.reload();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-03 17:39:48 +02:00
|
|
|
/**
|
|
|
|
* Trigger that to manually check for updates
|
|
|
|
*/
|
|
|
|
public checkForUpdate(): void {
|
|
|
|
if (this.swUpdate.isEnabled) {
|
|
|
|
this.swUpdate.checkForUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emits a message to all clients initiating to check for updates. This method
|
|
|
|
* can only be called by users with 'users.can_manage'. This will be checked by
|
|
|
|
* the server.
|
|
|
|
*/
|
|
|
|
public initiateUpdateCheckForAllClients(): void {
|
|
|
|
this.notify.sendToAllUsers(UpdateService.NOTIFY_NAME, {});
|
|
|
|
}
|
|
|
|
}
|