diff --git a/VideoWeb/VideoWeb/ClientApp/src/app/services/launch-darkly.service.spec.ts b/VideoWeb/VideoWeb/ClientApp/src/app/services/launch-darkly.service.spec.ts index 5550d8bff4..089b3fe4d9 100644 --- a/VideoWeb/VideoWeb/ClientApp/src/app/services/launch-darkly.service.spec.ts +++ b/VideoWeb/VideoWeb/ClientApp/src/app/services/launch-darkly.service.spec.ts @@ -55,4 +55,25 @@ describe('LaunchDarklyService', () => { expect(result).toBe(true); })); + + it('should wait for flags to be loaded before returning requested flag', fakeAsync(() => { + // Arrange + service.client = ldClientSpy; + const flagKey = FEATURE_FLAGS.dom1SignIn; + const keyParam = `change:${flagKey}`; + const allFlags: LDFlagSet = { [flagKey]: true }; + ldClientSpy.allFlags.and.returnValue(allFlags); + ldClientSpy.on.withArgs(keyParam, jasmine.anything()).and.returnValue(); + ldClientSpy.variation.withArgs(flagKey, jasmine.any(Boolean)).and.returnValue(true); + let result: boolean; + + // Act + service.getFlag(flagKey).subscribe(val => (result = val)); + service.loadAllFlagsAndSetupSubscriptions(); + ldClientSpy.waitUntilReady.and.returnValue(Promise.resolve()); + tick(); + + // Assert + expect(result).toBe(true); + })); }); diff --git a/VideoWeb/VideoWeb/ClientApp/src/app/services/launch-darkly.service.ts b/VideoWeb/VideoWeb/ClientApp/src/app/services/launch-darkly.service.ts index 4bc2d245fb..296f34513f 100644 --- a/VideoWeb/VideoWeb/ClientApp/src/app/services/launch-darkly.service.ts +++ b/VideoWeb/VideoWeb/ClientApp/src/app/services/launch-darkly.service.ts @@ -1,8 +1,8 @@ import { Injectable, OnDestroy } from '@angular/core'; import { LDFlagValue, LDClient, LDContext, initialize } from 'launchdarkly-js-client-sdk'; -import { BehaviorSubject, Observable } from 'rxjs'; +import { BehaviorSubject, Observable, ReplaySubject } from 'rxjs'; import { ConfigService } from './api/config.service'; -import { first, map } from 'rxjs/operators'; +import { first, map, switchMap } from 'rxjs/operators'; export const FEATURE_FLAGS = { dom1SignIn: 'dom1', @@ -19,6 +19,7 @@ export const FEATURE_FLAGS = { export class LaunchDarklyService implements OnDestroy { client: LDClient; private flagSubjects: { [key: string]: BehaviorSubject } = {}; + private readonly flagsReadySubject = new ReplaySubject(1); constructor(private configService: ConfigService) { this.vhInitialize(); @@ -57,12 +58,18 @@ export class LaunchDarklyService implements OnDestroy { this.flagSubjects[flagKey].next(newValue); }); }); + this.flagsReadySubject.next(true); + this.flagsReadySubject.complete(); } getFlag(flagKey: string, defaultValue: LDFlagValue = false): Observable { - if (!this.flagSubjects[flagKey]) { - this.flagSubjects[flagKey] = new BehaviorSubject(defaultValue); - } - return this.flagSubjects[flagKey].asObservable().pipe(map(value => value as T)); + return this.flagsReadySubject.asObservable().pipe( + switchMap(() => { + if (!this.flagSubjects[flagKey]) { + this.flagSubjects[flagKey] = new BehaviorSubject(defaultValue); + } + return this.flagSubjects[flagKey].asObservable().pipe(map(value => value as T)); + }) + ); } }