Skip to content

Commit

Permalink
refactor: featureGate uses PhStore instead of localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Dec 28, 2023
1 parent 57cd077 commit 0fc832a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/utils/ExtensionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ define(function (require, exports, module) {
// we should still create an empty one, so we can attach
// disabled property on it in case it's disabled
let disabled,
defaultDisabled = JSON.parse(localStorage.getItem(Package.DEFAULT_DISABLED_EXTENSIONS_KEY) || "[]");
defaultDisabled = JSON.parse(PhStore.getItem(Package.DEFAULT_DISABLED_EXTENSIONS_KEY) || "[]");
if (Array.isArray(defaultDisabled) && defaultDisabled.indexOf(baseExtensionUrl) !== -1) {
console.warn("Extension has been disabled on startup: " + baseExtensionUrl);
disabled = true;
Expand Down
13 changes: 12 additions & 1 deletion src/utils/FeatureGate.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ define(function (require, exports, module) {
* @type {function}
*/
function isFeatureEnabled(featureName) {
let userOverRide = localStorage.getItem(`${featureName}`);
let userOverRide = PhStore.getItem(`FeatureGate-${featureName}`);
if(userOverRide === ENABLED){
return true;
} else if(userOverRide === DISABLED){
Expand All @@ -115,11 +115,22 @@ define(function (require, exports, module) {
return _FeatureGateMap[featureName] === true;
}

/**
* Sets the enabled state of a specific feature in the application.
*
* @param {string} featureName - The name of the feature to be modified.
* @param {boolean} isEnabled - A boolean flag indicating whether the feature should be enabled (true) or disabled (false).
*/
function setFeatureEnabled(featureName, isEnabled) {
PhStore.setItem(`FeatureGate-${featureName}`, isEnabled ? ENABLED : DISABLED);
}

EventDispatcher.makeEventDispatcher(exports);
// Public API
exports.registerFeatureGate = registerFeatureGate;
exports.getAllRegisteredFeatures = getAllRegisteredFeatures;
exports.isFeatureEnabled = isFeatureEnabled;
exports.setFeatureEnabled = setFeatureEnabled;
// Events
exports.FEATURE_REGISTERED = FEATURE_REGISTERED;
});
19 changes: 7 additions & 12 deletions test/spec/FeatureGate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,14 @@ define(function (require, exports, module) {
}, "Feature gate registration notification");
});

it("user should be able to override feature in localstorage", function () {
const FEATURENAME = "feature3",
STORAGE_KEY = `${FEATURENAME}`;
localStorage.removeItem(STORAGE_KEY);
FeatureGate.registerFeatureGate(FEATURENAME, true);
it("user should be able to override feature enable/disable in FeatureGate", function () {
const FEATURE_NAME = "feature3";
FeatureGate.registerFeatureGate(FEATURE_NAME, true);

localStorage.setItem(STORAGE_KEY, "enabled");
expect(FeatureGate.isFeatureEnabled(FEATURENAME)).toEqual(true);
localStorage.setItem(STORAGE_KEY, "disabled");
expect(FeatureGate.isFeatureEnabled(FEATURENAME)).toEqual(false);
localStorage.setItem(STORAGE_KEY, "invalidValueWillHonorDefaultValue");
expect(FeatureGate.isFeatureEnabled(FEATURENAME)).toEqual(true);
localStorage.removeItem(STORAGE_KEY);
FeatureGate.setFeatureEnabled(FEATURE_NAME, true);
expect(FeatureGate.isFeatureEnabled(FEATURE_NAME)).toEqual(true);
FeatureGate.setFeatureEnabled(FEATURE_NAME, false);
expect(FeatureGate.isFeatureEnabled(FEATURE_NAME)).toEqual(false);
});
});
});

0 comments on commit 0fc832a

Please sign in to comment.