Skip to content

Commit

Permalink
feat(service-worker): expose SwRegistrationOptions token to allow r…
Browse files Browse the repository at this point in the history
…untime config (#21842)

Previously, the ServiceWorker registration options should be defined as
an object literal (in order for them to be compatible with Ahead-of-Time
compilation), thus making it impossible to base the ServiceWorker
behavior on runtime conditions.
This commit allows specifying the registration options using a regular
provider, which means that it can take advantage of the `useFactory`
option to determine the config at runtime, while still remaining
compatible with AoT compilation.

PR Close #21842
  • Loading branch information
deebloo authored and AndrewKushnir committed Apr 25, 2019
1 parent d7887ab commit 39c0152
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/service-worker/src/index.ts
Expand Up @@ -15,6 +15,6 @@
*/

export {UpdateActivatedEvent, UpdateAvailableEvent} from './low_level';
export {ServiceWorkerModule} from './module';
export {ServiceWorkerModule, SwRegistrationOptions} from './module';
export {SwPush} from './push';
export {SwUpdate} from './update';
8 changes: 7 additions & 1 deletion packages/service-worker/src/module.ts
Expand Up @@ -14,6 +14,12 @@ import {NgswCommChannel} from './low_level';
import {SwPush} from './push';
import {SwUpdate} from './update';

/**
* Token that can be used to provide options for `ServiceWorkerModule` outside of
* `ServiceWorkerModule.register()`.
*
* @publicApi
*/
export abstract class SwRegistrationOptions {
scope?: string;
enabled?: boolean;
Expand Down Expand Up @@ -69,7 +75,7 @@ export class ServiceWorkerModule {
* If `enabled` is set to `false` in the given options, the module will behave as if service
* workers are not supported by the browser, and the service worker will not be registered.
*/
static register(script: string, opts: {scope?: string; enabled?: boolean;} = {}):
static register(script: string, opts: SwRegistrationOptions = {}):
ModuleWithProviders<ServiceWorkerModule> {
return {
ngModule: ServiceWorkerModule,
Expand Down
41 changes: 40 additions & 1 deletion packages/service-worker/test/comm_spec.ts
Expand Up @@ -9,7 +9,7 @@
import {PLATFORM_ID} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {NgswCommChannel} from '@angular/service-worker/src/low_level';
import {SwRegistrationOptions, ngswCommChannelFactory} from '@angular/service-worker/src/module';
import {ServiceWorkerModule, SwRegistrationOptions, ngswCommChannelFactory} from '@angular/service-worker/src/module';
import {SwPush} from '@angular/service-worker/src/push';
import {SwUpdate} from '@angular/service-worker/src/update';
import {MockPushManager, MockPushSubscription, MockServiceWorkerContainer, MockServiceWorkerRegistration, patchDecodeBase64} from '@angular/service-worker/testing/mock';
Expand Down Expand Up @@ -47,6 +47,45 @@ import {async_fit, async_it} from './async';
});
});

describe('ServiceWorkerModule config', () => {
it('SwUpdate isEnabled is false when configuring via static method', () => {
TestBed.configureTestingModule(
{imports: [ServiceWorkerModule.register('', {enabled: false})]});

expect(TestBed.get(SwUpdate).isEnabled).toEqual(false);
});

it('SwUpdate isEnabled is true when configuring via static method', () => {
TestBed.configureTestingModule({
imports: [ServiceWorkerModule.register('', {enabled: true})],
providers: [{provide: NgswCommChannel, useValue: comm}]
});

expect(TestBed.get(SwUpdate).isEnabled).toEqual(true);
});

it('SwUpdate isEnabled is false when configuring directly via token', () => {
TestBed.configureTestingModule({
imports: [ServiceWorkerModule.register('')],
providers: [{provide: SwRegistrationOptions, useFactory: () => ({enabled: false})}]
});

expect(TestBed.get(SwUpdate).isEnabled).toEqual(false);
});

it('SwUpdate isEnabled is true when configuring directly via token', () => {
TestBed.configureTestingModule({
imports: [ServiceWorkerModule.register('')],
providers: [
{provide: NgswCommChannel, useValue: comm},
{provide: SwRegistrationOptions, useFactory: () => ({enabled: true})}
]
});

expect(TestBed.get(SwUpdate).isEnabled).toEqual(true);
});
});

describe('ngswCommChannelFactory', () => {
it('gives disabled NgswCommChannel for platform-server', () => {
TestBed.configureTestingModule({
Expand Down
10 changes: 6 additions & 4 deletions tools/public_api_guard/service-worker/service-worker.d.ts
@@ -1,8 +1,5 @@
export declare class ServiceWorkerModule {
static register(script: string, opts?: {
scope?: string;
enabled?: boolean;
}): ModuleWithProviders<ServiceWorkerModule>;
static register(script: string, opts?: SwRegistrationOptions): ModuleWithProviders<ServiceWorkerModule>;
}

export declare class SwPush {
Expand All @@ -22,6 +19,11 @@ export declare class SwPush {
unsubscribe(): Promise<void>;
}

export declare abstract class SwRegistrationOptions {
enabled?: boolean;
scope?: string;
}

export declare class SwUpdate {
readonly activated: Observable<UpdateActivatedEvent>;
readonly available: Observable<UpdateAvailableEvent>;
Expand Down

0 comments on commit 39c0152

Please sign in to comment.