Skip to content

Instantly share code, notes, and snippets.

@ncjones
Forked from alistair/feature.toggles.js
Last active August 29, 2015 14:16
Show Gist options
  • Save ncjones/f1a18c21eb427cb9a694 to your computer and use it in GitHub Desktop.
Save ncjones/f1a18c21eb427cb9a694 to your computer and use it in GitHub Desktop.
======================================================
======================================================
//File toggle.js
// All the logic for getting toggle information
exports = function FeatureToggle(toggleName, offFunc, onFunc) {
return function() {
if (toggleOn(toggleName))
{
onFunc.apply(this, arguments); //
} else
{
offFunc.apply(this, arguments);
}
}
}
======================================================
======================================================
// file profile-store.js
var toggle = require('FeatureToggle');
var profileStoreOn = {
method1 : function() { /* does some stuff */ }
method2 : function() {}
};
var profileStoreOff = function() {
method1 : function() { /* does some nothing, it is a null object */ }
method2 : function() {}
}
exports = toggle('feature', profileStoreOn, profileStoreOff);
======================================================
======================================================
// file some-other-store.js
var toggle = require('FeatureToggle');
var on = function() { /* does some stuff */ };
var off = function() { /* does some other stuff */ };
exports = {
method1 : toggle('tooglename', on, off)
};
======================================================
======================================================
// file some-file-using-those-others
var profileStore = require('profile-store');
var otherStore = require('some-other-store');
//What is important to understand here is that guess what....
//the consuming code doesn't give a ... about whether there is a toggle or not.
profileStore.method1();
otherStore.method1();
// we can do it
// we can write code without if statements.
// we can fix it!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment