Skip to content

Commit 4ba5b0b

Browse files
committed
chore: addressing PR comments
1 parent ba50d1b commit 4ba5b0b

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

packages/sdk/browser/example/.env.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
LD_CLIENT_SIDE_ID=
33

44
# Set LD_FLAG_KEY to the feature flag key you want to evaluate
5-
LD_FLAG_KEY=
5+
LD_FLAG_KEY=

packages/sdk/browser/example/src/app.ts

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,47 @@ const context = {
1515
};
1616

1717
const div = document.createElement('div');
18+
const statusBox = document.createElement('div');
19+
20+
document.body.appendChild(statusBox);
1821
document.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

2126
const 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

5861
main();

0 commit comments

Comments
 (0)