Skip to content

Commit

Permalink
VIH-11072 Fix flag evaluation issue with launch darkly service (#2279)
Browse files Browse the repository at this point in the history
* Wait for flags to be ready before returning them

* Sonar fix
  • Loading branch information
oliver-scott committed Oct 22, 2024
1 parent 267b0e9 commit a4a1db3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>(flagKey).subscribe(val => (result = val));
service.loadAllFlagsAndSetupSubscriptions();
ldClientSpy.waitUntilReady.and.returnValue(Promise.resolve());
tick();

// Assert
expect(result).toBe(true);
}));
});
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -19,6 +19,7 @@ export const FEATURE_FLAGS = {
export class LaunchDarklyService implements OnDestroy {
client: LDClient;
private flagSubjects: { [key: string]: BehaviorSubject<LDFlagValue> } = {};
private readonly flagsReadySubject = new ReplaySubject<boolean>(1);

constructor(private configService: ConfigService) {
this.vhInitialize();
Expand Down Expand Up @@ -57,12 +58,18 @@ export class LaunchDarklyService implements OnDestroy {
this.flagSubjects[flagKey].next(newValue);
});
});
this.flagsReadySubject.next(true);
this.flagsReadySubject.complete();
}

getFlag<T>(flagKey: string, defaultValue: LDFlagValue = false): Observable<T> {
if (!this.flagSubjects[flagKey]) {
this.flagSubjects[flagKey] = new BehaviorSubject<LDFlagValue>(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<LDFlagValue>(defaultValue);
}
return this.flagSubjects[flagKey].asObservable().pipe(map(value => value as T));
})
);
}
}

0 comments on commit a4a1db3

Please sign in to comment.