|
| 1 | +import { initialize } from '@launchdarkly/js-client-sdk'; |
| 2 | + |
| 3 | +// Set clientSideID to your LaunchDarkly client-side ID |
| 4 | +const clientSideID = ''; |
| 5 | + |
| 6 | +// Set flagKey to the feature flag key you want to evaluate |
| 7 | +const flagKey = 'sample-feature'; |
| 8 | + |
| 9 | +// Set up the evaluation context. This context should appear on your |
| 10 | +// LaunchDarkly contexts dashboard soon after you run the demo. |
| 11 | +const context = { |
| 12 | + kind: 'user', |
| 13 | + key: 'example-user-key', |
| 14 | + name: 'Sandy', |
| 15 | +}; |
| 16 | + |
| 17 | +const div = document.createElement('div'); |
| 18 | +document.body.appendChild(div); |
| 19 | +div.appendChild(document.createTextNode('Initializing...')); |
| 20 | + |
| 21 | +const ldclient = initialize(clientSideID); |
| 22 | + |
| 23 | +function render() { |
| 24 | + const flagValue = ldclient.variation(flagKey, false); |
| 25 | + const label = `The ${flagKey} feature flag evaluates to ${flagValue}.`; |
| 26 | + document.body.style.background = flagValue ? '#00844B' : '#373841'; |
| 27 | + div.replaceChild(document.createTextNode(label), div.firstChild as Node); |
| 28 | +} |
| 29 | + |
| 30 | +ldclient.identify(context).then(() => { |
| 31 | + ldclient.on('initialized', () => { |
| 32 | + div.replaceChild( |
| 33 | + document.createTextNode('SDK successfully initialized!'), |
| 34 | + div.firstChild as Node, |
| 35 | + ); |
| 36 | + }); |
| 37 | + ldclient.on('failed', () => { |
| 38 | + div.replaceChild(document.createTextNode('SDK failed to initialize'), div.firstChild as Node); |
| 39 | + }); |
| 40 | + ldclient.on('ready', render); |
| 41 | + ldclient.on('change', render); |
| 42 | +}); |
0 commit comments