1
- import { OpenFeature } from '@openfeature/js-sdk' ;
2
- import { init } from 'launchdarkly-node-server-sdk' ;
1
+ import { OpenFeature , ProviderEvents } from '@openfeature/server-sdk' ;
3
2
import { LaunchDarklyProvider } from '@launchdarkly/openfeature-node-server' ;
4
3
5
4
// Set sdkKey to your LaunchDarkly SDK key.
@@ -16,20 +15,19 @@ const context = {
16
15
kind : 'user'
17
16
} ;
18
17
19
- const ldClient = init ( sdkKey ) ;
20
- await ldClient . waitForInitialization ( ) ;
21
-
22
- OpenFeature . setProvider ( new LaunchDarklyProvider ( ldClient ) ) ;
23
- const client = OpenFeature . getClient ( ) ;
24
-
25
- const flagValue = await client . getBooleanValue ( featureFlagKey , false , context ) ;
26
-
27
- console . log ( `Feature flag '${ featureFlagKey } ' is ${ flagValue } for this context` ) ;
28
-
29
- // Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics
30
- // events to LaunchDarkly before the program exits. If analytics events are not delivered,
31
- // the context properties and flag usage statistics will not appear on your dashboard. In a
32
- // normal long-running application, the SDK would continue running and events would be
33
- // delivered automatically in the background.
34
- await ldClient . flush ( ) ;
35
- ldClient . close ( ) ;
18
+ OpenFeature . setProvider ( new LaunchDarklyProvider ( sdkKey ) ) ;
19
+
20
+ OpenFeature . addHandler ( ProviderEvents . Ready , async ( _eventDetails ) => {
21
+ const client = OpenFeature . getClient ( ) ;
22
+
23
+ const flagValue = await client . getBooleanValue ( featureFlagKey , false , context ) ;
24
+
25
+ console . log ( `Feature flag '${ featureFlagKey } ' is ${ flagValue } for this context` ) ;
26
+
27
+ // Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics
28
+ // events to LaunchDarkly before the program exits. If analytics events are not delivered,
29
+ // the context properties and flag usage statistics will not appear on your dashboard. In a
30
+ // normal long-running application, the SDK would continue running and events would be
31
+ // delivered automatically in the background.
32
+ await OpenFeature . close ( ) ;
33
+ } ) ;
0 commit comments