refactor(service-worker): Remove deprecated activated & available observables. (#49498)

`activated`and `available` were deprecated by #43535 in v13.

PR Close #49498
This commit is contained in:
Matthieu Riegler
2023-03-20 21:08:04 +01:00
committed by Andrew Scott
parent 21b085d221
commit a645ceca94
15 changed files with 53 additions and 211 deletions
@@ -1,6 +1,6 @@
import { ApplicationRef, ErrorHandler, Injector } from '@angular/core';
import { discardPeriodicTasks, fakeAsync, tick } from '@angular/core/testing';
import { SwUpdate, VersionEvent, VersionReadyEvent } from '@angular/service-worker';
import { SwUpdate, VersionEvent } from '@angular/service-worker';
import { BehaviorSubject, Subject } from 'rxjs';
import { LocationService } from 'app/shared/location.service';
@@ -89,7 +89,7 @@ describe('SwUpdatesService', () => {
appRef.isStable.next(true);
expect(swu.activateUpdate).not.toHaveBeenCalled();
swu.$$versionUpdatesSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'} as VersionReadyEvent);
swu.$$availableSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'});
expect(swu.activateUpdate).toHaveBeenCalled();
})));
@@ -100,7 +100,7 @@ describe('SwUpdatesService', () => {
tick(checkInterval);
expect(swu.checkForUpdate).toHaveBeenCalledTimes(1);
swu.$$versionUpdatesSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'} as VersionReadyEvent);
swu.$$availableSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'});
tick(checkInterval);
expect(swu.checkForUpdate).toHaveBeenCalledTimes(2);
@@ -111,14 +111,15 @@ describe('SwUpdatesService', () => {
discardPeriodicTasks();
})));
it('should request a full page navigation when an update has been activated', fakeAsync(run(() => {
swu.$$versionUpdatesSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'} as VersionReadyEvent);
tick();
it('should request a full page navigation when an update has been activated', fakeAsync(run(async () => {
expect(location.fullPageNavigationNeeded).toHaveBeenCalledTimes(0);
swu.$$availableSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'});
tick(checkInterval);
expect(location.fullPageNavigationNeeded).toHaveBeenCalledTimes(1);
swu.$$versionUpdatesSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'} as VersionReadyEvent);
tick();
swu.$$availableSubj.next({latestVersion: {hash: 'baz'}, type: 'VERSION_READY'});
tick(checkInterval);
expect(location.fullPageNavigationNeeded).toHaveBeenCalledTimes(2);
})));
@@ -159,9 +160,7 @@ describe('SwUpdatesService', () => {
tick(checkInterval);
tick(checkInterval);
swu.$$versionUpdatesSubj.next({
latestVersion: {hash: 'foo'}, currentVersion: {hash: 'bar'} , type: 'VERSION_READY'
});
swu.$$availableSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'});
tick(checkInterval);
tick(checkInterval);
@@ -170,21 +169,18 @@ describe('SwUpdatesService', () => {
})));
it('should not activate available updates', fakeAsync(runDeactivated(() => {
swu.$$versionUpdatesSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'} as VersionReadyEvent);
swu.$$availableSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'});
expect(swu.activateUpdate).not.toHaveBeenCalled();
})));
it('should never request a full page navigation', runDeactivated(() => {
swu.$$versionUpdatesSubj.next({
latestVersion: {hash: 'foo'}, currentVersion: {hash: 'bar'} , type: 'VERSION_READY'
});
swu.$$versionUpdatesSubj.next({
latestVersion: {hash: 'baz'}, currentVersion: {hash: 'qux'} , type: 'VERSION_READY'
});
it('should never request a full page navigation', fakeAsync(runDeactivated(() => {
swu.$$availableSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'});
tick(checkInterval);
swu.$$availableSubj.next({latestVersion: {hash: 'baz'}, type: 'VERSION_READY'});
tick(checkInterval);
expect(location.fullPageNavigationNeeded).not.toHaveBeenCalled();
}));
})));
it('should never request a page reload', runDeactivated(() => {
swu.$$unrecoverableSubj.next({reason: 'Something bad happened'});
@@ -216,9 +212,7 @@ describe('SwUpdatesService', () => {
service.disable();
swu.checkForUpdate.calls.reset();
swu.$$versionUpdatesSubj.next({
latestVersion: {hash: 'foo'}, currentVersion: {hash: 'baz'} , type: 'VERSION_READY'
});
swu.$$availableSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'});
tick(checkInterval);
tick(checkInterval);
@@ -228,24 +222,22 @@ describe('SwUpdatesService', () => {
it('should not activate available updates', fakeAsync(run(() => {
service.disable();
swu.$$versionUpdatesSubj.next({latestVersion: {hash: 'foo'}} as VersionReadyEvent);
swu.$$availableSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'});
expect(swu.activateUpdate).not.toHaveBeenCalled();
})));
it('should stop requesting full page navigations when updates are activated', fakeAsync(run(() => {
swu.$$versionUpdatesSubj.next({
latestVersion: {hash: 'foo'}, currentVersion: {hash: 'bar'}, type: 'VERSION_READY'
});
tick();
swu.$$availableSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'});
tick(checkInterval);
expect(location.fullPageNavigationNeeded).toHaveBeenCalledTimes(1);
service.disable();
location.fullPageNavigationNeeded.calls.reset();
swu.$$versionUpdatesSubj.next({
latestVersion: {hash: 'baz'}, currentVersion: {hash: 'qux'}, type: 'VERSION_READY'
});
swu.$$availableSubj.next({latestVersion: {hash: 'baz'}, type: 'VERSION_READY'});
tick(checkInterval);
expect(location.fullPageNavigationNeeded).not.toHaveBeenCalled();
})));
@@ -281,6 +273,7 @@ describe('SwUpdatesService', () => {
service.enable();
swu.checkForUpdate.calls.reset();
swu.$$availableSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'});
tick(checkInterval);
expect(swu.checkForUpdate).toHaveBeenCalled();
@@ -290,10 +283,7 @@ describe('SwUpdatesService', () => {
service.disable();
service.enable();
swu.$$versionUpdatesSubj.next({
latestVersion: {hash: 'foo'}, type: 'VERSION_READY'
} as VersionReadyEvent);
swu.$$availableSubj.next({latestVersion: {hash: 'foo'}, type: 'VERSION_READY'});
expect(swu.activateUpdate).toHaveBeenCalled();
})));
});
@@ -316,11 +306,12 @@ class MockApplicationRef {
}
class MockSwUpdate {
$$availableSubj = new Subject<{latestVersion: {hash: string}, type: 'VERSION_READY'}>();
$$unrecoverableSubj = new Subject<{reason: string}>();
$$versionUpdatesSubj = new Subject<VersionEvent>();
versionUpdates = this.$$availableSubj.asObservable();
unrecoverable = this.$$unrecoverableSubj.asObservable();
versionUpdates =this.$$versionUpdatesSubj.asObservable();
activateUpdate = jasmine.createSpy('MockSwUpdate.activateUpdate')
.and.callFake(() => Promise.resolve(true));
+3 -2
View File
@@ -1,5 +1,5 @@
import { ApplicationRef, ErrorHandler, Injectable, OnDestroy } from '@angular/core';
import { SwUpdate, VersionReadyEvent } from '@angular/service-worker';
import { SwUpdate, VersionEvent, VersionReadyEvent } from '@angular/service-worker';
import { concat, from, interval, Subject } from 'rxjs';
import { filter, first, switchMap, takeUntil, tap } from 'rxjs/operators';
@@ -45,11 +45,12 @@ export class SwUpdatesService implements OnDestroy {
// Activate available updates.
this.swu.versionUpdates
.pipe(
filter((evt): evt is VersionReadyEvent => evt.type === 'VERSION_READY'),
filter((evt: VersionEvent): evt is VersionReadyEvent => evt.type === 'VERSION_READY'),
tap(evt => this.log(`Update available: ${JSON.stringify(evt)}`)),
takeUntil(this.onDisable),
switchMap(() => from(this.swu.activateUpdate()))
)
.subscribe((isActivated) => {
if(isActivated) {
this.log('Update activated');