Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions packages/launchdarkly-react-client-sdk/src/withLDProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ export interface ProviderConfig {
*/
export interface EnhancedComponent extends React.Component {
subscribeToChanges(ldClient: LDClient): void;
componentDidMount(): Promise<void>;
initSession(clientSideID: string, user?: LDUser, options?: LDOptions, flags?: LDFlagSet): Promise<void>;
}

export type Callback<P> = (ownProps: P) => ProviderConfig

/**
* withLDProvider is a function which accepts a config object which is used to initialise launchdarkly-js-client-sdk.
* It returns a function which accepts your root React component and returns a HOC.
Expand All @@ -56,8 +58,8 @@ export interface EnhancedComponent extends React.Component {
*
* @param config - The configuration used to initialize LaunchDarkly's js client
*/
export function withLDProvider(config: ProviderConfig) {
return function withLDPoviderHoc<P>(WrappedComponent: React.ComponentType<P>) {
export function withLDProvider<P>(config: ProviderConfig | Callback<P>) {
return function withLDPoviderHoc(WrappedComponent: React.ComponentType<P>) {
return class extends React.Component<P, HocState> implements EnhancedComponent {
readonly state: Readonly<HocState>;

Expand All @@ -69,14 +71,16 @@ export function withLDProvider(config: ProviderConfig) {
ldClient: undefined,
};

const { options } = config;
if (options) {
const { bootstrap } = options;
if (bootstrap && bootstrap !== 'localStorage') {
this.state = {
flags: camelCaseKeys(bootstrap),
ldClient: undefined,
};
if (typeof config !== 'function') {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I'm not familiar enough with the localStorage option to see how this will work with this new logic.

const { options } = config;
if (options) {
const { bootstrap } = options;
if (bootstrap && bootstrap !== 'localStorage') {
this.state = {
flags: camelCaseKeys(bootstrap),
ldClient: undefined,
};
}
}
}
}
Expand All @@ -91,8 +95,17 @@ export function withLDProvider(config: ProviderConfig) {
});
};

async componentDidMount() {
const { clientSideID, user, options, flags } = config;
componentDidMount() {
if (typeof config !== 'function') {
const { clientSideID, user, options, flags } = config;
this.initSession(clientSideID, user, options, flags)
} else {
const { clientSideID, user, options, flags } = config(this.props);
this.initSession(clientSideID, user, options, flags)
}
}

initSession = async (clientSideID: string, user?: LDUser, options?: LDOptions, flags?: LDFlagSet) => {
const { flags: fetchedFlags, ldClient } = await initLDClient(clientSideID, user, options, flags);
this.setState({ flags: fetchedFlags, ldClient });
this.subscribeToChanges(ldClient);
Expand Down