forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_store.js
98 lines (79 loc) · 1.98 KB
/
feature_store.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
// An in-memory feature store with an async interface.
// It's async as other implementations (e.g. the RedisFeatureStore)
// may be async, and we want to retain interface compatibility.
var noop = function(){};
function InMemoryFeatureStore() {
var store = {flags:{}};
store.get = function(key, cb) {
cb = cb || noop;
if (this.flags.hasOwnProperty(key)) {
var flag = this.flags[key];
if (!flag || flag.deleted) {
cb(null);
} else {
cb(clone(flag));
}
} else {
cb(null);
}
}
store.all = function(cb) {
cb = cb || noop;
var results = {};
for (var key in this.flags) {
if (this.flags.hasOwnProperty(key)) {
var flag = this.flags[key];
if (flag && !flag.deleted) {
results[key] = clone(flag);
}
}
}
cb(results);
}
store.init = function(flags, cb) {
cb = cb || noop;
this.flags = flags;
this.init_called = true;
cb();
}
store.delete = function(key, version, cb) {
cb = cb || noop;
if (this.flags.hasOwnProperty(key)) {
var old = this.flags[key];
if (old && old.version < version) {
old.deleted = true;
old.version = version;
this.flags[key] = old;
}
} else {
this.flags[key] = old;
}
cb();
}
store.upsert = function(key, flag, cb) {
cb = cb || noop;
var old = this.flags[key];
if (this.flags.hasOwnProperty(key)) {
var old = this.flags[key];
if (old && old.version < flag.version) {
this.flags[key] = flag;
}
} else {
this.flags[key] = flag;
}
cb();
}
store.initialized = function() {
return this.init_called === true;
}
store.close = function() {
// Close on the in-memory store is a no-op
}
return store;
}
// Deep clone an object. Does not preserve any
// functions on the object
function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
module.exports = InMemoryFeatureStore;