Skip to content

Commit da5ccdc

Browse files
committed
feat: add connection mode buttons to FDv2 example app
Add a Connection Mode control section with buttons for each FDv2ConnectionMode (streaming, polling, offline, one-shot, background) and a Clear button to reset to automatic mode selection.
1 parent e3ea7c1 commit da5ccdc

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

  • packages/sdk/browser/example-fdv2/src

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// Temporary app for testing FDv2 functionality.
2-
import { basicLogger, createClient, type LDClient } from '@launchdarkly/js-client-sdk';
2+
import {
3+
basicLogger,
4+
createClient,
5+
type FDv2ConnectionMode,
6+
type LDClient,
7+
} from '@launchdarkly/js-client-sdk';
38

49
// Set clientSideID to your LaunchDarkly client-side ID
510
const clientSideID = 'LD_CLIENT_SIDE_ID';
@@ -98,6 +103,26 @@ function buildUI() {
98103
streamSection.appendChild(btnUndef);
99104
controls.appendChild(streamSection);
100105

106+
// Connection mode control
107+
const modeSection = el('div');
108+
modeSection.appendChild(el('h3'));
109+
modeSection.querySelector('h3')!.textContent = 'Connection Mode';
110+
const modeStatus = el('span', { id: 'mode-status' });
111+
modeStatus.textContent = 'undefined (automatic)';
112+
modeSection.appendChild(modeStatus);
113+
modeSection.appendChild(el('br'));
114+
const modes: FDv2ConnectionMode[] = ['streaming', 'polling', 'offline', 'one-shot', 'background'];
115+
for (const mode of modes) {
116+
const btn = el('button', { id: `btn-mode-${mode}` });
117+
btn.textContent = mode;
118+
modeSection.appendChild(btn);
119+
modeSection.appendChild(text(' '));
120+
}
121+
const btnModeClear = el('button', { id: 'btn-mode-clear' });
122+
btnModeClear.textContent = 'Clear';
123+
modeSection.appendChild(btnModeClear);
124+
controls.appendChild(modeSection);
125+
101126
// Log
102127
const logSection = el('div');
103128
logSection.appendChild(el('h3'));
@@ -149,6 +174,15 @@ function updateEvtStatus() {
149174
}
150175
}
151176

177+
function updateModeStatus(mode: FDv2ConnectionMode | undefined) {
178+
const label = document.getElementById('mode-status')!;
179+
if (mode !== undefined) {
180+
label.textContent = `${mode} (override active)`;
181+
} else {
182+
label.textContent = 'undefined (automatic)';
183+
}
184+
}
185+
152186
function updateStreamStatus(value: boolean | undefined) {
153187
const label = document.getElementById('stream-status')!;
154188
if (value === true) {
@@ -240,6 +274,27 @@ const main = async () => {
240274
log('setStreaming(undefined)');
241275
});
242276

277+
// Connection mode controls
278+
const connectionModes: FDv2ConnectionMode[] = [
279+
'streaming',
280+
'polling',
281+
'offline',
282+
'one-shot',
283+
'background',
284+
];
285+
for (const mode of connectionModes) {
286+
document.getElementById(`btn-mode-${mode}`)!.addEventListener('click', () => {
287+
client.setConnectionMode(mode);
288+
updateModeStatus(mode);
289+
log(`setConnectionMode('${mode}')`);
290+
});
291+
}
292+
document.getElementById('btn-mode-clear')!.addEventListener('click', () => {
293+
client.setConnectionMode(undefined);
294+
updateModeStatus(undefined);
295+
log('setConnectionMode(undefined)');
296+
});
297+
243298
// Start
244299
client.start();
245300
const { status } = await client.waitForInitialization();

0 commit comments

Comments
 (0)