Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 30 additions & 43 deletions bullet-train-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const BulletTrain = class {
constructor(props) {
fetch = props.fetch;

this.checkFeatureEnabled = this.checkFeatureEnabled.bind(this);
this.getFlags = this.getFlags.bind(this);
this.getFlagsForUser = this.getFlagsForUser.bind(this);
this.getValue = this.getValue.bind(this);
this.getValueFromFeatures = this.getValueFromFeatures.bind(this);
this.hasFeature = this.hasFeature.bind(this);
this.init = this.init.bind(this);

this.getJSON = function (url, method) {
const { environmentID } = this;
return fetch(url + '?format=json', {
Expand All @@ -18,13 +26,10 @@ const BulletTrain = class {
};
}


getFlagsForUser(identity, self) {
const { onChange, onError, api, disableCache } = this;
getFlagsForUser (identity) {
const { onError, api } = this;
//Because timer functions get the timeout context we need to pass through the explicit self on timers
if (self === undefined) {
self = this;
}

const handleResponse = (res) => {
// Handle server response
let flags = {};
Expand All @@ -40,9 +45,6 @@ const BulletTrain = class {
};
}
});
self.oldFlags = userFlags[identity];
self.flags = userFlags[identity];

return userFlags;
};

Expand All @@ -56,12 +58,8 @@ const BulletTrain = class {
});
}

getFlags(self) {
//Because timer functions get the timeout context we need to pass through the explicit self on timers
if (self === undefined) {
self = this;
}
const { onChange, onError, api, disableCache } = self;
getFlags() {
const { onError, api } = this;

const handleResponse = (res) => {
// Handle server response
Expand All @@ -72,63 +70,51 @@ const BulletTrain = class {
value: feature.feature_state_value
};
});
self.oldFlags = flags;
self.flags = flags;
return flags;
};

return this.getJSON(api + "flags/")
.then(res => {
console.log(res);
return handleResponse(res);
}).catch(({ message }) => {
onError && onError({ message })
});
};

init({
environmentID,
api,
disableCache,
onError,
defaultFlags
}) {
environmentID,
api,
disableCache,
onError,
}) {

this.environmentID = environmentID;

this.api = api;
this.interval = null;
this.disableCache = disableCache;
this.onError = onError;
this.flags = Object.assign({}, defaultFlags) || {};
this.initialised = true;

if (!environmentID) {
throw ('Please specify a environment id');
}
if (api === undefined) {
this.api = config.api;
}
return;
}

getAllFlags() {
return this.flags;
}

getValue(key, userId) {
getValue (key, userId) {
if (userId) {
return this.getFlagsForUser(userId).then((flags) => {
return this.getValueFromFlags(key, flags);
return this.getValueFromFeatures(key, flags);
})
} else {
return this.getFlags().then((flags) => {
return this.getValueFromFlags(key, flags);
return this.getValueFromFeatures(key, flags);
});
}
}

hasFeature(key, userId) {
hasFeature (key, userId) {
if (userId) {
return this.getFlagsForUser(userId).then((flags) => {
return this.checkFeatureEnabled(key, flags);
Expand All @@ -139,22 +125,23 @@ const BulletTrain = class {
});
}
}
getValueFromFlags(key, flags) {
if (flags === undefined || flags[key] === undefined) {
return undefined;

getValueFromFeatures (key, flags) {
if (!flags) {
return null;
}
const flag = this.flags[key];
const flag = flags[key];
let res = null;
if (flag && flag.enabled) {
if (flag) {
res = flag.value;
}
//todo record check for value

return res;
}

checkFeatureEnabled(key, flags) {
if (flags === undefined || flags[key] === undefined) {
checkFeatureEnabled (key, flags) {
if (!flags) {
return false;
}
const flag = flags[key];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bullet-train-nodejs",
"version": "0.1.2",
"version": "0.1.3",
"description": "Bullet Train lets you manage features flags and remote config across web, mobile and server side applications. Deliver true Continuous Integration. Get builds out faster. Control who has access to new features.",
"main": "index.js",
"repository": {
Expand Down