11import React , { Component , PropsWithChildren } from 'react' ;
2- import { LDClient , LDFlagChangeset } from 'launchdarkly-js-client-sdk' ;
2+ import { LDClient , LDFlagChangeset , LDFlagSet } from 'launchdarkly-js-client-sdk' ;
33import { EnhancedComponent , ProviderConfig , defaultReactOptions } from './types' ;
4- import { Provider , LDContext as HocState } from './context' ;
4+ import { Provider , LDContext } from './context' ;
55import initLDClient from './initLDClient' ;
66import { camelCaseKeys , fetchFlags , getFlattenedFlagsFromChangeset } from './utils' ;
77import getFlagsProxy from './getFlagsProxy' ;
88
9+ interface LDHocState extends LDContext {
10+ unproxiedFlags : LDFlagSet ;
11+ }
12+
913/**
1014 * The `LDProvider` is a component which accepts a config object which is used to
1115 * initialize `launchdarkly-js-client-sdk`.
@@ -23,8 +27,8 @@ import getFlagsProxy from './getFlagsProxy';
2327 * within your application. This provider is used inside the `withLDProviderHOC` and can be used instead to initialize
2428 * the `launchdarkly-js-client-sdk`. For async initialization, check out the `asyncWithLDProvider` function
2529 */
26- class LDProvider extends Component < PropsWithChildren < ProviderConfig > , HocState > implements EnhancedComponent {
27- readonly state : Readonly < HocState > ;
30+ class LDProvider extends Component < PropsWithChildren < ProviderConfig > , LDHocState > implements EnhancedComponent {
31+ readonly state : Readonly < LDHocState > ;
2832
2933 constructor ( props : ProviderConfig ) {
3034 super ( props ) ;
@@ -33,7 +37,7 @@ class LDProvider extends Component<PropsWithChildren<ProviderConfig>, HocState>
3337
3438 this . state = {
3539 flags : { } ,
36- _flags : { } ,
40+ unproxiedFlags : { } ,
3741 flagKeyMap : { } ,
3842 ldClient : undefined ,
3943 } ;
@@ -42,10 +46,9 @@ class LDProvider extends Component<PropsWithChildren<ProviderConfig>, HocState>
4246 const { bootstrap } = options ;
4347 if ( bootstrap && bootstrap !== 'localStorage' ) {
4448 const { useCamelCaseFlagKeys } = this . getReactOptions ( ) ;
45- const flags = useCamelCaseFlagKeys ? camelCaseKeys ( bootstrap ) : bootstrap ;
4649 this . state = {
47- flags,
48- _flags : bootstrap ,
50+ flags : useCamelCaseFlagKeys ? camelCaseKeys ( bootstrap ) : bootstrap ,
51+ unproxiedFlags : bootstrap ,
4952 flagKeyMap : { } ,
5053 ldClient : undefined ,
5154 } ;
@@ -60,8 +63,12 @@ class LDProvider extends Component<PropsWithChildren<ProviderConfig>, HocState>
6063 ldClient . on ( 'change' , ( changes : LDFlagChangeset ) => {
6164 const reactOptions = this . getReactOptions ( ) ;
6265 const updates = getFlattenedFlagsFromChangeset ( changes , targetFlags ) ;
66+ const unproxiedFlags = {
67+ ...this . state . unproxiedFlags ,
68+ ...updates ,
69+ } ;
6370 if ( Object . keys ( updates ) . length > 0 ) {
64- this . setState ( ( { _flags } ) => getFlagsProxy ( ldClient , { ..._flags , ... updates } , reactOptions , targetFlags ) ) ;
71+ this . setState ( { unproxiedFlags , ...getFlagsProxy ( ldClient , unproxiedFlags , reactOptions , targetFlags ) } ) ;
6572 }
6673 } ) ;
6774 } ;
@@ -70,15 +77,15 @@ class LDProvider extends Component<PropsWithChildren<ProviderConfig>, HocState>
7077 const { clientSideID, flags, options, user } = this . props ;
7178 let ldClient = await this . props . ldClient ;
7279 const reactOptions = this . getReactOptions ( ) ;
73- let fetchedFlags ;
80+ let unproxiedFlags ;
7481 if ( ldClient ) {
75- fetchedFlags = fetchFlags ( ldClient , flags ) ;
82+ unproxiedFlags = fetchFlags ( ldClient , flags ) ;
7683 } else {
7784 const initialisedOutput = await initLDClient ( clientSideID , user , options , flags ) ;
78- fetchedFlags = initialisedOutput . flags ;
85+ unproxiedFlags = initialisedOutput . flags ;
7986 ldClient = initialisedOutput . ldClient ;
8087 }
81- this . setState ( { ...getFlagsProxy ( ldClient , fetchedFlags , reactOptions , flags ) , ldClient } ) ;
88+ this . setState ( { unproxiedFlags , ...getFlagsProxy ( ldClient , unproxiedFlags , reactOptions , flags ) , ldClient } ) ;
8289 this . subscribeToChanges ( ldClient ) ;
8390 } ;
8491
@@ -100,7 +107,9 @@ class LDProvider extends Component<PropsWithChildren<ProviderConfig>, HocState>
100107 }
101108
102109 render ( ) {
103- return < Provider value = { this . state } > { this . props . children } </ Provider > ;
110+ const { flags, flagKeyMap, ldClient } = this . state ;
111+
112+ return < Provider value = { { flags, flagKeyMap, ldClient } } > { this . props . children } </ Provider > ;
104113 }
105114}
106115
0 commit comments