|  | 
| 1 | 1 | import * as LaunchDarkly from '@launchdarkly/node-server-sdk'; | 
| 2 | 2 | 
 | 
| 3 | 3 | // Set sdkKey to your LaunchDarkly SDK key. | 
| 4 |  | -const sdkKey = ""; | 
|  | 4 | +const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY ?? 'your-sdk-key'; | 
| 5 | 5 | 
 | 
| 6 | 6 | // Set featureFlagKey to the feature flag key you want to evaluate. | 
| 7 |  | -const featureFlagKey = "my-boolean-flag"; | 
|  | 7 | +const featureFlagKey = process.env.LAUNCHDARKLY_FLAG_KEY ?? 'sample-feature'; | 
| 8 | 8 | 
 | 
| 9 |  | -// Set up the context properties. This use should appear on your LaunchDarkly | 
| 10 |  | -// contexts dashboard soon after you run the demo. | 
|  | 9 | +function showBanner() { | 
|  | 10 | + console.log( | 
|  | 11 | + ` ██ | 
|  | 12 | + ██ | 
|  | 13 | + ████████ | 
|  | 14 | + ███████ | 
|  | 15 | +██ LAUNCHDARKLY █ | 
|  | 16 | + ███████ | 
|  | 17 | + ████████ | 
|  | 18 | + ██ | 
|  | 19 | + ██ | 
|  | 20 | +`, | 
|  | 21 | + ); | 
|  | 22 | +} | 
|  | 23 | + | 
|  | 24 | +function printValueAndBanner(flagValue: boolean) { | 
|  | 25 | + console.log(`*** The '${featureFlagKey}' feature flag evaluates to ${flagValue}.`); | 
|  | 26 | + | 
|  | 27 | + if (flagValue) showBanner(); | 
|  | 28 | +} | 
|  | 29 | + | 
|  | 30 | +if (!sdkKey) { | 
|  | 31 | + console.log('*** Please edit index.js to set sdkKey to your LaunchDarkly SDK key first.'); | 
|  | 32 | + process.exit(1); | 
|  | 33 | +} | 
|  | 34 | + | 
|  | 35 | + | 
|  | 36 | +const ldClient = LaunchDarkly.init(sdkKey); | 
|  | 37 | + | 
|  | 38 | +// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard | 
|  | 39 | +// soon after you run the demo. | 
| 11 | 40 | const context = { | 
| 12 |  | - "kind": "user", | 
| 13 |  | - "name": "Sandy", | 
| 14 |  | - "key": "example-context-key" | 
|  | 41 | + kind: 'user', | 
|  | 42 | + key: 'example-user-key', | 
|  | 43 | + name: 'Sandy', | 
| 15 | 44 | }; | 
| 16 | 45 | 
 | 
| 17 |  | -function showMessage(s: string) { | 
| 18 |  | - console.log("*** " + s); | 
| 19 |  | - console.log(""); | 
| 20 |  | -} | 
|  | 46 | +async function main() { | 
|  | 47 | + try { | 
|  | 48 | + await ldClient.waitForInitialization({timeout: 10}); | 
| 21 | 49 | 
 | 
| 22 |  | -const client = LaunchDarkly.init(sdkKey); | 
| 23 |  | - | 
| 24 |  | -client.once('ready', function () { | 
| 25 |  | - showMessage("SDK successfully initialized!"); | 
| 26 |  | - client.variation(featureFlagKey, context, false, function (err, showFeature) { | 
| 27 |  | - client.track("event-called", context); | 
| 28 |  | - if (showFeature) { | 
| 29 |  | - // application code to show the feature | 
| 30 |  | - showMessage("Feature flag '" + featureFlagKey + "' is true for this context"); | 
| 31 |  | - } else { | 
| 32 |  | - // the code to run if the feature is off | 
| 33 |  | - showMessage("Feature flag '" + featureFlagKey + "' is false for this context"); | 
| 34 |  | - } | 
|  | 50 | + console.log('*** SDK successfully initialized!'); | 
| 35 | 51 | 
 | 
| 36 |  | - // Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics | 
| 37 |  | - // events to LaunchDarkly before the program exits. If analytics events are not delivered, | 
| 38 |  | - // the context properties and flag usage statistics will not appear on your dashboard. In a | 
| 39 |  | - // normal long-running application, the SDK would continue running and events would be | 
| 40 |  | - // delivered automatically in the background. | 
| 41 |  | - client.flush(function () { | 
| 42 |  | - client.close(); | 
|  | 52 | + const eventKey = `update:${featureFlagKey}`; | 
|  | 53 | + ldClient.on(eventKey, async () => { | 
|  | 54 | + const flagValue = await ldClient.variation(featureFlagKey, context, false); | 
|  | 55 | + printValueAndBanner(flagValue); | 
| 43 | 56 |  }); | 
| 44 |  | - }); | 
| 45 |  | -}); | 
|  | 57 | + | 
|  | 58 | + const flagValue = await ldClient.variation(featureFlagKey, context, false); | 
|  | 59 | + printValueAndBanner(flagValue); | 
|  | 60 | + | 
|  | 61 | + if (typeof process.env.CI !== "undefined") { | 
|  | 62 | + process.exit(0); | 
|  | 63 | + } | 
|  | 64 | + } catch (error) { | 
|  | 65 | + console.log(`*** SDK failed to initialize: ${error}`); | 
|  | 66 | + process.exit(1); | 
|  | 67 | + } | 
|  | 68 | + | 
|  | 69 | +} | 
|  | 70 | + | 
|  | 71 | +main(); | 
0 commit comments