-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathapp.ts
More file actions
250 lines (216 loc) · 7.59 KB
/
app.ts
File metadata and controls
250 lines (216 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { basicLogger, createClient, type LDClient } from '@launchdarkly/js-client-sdk';
// Set clientSideID to your LaunchDarkly client-side ID
const clientSideID = 'LD_CLIENT_SIDE_ID';
// Set flagKey to the feature flag key you want to evaluate
const flagKey = 'LD_FLAG_KEY';
const contexts = [
{ kind: 'user', key: 'user-1', name: 'Sandy' },
{ kind: 'user', key: 'user-2', name: 'Alex' },
{ kind: 'user', key: 'user-3', name: 'Jordan' },
{ kind: 'org', key: 'org-1', name: 'Acme Corp' },
];
let currentContextIndex = 0;
let eventHandlersRegistered = false;
let changeHandler: (() => void) | undefined;
let errorHandler: (() => void) | undefined;
function el(tag: string, attrs?: Record<string, string>): HTMLElement {
const e = document.createElement(tag);
if (attrs) {
Object.entries(attrs).forEach(([k, v]) => e.setAttribute(k, v));
}
return e;
}
function text(s: string): Text {
return document.createTextNode(s);
}
function formatContext(ctx: (typeof contexts)[0]): string {
return `${ctx.kind}:${ctx.key} (${ctx.name})`;
}
function buildUI() {
const container = el('div', { id: 'app' });
// Status
const statusBox = el('div', { id: 'status' });
statusBox.appendChild(text('Initializing...'));
container.appendChild(statusBox);
// Flag value
const flagBox = el('div', { id: 'flag' });
flagBox.appendChild(text('No flag evaluations yet'));
container.appendChild(flagBox);
// Controls
const controls = el('div', { id: 'controls' });
// Context switcher
const ctxSection = el('div');
ctxSection.appendChild(el('h3'));
ctxSection.querySelector('h3')!.textContent = 'Context';
const ctxLabel = el('span', { id: 'ctx-label' });
ctxLabel.textContent = formatContext(contexts[0]);
ctxSection.appendChild(ctxLabel);
ctxSection.appendChild(text(' '));
const ctxBtn = el('button', { id: 'btn-ctx' });
ctxBtn.textContent = 'Switch Context';
ctxSection.appendChild(ctxBtn);
controls.appendChild(ctxSection);
// Event handlers
const evtSection = el('div');
evtSection.appendChild(el('h3'));
evtSection.querySelector('h3')!.textContent = 'Event Handlers';
const evtStatus = el('span', { id: 'evt-status' });
evtStatus.textContent = 'Not registered';
evtSection.appendChild(evtStatus);
evtSection.appendChild(text(' '));
const evtBtn = el('button', { id: 'btn-evt' });
evtBtn.textContent = 'Register';
evtSection.appendChild(evtBtn);
controls.appendChild(evtSection);
// Streaming control
const streamSection = el('div');
streamSection.appendChild(el('h3'));
streamSection.querySelector('h3')!.textContent = 'Streaming';
const streamStatus = el('span', { id: 'stream-status' });
streamStatus.textContent = 'undefined (automatic)';
streamSection.appendChild(streamStatus);
streamSection.appendChild(el('br'));
const btnTrue = el('button', { id: 'btn-stream-true' });
btnTrue.textContent = 'Force On';
const btnFalse = el('button', { id: 'btn-stream-false' });
btnFalse.textContent = 'Force Off';
const btnUndef = el('button', { id: 'btn-stream-undef' });
btnUndef.textContent = 'Automatic';
streamSection.appendChild(btnTrue);
streamSection.appendChild(text(' '));
streamSection.appendChild(btnFalse);
streamSection.appendChild(text(' '));
streamSection.appendChild(btnUndef);
controls.appendChild(streamSection);
// Log
const logSection = el('div');
logSection.appendChild(el('h3'));
logSection.querySelector('h3')!.textContent = 'Event Log';
const logBox = el('div', { id: 'log' });
logSection.appendChild(logBox);
controls.appendChild(logSection);
container.appendChild(controls);
document.body.appendChild(container);
}
function log(msg: string) {
const logBox = document.getElementById('log')!;
const entry = el('div');
const time = new Date().toLocaleTimeString();
entry.textContent = `[${time}] ${msg}`;
logBox.insertBefore(entry, logBox.firstChild);
// Keep last 50 entries
while (logBox.children.length > 50) {
logBox.removeChild(logBox.lastChild!);
}
}
function renderFlag(client: LDClient) {
const flagValue = client.variation(flagKey, false);
const flagBox = document.getElementById('flag')!;
flagBox.textContent = `${flagKey} = ${JSON.stringify(flagValue)}`;
document.body.style.background = flagValue ? '#00844B' : '#373841';
}
function updateStatus(msg: string) {
document.getElementById('status')!.textContent = msg;
}
function updateCtxLabel() {
document.getElementById('ctx-label')!.textContent = formatContext(contexts[currentContextIndex]);
}
function updateEvtStatus() {
const evtStatus = document.getElementById('evt-status')!;
const btn = document.getElementById('btn-evt')!;
if (eventHandlersRegistered) {
evtStatus.textContent = 'Registered (change + error)';
btn.textContent = 'Unregister';
} else {
evtStatus.textContent = 'Not registered';
btn.textContent = 'Register';
}
}
function updateStreamStatus(value: boolean | undefined) {
const label = document.getElementById('stream-status')!;
if (value === true) {
label.textContent = 'true (forced on)';
} else if (value === false) {
label.textContent = 'false (forced off)';
} else {
label.textContent = 'undefined (automatic)';
}
}
function registerHandlers(client: LDClient) {
if (eventHandlersRegistered) return;
changeHandler = () => {
log('change event received');
renderFlag(client);
};
errorHandler = () => {
log('error event received');
};
client.on('change', changeHandler);
client.on('error', errorHandler);
eventHandlersRegistered = true;
updateEvtStatus();
log('Event handlers registered');
}
function unregisterHandlers(client: LDClient) {
if (!eventHandlersRegistered) return;
if (changeHandler) {
client.off('change', changeHandler);
changeHandler = undefined;
}
if (errorHandler) {
client.off('error', errorHandler);
errorHandler = undefined;
}
eventHandlersRegistered = false;
updateEvtStatus();
log('Event handlers unregistered');
}
const main = async () => {
buildUI();
const client = createClient(clientSideID, contexts[currentContextIndex], {
// @ts-ignore dataSystem is @internal — experimental FDv2 opt-in
dataSystem: {},
logger: basicLogger({ level: 'debug' }),
});
// Context switching
document.getElementById('btn-ctx')!.addEventListener('click', async () => {
currentContextIndex = (currentContextIndex + 1) % contexts.length;
const ctx = contexts[currentContextIndex];
updateCtxLabel();
log(`Identifying as ${formatContext(ctx)}...`);
const result = await client.identify(ctx);
log(`Identify result: ${result.status}`);
renderFlag(client);
});
// Event handler toggle
document.getElementById('btn-evt')!.addEventListener('click', () => {
if (eventHandlersRegistered) {
unregisterHandlers(client);
} else {
registerHandlers(client);
}
});
// Streaming controls
document.getElementById('btn-stream-true')!.addEventListener('click', () => {
client.setStreaming(true);
updateStreamStatus(true);
log('setStreaming(true)');
});
document.getElementById('btn-stream-false')!.addEventListener('click', () => {
client.setStreaming(false);
updateStreamStatus(false);
log('setStreaming(false)');
});
document.getElementById('btn-stream-undef')!.addEventListener('click', () => {
client.setStreaming(undefined);
updateStreamStatus(undefined);
log('setStreaming(undefined)');
});
// Start
client.start();
const { status } = await client.waitForInitialization();
updateStatus(`Initialized (${status}) - ${formatContext(contexts[currentContextIndex])}`);
log(`Initialization: ${status}`);
renderFlag(client);
};
main();