-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventSourceClient.js
More file actions
123 lines (99 loc) · 3.08 KB
/
eventSourceClient.js
File metadata and controls
123 lines (99 loc) · 3.08 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
const EventSource = require("eventsource");
const eventTypes = require('./lib/eventTypes');
const FeatureState = require('./featureState');
const AnalyticsCollector = require('./analyticsCollector');
const handleUndefinedFeature = require('./lib/handleUndefinedFeature');
class EventSourceClient {
constructor(config) {
this.config = config;
this.features = {};
this.hasData = false;
this.analyticsCollectors = [];
const options = {
headers: { Authorization: config.sdkKey}
}
const apiClient = new EventSource(config.getServerAddress(), options);
this.apiClient = apiClient;
}
start() {
// start listening for SSE messages
this.handleEvents();
this.handleErrors();
}
handleEvents() {
this.apiClient.onmessage = (event) => {
// JSON parse the entire SSE message (in the data property)
const data = JSON.parse(event.data);
// get the eventType
const eventType = data.eventType;
// get the payload
const payload = data.payload;
// call the proper handler function based on the eventType
switch (eventType) {
case eventTypes.UPDATE_FEATURE:
this.handleUpdateFeature(payload);
return
case eventTypes.ALL_FEATURES:
this.handleAllFeatures(payload);
return
case eventTypes.CREATE_CONNECTION:
return
}
}
}
handleErrors() {
this.apiClient.onerror = (error) => {
console.log("Event source failed: ", error);
}
}
handleUpdateFeature(payload) {
const { key } = payload;
// create a new feature state and override the previous value
this.features[key] = new FeatureState(payload);
}
handleAllFeatures(allFeatures) {
allFeatures = JSON.parse(allFeatures);
// initialize a new object of feature states and override the previous value
const featureStates = {};
allFeatures.forEach((featureStateParams) => {
const { title, is_active, rollout } = featureStateParams;
const modifiedFeatureStateParams = {
value: is_active,
title,
strategy: {
percentage: rollout
}
}
featureStates[title] = new FeatureState(modifiedFeatureStateParams);
})
this.features = featureStates;
this.hasData = true;
}
getFeature(key, defaultValue) {
const featureState = this.getFeatureState(key);
if (!featureState) {
return handleUndefinedFeature(key, defaultValue);
}
const value = featureState.value;
return value;
}
getFeatureState(key) {
return this.features[key];
}
addGoogleAnalyticsCollector({ trackingId, clientId, strictCidFormat }) {
const analyticsCollector = new AnalyticsCollector({ trackingId, clientId, strictCidFormat });
this.analyticsCollectors.push(analyticsCollector);
return analyticsCollector;
}
logEvent({ category, action, label, value }) {
this.analyticsCollectors.forEach((analyticsCollector) => {
analyticsCollector.logEvent({
category,
action,
label,
value
});
})
}
}
module.exports = EventSourceClient