Skip to content

Commit

Permalink
Using Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Robsahm committed Dec 1, 2016
1 parent 61dc595 commit f060920
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ if(!process.env.CONFIG_REQUEST_URL || !process.env.CONFIG_REQUEST_INTERVAL) {
console.error("Environment variables CONFIG_REQUEST_URL and CONFIG_REQUEST_INTERVAL must be set");
}

let config;

const requestConfig = (webSocket, environment) => {
webSocket.send(environment);
};

const startUpdateConfig = (environment) => {
const initializeConfigUpdate = (environment) => {
let retryNumOfTimes = 0;
let interval;

const connect = () => {
const connect = (resolve, reject) => {
const webSocket = new ws(url.resolve(process.env.CONFIG_REQUEST_URL, environment));

webSocket.on("open", () => {
Expand All @@ -28,7 +30,9 @@ const startUpdateConfig = (environment) => {
});

webSocket.on("message", (message) => {
process.env = Object.assign(process.env, JSON.parse(message));
console.log("update!");
config = JSON.parse(message);
resolve(config);
});

webSocket.on("error", (error) => {
Expand All @@ -37,12 +41,23 @@ const startUpdateConfig = (environment) => {

webSocket.on("close", () => {
clearInterval(interval);
setTimeout(connect, Math.ceil(Math.pow(Math.E, retryNumOfTimes)) * 100);
setTimeout(connect, Math.ceil(Math.pow(Math.E, retryNumOfTimes)) * 100, resolve, reject);
retryNumOfTimes += 1;
});
};

connect();
return new Promise(connect(resolve, reject));
};

module.exports = startUpdateConfig;
const getConfig = () => {
if (!config) {
console.error("Config update not initialized! Use initializeConfigUpdate()");
}

return config;
}

module.exports = {
initializeConfigUpdate,
getConfig
};

0 comments on commit f060920

Please sign in to comment.