Last active
November 15, 2016 17:07
-
-
Save jimbol/2cae55f9f6afadc3e1b34b65f9f2735a to your computer and use it in GitHub Desktop.
Features module concept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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