-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathfeatures.js
90 lines (75 loc) · 2.34 KB
/
features.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
/*eslint-disable no-extra-boolean-cast, no-console */
import Service from '@ember/service';
import { camelize } from '@ember/string';
const FeaturesService = Service.extend({
init() {
this._super(...arguments);
this._flags = Object.create(null);
this.setUnknownProperty = function(key) {
throw new Error(`Please use enable/disable to set feature flags. You attempted to set ${key}`);
};
},
setup(flags) {
this._resetFlags();
for (let flag in flags) {
if (flags.hasOwnProperty(flag)) {
if (!!flags[flag]) {
this.enable(flag);
} else {
this.disable(flag);
}
}
}
},
enable(flag) {
let normalizedFlag = this._normalizeFlag(flag);
this._flags[normalizedFlag] = true;
this.notifyPropertyChange(normalizedFlag);
},
disable(flag) {
let normalizedFlag = this._normalizeFlag(flag);
this._flags[normalizedFlag] = false;
this.notifyPropertyChange(normalizedFlag);
},
isEnabled(feature) {
let isEnabled = this._featureIsEnabled(feature);
if (this._logFeatureFlagMissEnabled() && !isEnabled) {
this._logFeatureFlagMiss(feature);
}
return isEnabled;
},
_resetFlags() {
this._flags = Object.create(null);
},
_featureIsEnabled(feature) {
let normalizeFeature = this._normalizeFlag(feature);
return this._flags[normalizeFeature] || false;
},
_logFeatureFlagMissEnabled() {
return !!this.get('config.LOG_FEATURE_FLAG_MISS');
},
_logFeatureFlagMiss(feature) {
if (console && console.info) {
console.info('Feature flag off:', feature);
}
},
_normalizeFlag(flag) {
return camelize(flag);
},
unknownProperty(key) {
return this.isEnabled(key);
}
});
// Use a native getter instead of a `volatile` computed property since those
// were deprecated as of Ember 3.9. The Object.defineProperty approach (from
// https://github.com/emberjs/ember.js/issues/17709#issuecomment-469941364 is
// used because defining native getters directly on EmberObject-based classes
// is only supported from Ember 3.9 on (https://github.com/emberjs/ember.js/pull/17710)
// and this preserves compatiblity until this addon drops support for older
// Ember versions.
Object.defineProperty(FeaturesService.prototype, 'flags', {
get() {
return Object.keys(this._flags);
}
});
export default FeaturesService;