Skip to content

Instantly share code, notes, and snippets.

@jimbol
Last active November 15, 2016 17:07
Show Gist options
  • Save jimbol/2cae55f9f6afadc3e1b34b65f9f2735a to your computer and use it in GitHub Desktop.
Save jimbol/2cae55f9f6afadc3e1b34b65f9f2735a to your computer and use it in GitHub Desktop.
Features module concept
// Having some sort of feature flags allows us to
// - write centralized logic around when to show features
// - develop features in smaller pieces behind feature flags
// - turn on/off features at will
// In a view, effect, whereever
import features from '../features'
if (features('showFooBar')) {
// display FooBar
}
// In features.development.js
const developmentFeatures = {
showFooBar: true,
showBamWhack: false,
}
export developmentFeatures;
// in features.production.js is slightly different
// we dont want to showFooBar yet in prod
const productionFeatures = {
showFooBar: false,
showBamWhack: false,
}
export productionFeatures;
// so why is features.showFooBar a function?
// it allows us to set up experiments or other logic
const productionFeatures = {
// TODO: find a way to pass state in
showFooBar: (state) => return state.settings.showFooBar || true,
showBamWhack: () => return inBamWhackExperiment() || false,
}
// The module itself is pretty simple
let featureHash;
if (env.NODE_ENV === 'production') {
featureHash = require('featues.production.js');
} else {
featureHash = require('featues.development.js');
}
const features = function features(key){
if (!featureHash[key]) return console.error(`Feature '${key}' does not exist.`);
if (typeof featureHash[key] === 'function') return featureHash[key]();
return featureHash[key];
};
export default features;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment