-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
launch-darkly-client-remote.js
116 lines (85 loc) · 2.94 KB
/
launch-darkly-client-remote.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
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
import Service from '@ember/service';
import { getOwner } from '@ember/application';
import { assert, warn } from '@ember/debug';
import { run } from '@ember/runloop';
import Evented from '@ember/object/evented';
import RSVP from 'rsvp';
import NullClient from 'ember-launch-darkly/lib/null-client';
export default Service.extend(Evented, {
_client: null,
init() {
this._super(...arguments);
},
initialize(user = {}) {
let { clientSideId, streaming = false } = this._config();
assert('ENV.launchDarkly.clientSideId must be specified in config/environment.js', clientSideId);
if (!clientSideId) {
warn('ENV.launchDarkly.clientSideId not specified. Defaulting all feature flags to "false"', false, { id: 'ember-launch-darkly.client-id-not-specified' });
this.set('_client', NullClient);
return RSVP.resolve();
}
assert('user.key must be specified in initilize payload', user.key);
if (!user.key) {
warn('user.key not specified in initialize payload. Defaulting all feature flags to "false"', false, { id: 'ember-launch-darkly.user-key-not-specified' });
this.set('_client', NullClient);
return RSVP.resolve();
}
if (!window.LDClient) {
warn('Launch Darkly JS client not found. Defaulting all feature flags to "false"', false, { id: 'ember-launch-darkly.client-not-found' });
this.set('_client', NullClient);
return RSVP.resolve();
}
return this._initialize(clientSideId, user, streaming);
},
identify(user) {
return this._identify(user);
},
allFlags() {
return this.get('_client').allFlags();
},
variation(key, defaultValue) {
return this.get('_client').variation(key, defaultValue);
},
_config() {
let appConfig = getOwner(this).resolveRegistration('config:environment');
return appConfig.launchDarkly || {};
},
_initialize(id, user, streamingOptions) {
return new RSVP.Promise((resolve, reject) => {
let client = window.LDClient.initialize(id, user);
client.on('ready', () => {
this.set('_client', client);
run(null, resolve);
});
client.on('change', settings => {
Object.keys(settings).forEach(key => {
if (this._shouldTriggerEvent(key, streamingOptions)) {
this.trigger(key);
}
});
});
run.later(this, () => {
if (!this.get('_client')) {
run(null, reject);
}
}, 10000);
})
},
_identify(user) {
return new RSVP.Promise(resolve => {
this.get('_client').identify(user, null, resolve);
})
},
_shouldTriggerEvent(key, streamingOptions) {
if (streamingOptions === true) {
return true;
}
if (typeof streamingOptions === 'object') {
if (streamingOptions.allExcept && Array.isArray(streamingOptions.allExcept)) {
return streamingOptions.allExcept.indexOf(key) === -1;
}
return streamingOptions[key];
}
return false;
}
});