forked from launchdarkly/react-client-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncWithLDProvider.tsx
59 lines (53 loc) · 2.74 KB
/
asyncWithLDProvider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React, { useState, useEffect, FunctionComponent } from 'react';
import { LDFlagSet, LDFlagChangeset } from 'launchdarkly-js-client-sdk';
import { defaultReactOptions, ProviderConfig } from './types';
import { Provider } from './context';
import initLDClient from './initLDClient';
import {camelCaseKeys, getFlattenedFlagsFromChangeset} from './utils';
/**
* This is an async function which initializes LaunchDarkly's JS SDK (`launchdarkly-js-client-sdk`)
* and awaits it so all flags and the ldClient are ready before the consumer app is rendered.
*
* The difference between `withLDProvider` and `asyncWithLDProvider` is that `withLDProvider` initializes
* `launchdarkly-js-client-sdk` at componentDidMount. This means your flags and the ldClient are only available after
* your app has mounted. This can result in a flicker due to flag changes at startup time.
*
* `asyncWithLDProvider` initializes `launchdarkly-js-client-sdk` at the entry point of your app prior to render.
* This means that your flags and the ldClient are ready at the beginning of your app. This ensures your app does not
* flicker due to flag changes at startup time.
*
* `asyncWithLDProvider` accepts a config object which is used to initialize `launchdarkly-js-client-sdk`.
* It returns a provider which is a React FunctionComponent which:
* - saves all flags and the ldClient instance in the context API
* - subscribes to flag changes and propagate them through the context API
*
* @param config - The configuration used to initialize LaunchDarkly's JS SDK
*/
export default async function asyncWithLDProvider(config: ProviderConfig) {
const { clientSideID, user, flags, options, reactOptions: userReactOptions } = config;
const reactOptions = { ...defaultReactOptions, ...userReactOptions };
const { flags: fetchedFlags, ldClient } = await initLDClient(clientSideID, user, reactOptions, options, flags);
const LDProvider: FunctionComponent = ({ children }) => {
const [ldData, setLDData] = useState({
flags: fetchedFlags,
ldClient,
});
useEffect(() => {
if (options) {
const { bootstrap } = options;
if (bootstrap && bootstrap !== 'localStorage') {
const bootstrappedFlags = reactOptions.useCamelCaseFlagKeys ? camelCaseKeys(bootstrap) : bootstrap;
setLDData(prev => ({ ...prev, flags: bootstrappedFlags }));
}
}
ldClient.on('change', (changes: LDFlagChangeset) => {
const flattened: LDFlagSet | null = getFlattenedFlagsFromChangeset(changes, flags, reactOptions);
if (flattened) {
setLDData(prev => ({ ...prev, flags: { ...prev.flags, ...flattened } }));
}
});
}, []);
return <Provider value={ldData}>{children}</Provider>;
};
return LDProvider;
}