@@ -15,44 +15,47 @@ const context = {
1515} ;
1616
1717const div = document . createElement ( 'div' ) ;
18+ const statusBox = document . createElement ( 'div' ) ;
19+
20+ document . body . appendChild ( statusBox ) ;
1821document . body . appendChild ( div ) ;
19- div . appendChild ( document . createTextNode ( 'Initializing...' ) ) ;
22+
23+ div . appendChild ( document . createTextNode ( 'No flag evaluations yet' ) ) ;
24+ statusBox . appendChild ( document . createTextNode ( 'Initializing...' ) ) ;
2025
2126const main = async ( ) => {
22- try {
23- const ldclient = initialize ( clientSideID ) ;
24- const render = ( ) => {
25- const flagValue = ldclient . variation ( flagKey , false ) ;
26- const label = `The ${ flagKey } feature flag evaluates to ${ flagValue } .` ;
27- document . body . style . background = flagValue ? '#00844B' : '#373841' ;
28- div . replaceChild ( document . createTextNode ( label ) , div . firstChild as Node ) ;
29- } ;
30-
31- ldclient . on ( 'error' , ( ) => {
32- div . replaceChild (
33- document . createTextNode ( 'Error caught in client SDK' ) ,
34- div . firstChild as Node ,
35- ) ;
36- } ) ;
37-
38- // Listen for flag changes
39- ldclient . on ( 'change' , ( ) => {
40- render ( ) ;
41- } ) ;
42-
43- const { status } = await ldclient . identify ( context ) ;
44- if ( status === 'completed' ) {
45- render ( ) ;
46- } else if ( status === 'error' ) {
47- div . replaceChild ( document . createTextNode ( 'Error identifying client' ) , div . firstChild as Node ) ;
48- }
49- } catch ( error ) {
50- div . replaceChild (
51- document . createTextNode ( `Error initializing LaunchDarkly client: ${ error } ` ) ,
52- div . firstChild as Node ,
27+ const ldclient = initialize ( clientSideID ) ;
28+ const render = ( ) => {
29+ const flagValue = ldclient . variation ( flagKey , false ) ;
30+ const label = `The ${ flagKey } feature flag evaluates to ${ flagValue } .` ;
31+ document . body . style . background = flagValue ? '#00844B' : '#373841' ;
32+ div . replaceChild ( document . createTextNode ( label ) , div . firstChild as Node ) ;
33+ } ;
34+
35+ ldclient . on ( 'error' , ( ) => {
36+ statusBox . replaceChild (
37+ document . createTextNode ( 'Error caught in client SDK' ) ,
38+ statusBox . firstChild as Node ,
39+ ) ;
40+ } ) ;
41+
42+ // Listen for flag changes
43+ ldclient . on ( 'change' , ( ) => {
44+ render ( ) ;
45+ } ) ;
46+
47+ const { status } = await ldclient . identify ( context ) ;
48+
49+ if ( status === 'completed' ) {
50+ statusBox . replaceChild ( document . createTextNode ( 'Initialized' ) , statusBox . firstChild as Node ) ;
51+ } else if ( status === 'error' ) {
52+ statusBox . replaceChild (
53+ document . createTextNode ( 'Error identifying client' ) ,
54+ statusBox . firstChild as Node ,
5355 ) ;
54- document . body . style . background = '#373841' ;
5556 }
57+
58+ render ( ) ;
5659} ;
5760
5861main ( ) ;
0 commit comments