-
Notifications
You must be signed in to change notification settings - Fork 1
/
browser-example.js
63 lines (54 loc) · 1.75 KB
/
browser-example.js
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
import {initialize, basicLogger} from "launchdarkly-js-client-sdk";
// assuming web pack support for json import
import { name as appName, version as appVersion } from "./package.json";
function initializeLaunchDarkly(clientSideID, context, options={}) {
return initialize(clientSideID, context, Object.assign(getLDConfig(), options));
}
function getLDConfig() {
const options = {
// Will be used for upcoming features :)
application: {
id: appName,
version: appVersion
},
privateAttributes: [],
};
if(isProductionBuild()) {
Object.assign(options, {
bootstrap: "localStorage"
})
}
if (isDevelopmentBuild()) {
Object.assign(options, {
// disable private attributes in development for easier debugging
privateAttributes: [],
allAttributesPrivate: false,
evaluationReasons: true,
bootstrap: false,
logger: basicLogger({
level: 'debug',
})
})
}
if (isAutomatedTest()) {
Object.assign(options, {
sendEvents: false,
bootstrap: false,
})
}
return options
}
// We are catching the error to prevent
function waitForInitialization(ldClient, timeout=500) {
return deadline(ldClient.waitForInitialization(), timeout).catch((err) => {
console.error("LaunchDarkly initialization failed. Fallback values or bootstrap values will be served", err)
})
}
function deadline(promise, timeout=500) {
const timeout = new Promise((resolve, reject) => {
setTimeout(() => {
reject(new Error("Timeout exceeded"))
}, timeout)
})
return Promise.race([promise, timeout])
}