forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_store_event_wrapper.js
61 lines (53 loc) · 1.92 KB
/
feature_store_event_wrapper.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
var dataKind = require('./versioned_data_kind');
function FeatureStoreEventWrapper(featureStore, emitter) {
function differ(key, oldValue, newValue) {
if(newValue && oldValue && newValue.version < oldValue.version) return;
setTimeout(function(){
emitter.emit("update", newValue);
emitter.emit(`update:${key}`, oldValue, newValue);
}, 0);
}
return {
get: featureStore.get.bind(featureStore),
all: featureStore.all.bind(featureStore),
initialized: featureStore.initialized.bind(featureStore),
close: featureStore.close.bind(featureStore),
init: function(newData, callback) {
featureStore.all(dataKind.features, function(oldFlags){
featureStore.init(newData, function(){
var allFlags = {};
var newFlags = newData[dataKind.features.namespace] || {};
Object.assign(allFlags, oldFlags, newFlags);
var handledFlags = {};
for (var key in allFlags) {
if(handledFlags[key]) continue;
differ(key, oldFlags[key], newFlags[key]);
handledFlags[key] = true;
}
callback && callback.apply(null, arguments);
});
});
},
delete: function(kind, key, version, callback) {
featureStore.get(kind, key, function(oldFlag) {
featureStore.delete(kind, key, version, function() {
if (kind === dataKind.features) {
differ(key, oldFlag, {});
}
callback && callback.apply(null, arguments);
});
});
},
upsert: function(kind, newItem, callback) {
featureStore.get(kind, newItem.key, function(oldItem) {
featureStore.upsert(kind, newItem, function() {
if (kind === dataKind.features) {
differ(oldItem ? oldItem.key : null, oldItem, newItem);
}
callback && callback.apply(null, arguments);
});
});
}
}
}
module.exports = FeatureStoreEventWrapper;