|
1 | 1 | // 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'; |
3 | 8 |
|
4 | 9 | // Set clientSideID to your LaunchDarkly client-side ID |
5 | 10 | const clientSideID = 'LD_CLIENT_SIDE_ID'; |
@@ -98,6 +103,26 @@ function buildUI() { |
98 | 103 | streamSection.appendChild(btnUndef); |
99 | 104 | controls.appendChild(streamSection); |
100 | 105 |
|
| 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 | + |
101 | 126 | // Log |
102 | 127 | const logSection = el('div'); |
103 | 128 | logSection.appendChild(el('h3')); |
@@ -149,6 +174,15 @@ function updateEvtStatus() { |
149 | 174 | } |
150 | 175 | } |
151 | 176 |
|
| 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 | + |
152 | 186 | function updateStreamStatus(value: boolean | undefined) { |
153 | 187 | const label = document.getElementById('stream-status')!; |
154 | 188 | if (value === true) { |
@@ -240,6 +274,27 @@ const main = async () => { |
240 | 274 | log('setStreaming(undefined)'); |
241 | 275 | }); |
242 | 276 |
|
| 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 | + |
243 | 298 | // Start |
244 | 299 | client.start(); |
245 | 300 | const { status } = await client.waitForInitialization(); |
|
0 commit comments