From ee317f2df3c64d87b5bb87d752ec814600c6e385 Mon Sep 17 00:00:00 2001 From: mmckenna Date: Wed, 17 Jul 2019 22:10:31 -0400 Subject: [PATCH] ads ability to pass callback with props to provider --- .../src/withLDProvider.tsx | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/launchdarkly-react-client-sdk/src/withLDProvider.tsx b/packages/launchdarkly-react-client-sdk/src/withLDProvider.tsx index ee9d7339..3d8348f7 100644 --- a/packages/launchdarkly-react-client-sdk/src/withLDProvider.tsx +++ b/packages/launchdarkly-react-client-sdk/src/withLDProvider.tsx @@ -43,9 +43,11 @@ export interface ProviderConfig { */ export interface EnhancedComponent extends React.Component { subscribeToChanges(ldClient: LDClient): void; - componentDidMount(): Promise; + initSession(clientSideID: string, user?: LDUser, options?: LDOptions, flags?: LDFlagSet): Promise; } +export type Callback

= (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. @@ -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

(WrappedComponent: React.ComponentType

) { +export function withLDProvider

(config: ProviderConfig | Callback

) { + return function withLDPoviderHoc(WrappedComponent: React.ComponentType

) { return class extends React.Component implements EnhancedComponent { readonly state: Readonly; @@ -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') { + const { options } = config; + if (options) { + const { bootstrap } = options; + if (bootstrap && bootstrap !== 'localStorage') { + this.state = { + flags: camelCaseKeys(bootstrap), + ldClient: undefined, + }; + } } } } @@ -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);