Skip to content

Instantly share code, notes, and snippets.

Created March 30, 2022 13:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
LaunchDarkly feature flag service in Angular using RxJS
import { Injectable, OnDestroy } from '@angular/core';
import { environment } from '@environments/environment';
import * as LaunchDarkly from 'launchdarkly-js-client-sdk';
import { LDFlagValue } from 'launchdarkly-js-client-sdk';
import { Observable, Subject } from 'rxjs';
import { map } from 'rxjs/operators';
providedIn: 'root'
export class FeatureFlagService implements OnDestroy {
client: LaunchDarkly.LDClient;
async ngOnDestroy() {
await this.client.close();
initialize(): void {
const user = {
anonymous: true
} as LaunchDarkly.LDUser;
this.client = LaunchDarkly.initialize(environment.launchDarklyClientId, user);
getFlag(flagKey: string, defaultValue: LDFlagValue = false): Observable<LDFlagValue> {
const fetchFlag = new Subject<void>();
this.client.on(`change:${flagKey}`, () => {;
this.client.waitUntilReady().then(() => {;
return fetchFlag.pipe(
map(() => {
return this.client.variation(flagKey, defaultValue);
Copy link

How do you test “this.client.on” event? Any example code will be appreciated?

Copy link

thardy commented May 17, 2023

Can you provide a link of how this is used, particularly how/when you call initialize()? It would be nice to see this inside a project - initialization, consumption, etc.

Copy link

@thardy it’s used in a private repo, so, I can’t link to anything live. I will look to add some more example code here.

Copy link

Eventually you would want to replace the "anonymous" user with a real one. How would you do that here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment