Skip to content

Commit

Permalink
Merge branch 'master' into EXUI-1079-start-enddate-for-service-banner…
Browse files Browse the repository at this point in the history
…s_NEW_LATEST
  • Loading branch information
RiteshHMCTS committed Jul 16, 2024
2 parents c1afcd8 + a32d29c commit 6ee1fc5
Show file tree
Hide file tree
Showing 5 changed files with 1,548 additions and 2,693 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hmcts/rpx-xui-common-lib",
"version": "2.0.21-message-banner-start-end-date-1",
"version": "2.0.23-message-banner-start-end-date-1",
"engines": {
"node": ">=18.19.0"
},
Expand Down Expand Up @@ -41,7 +41,7 @@
"@ng-idle/keepalive": "^11.0.3",
"core-js": "^3.37.0",
"govuk-frontend": "4.7.0",
"launchdarkly-js-client-sdk": "^2.15.2",
"launchdarkly-js-client-sdk": "^3.3.0",
"ngx-pagination": "^6.0.3",
"rpx-xui-translation": "1.0.3-angular-upgrade-17-rc",
"rxjs": "^7.8.1",
Expand Down
4 changes: 2 additions & 2 deletions projects/exui-common-lib/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@hmcts/rpx-xui-common-lib",
"version": "2.0.21-message-banner-start-end-date-1",
"version": "2.0.23-message-banner-start-end-date-1",
"peerDependencies": {
"launchdarkly-js-client-sdk": "^2.15.2",
"launchdarkly-js-client-sdk": "^3.3.0",
"ngx-pagination": "^3.2.1",
"rpx-xui-translation": "^0.1.1"
},
Expand Down
12 changes: 5 additions & 7 deletions projects/exui-common-lib/src/lib/models/feature-user.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
export class AnonymousFeatureUser {
public anonymous: true;
public anonymous: true;
}

export class LoggedInFeatureUser {
public key: string;
public custom: {
[key: string]: any,
roles: string[],
orgId: string
};
public key: string;
[key: string]: any;
public roles: string[];
public orgId: string;
}

export type FeatureUser = AnonymousFeatureUser | LoggedInFeatureUser;
Original file line number Diff line number Diff line change
@@ -1,59 +1,58 @@
import { Injectable } from '@angular/core';
import { initialize, LDClient} from 'launchdarkly-js-client-sdk';
import { initialize, LDClient, LDContext } from 'launchdarkly-js-client-sdk';
import { BehaviorSubject, Observable } from 'rxjs';
import { distinctUntilChanged, filter, map } from 'rxjs/operators';
import { FeatureUser } from '../../models/feature-user';
import { FeatureToggleService } from './feature-toggle.service';

@Injectable({
providedIn: 'root'
providedIn: 'root'
})
export class LaunchDarklyService implements FeatureToggleService {
private client: LDClient;
private readonly ready: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
private readonly features: Record<string, BehaviorSubject<any>> = {};
private context: LDContext = { kind: 'user', anonymous: true };
private clientId: string = '';

private client: LDClient;
private readonly ready: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
private readonly features: Record<string, BehaviorSubject<any>> = {};
private user: FeatureUser = { anonymous: true };
private clientId: string = '';
public initialize(user: FeatureUser = { anonymous: true }, clientId: string): void {
this.ready.next(false);
this.context = { kind: 'user', ...user };
this.clientId = clientId;
this.client = initialize(this.clientId, this.context, { useReport: true });
this.client.on('ready', () => {
this.client.identify(this.context).then(() => this.ready.next(true));
});
}

public initialize(user: FeatureUser = { anonymous: true }, clientId: string): void {
this.ready.next(false);
this.user = user;
this.clientId = clientId;
this.client = initialize(this.clientId, this.user, {});
this.client.on('ready', () => {
this.client.identify(this.user).then(() => this.ready.next(true));
});
}

public isEnabled(feature: string, defaultValue: boolean = false): Observable<boolean> {
return this.getValue<boolean>(feature, defaultValue);
}
public isEnabled(feature: string, defaultValue: boolean = false): Observable<boolean> {
return this.getValue<boolean>(feature, defaultValue);
}

public getArray<R = any>(feature: string): Observable<R[]> {
return this.getValue<R[]>(feature, []);
}
public getArray<R = any>(feature: string): Observable<R[]> {
return this.getValue<R[]>(feature, []);
}

// Note that this function always emits its default value first, which can lead to unexpected results
public getValue<R>(feature: string, defaultValue: R): Observable<R> {
if (!this.features.hasOwnProperty(feature)) {
this.features[feature] = new BehaviorSubject<R>(defaultValue);
this.ready.pipe(
filter(ready => ready),
map(() => this.client.variation(feature, defaultValue))
).subscribe(value => {
this.features[feature].next(value);
this.client.on(`change:${feature}`, (val: R) => {
this.features[feature].next(val);
});
});
}
return this.features[feature].pipe(
distinctUntilChanged()
);
// Note that this function always emits its default value first, which can lead to unexpected results
public getValue<R>(feature: string, defaultValue: R): Observable<R> {
if (!this.features.hasOwnProperty(feature)) {
this.features[feature] = new BehaviorSubject<R>(defaultValue);
this.ready.pipe(
filter((ready) => ready),
map(() => this.client.variation(feature, defaultValue))
).subscribe((value) => {
this.features[feature].next(value);
this.client.on(`change:${feature}`, (val: R) => {
this.features[feature].next(val);
});
});
}
return this.features[feature].pipe(
distinctUntilChanged()
);
}

/**
/**
* This method returns an observable that will only get the state of the feature toggle
* once. It calls the LD SDK directly, and should only be used in circumstances where
* only one value should be emitted, that value coming directly from LD. This will likely
Expand All @@ -62,14 +61,14 @@ export class LaunchDarklyService implements FeatureToggleService {
* @param feature string
* @param defaultValue R
*/
public getValueOnce<R>(feature: string, defaultValue: R): Observable<R> {
return this.ready.pipe(
filter(ready => ready),
map(() => this.client.variation(feature, defaultValue))
);
}
public getValueOnce<R>(feature: string, defaultValue: R): Observable<R> {
return this.ready.pipe(
filter((ready) => ready),
map(() => this.client.variation(feature, defaultValue))
);
}

public getValueSync<R>(feature: string, defaultValue: R): R {
return this.client.variation(feature, defaultValue);
}
public getValueSync<R>(feature: string, defaultValue: R): R {
return this.client.variation(feature, defaultValue);
}
}
Loading

0 comments on commit 6ee1fc5

Please sign in to comment.