Skip to content

Commit

Permalink
candleStore.js audit & refactor (ECMA compliance)
Browse files Browse the repository at this point in the history
No changes to the logic were made in this commit.

Technically the code executed and ran on node.js...
now it passes jshint / jslint and is ECMA compliant.
  • Loading branch information
Sarah White committed Jul 5, 2014
1 parent 7c7b9b5 commit d61cc6b
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions core/candleStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var Day = function(day) {
this.state = "uninitialized";
this.candles = [];
this.filename = "history-" + day.toString() + ".csv";
}
};

Day.prototype.addCandles = function(candles) {
this.candles = this.candles.concat(candles);
Expand All @@ -42,12 +42,12 @@ var Store = function() {
this.unzip,
this.readFile
);
}
};

Store.prototype.openDay = function(day, callback) {
// Load only if the open day changed, or we never opened a day
if(this.day == null || day != this.day.day) {
prepareNewDay(day);
if(this.day === null || day !== this.day.day) {
this.prepareNewDay(day);
this.loadDay(function(err, candles) {
if(!err) {
this.day.addCandles(candles);
Expand All @@ -56,73 +56,73 @@ Store.prototype.openDay = function(day, callback) {
callback(err, candles);
});
}
}
};

Store.prototype.loadDay = function(day, callback) {
this.read(day.filename, function(candles) {
callback(null, candles);
});
}
};

Store.prototype.prepareNewDay = function(day) {
if(this.day.state != 'loading') {
if(this.day.state !== 'loading') {
// Do we need to keep
this.day.state = 'closing';
this.day = new Day(day);
}
}
};

// Queue's candles to be added as soon as a day is loaded
Store.prototype.addCandles = function(candles) {
//NOTE: this.queue is array of arrays.
this.queue.push(candles);
this.flush();
}
};

// If there is a day in open state, append all queued candles to it.
Store.prototype.flush = function() {
//TODO(yin): Help, this.day.state can get easily stuck locked.
if(this.queue.length > 0 && this.day != null && this.day.state = 'open') {
if(this.queue.length > 0 && this.day !== null && this.day.state === 'open') {
this.day.addCandles(_.flatten(this.queue));
this.queue = [];
this.day.state = 'saving';
this.write(this.day.filename, this.day.candles, function(err) {
this.day.state = 'open';
})
});
}
}
};

Store.prototype.toCSV = function(file, candles, next) {
var csv = _.map(candles, function(properties) {
return _.values(properties).join(',');
}).join('\n');

next(null, file, csv);
}
};

Store.prototype.deflate = function(file, csv, next) {
zlib.deflate(csv, function(err, buffer) {
next(err, file, buffer);
});
}
};

Store.prototype.writeFile = function(file, gzip, next) {
fs.writeFile(this.directory + file, gzip, function(err) {
next(err);
});
}
};

Store.prototype.readFile = function(file, next) {
fs.readFile(this.directory + file, function(err, buffer) {
next(err, buffer);
});
}
};

Store.prototype.unzip = function(buffer, next) {
zlib.unzip(buffer, function(err, buffer) {
next(err, buffer.toString());
});
}
};

Store.prototype.toArray = function(csv, next) {
var f = parseFloat;
Expand All @@ -136,13 +136,13 @@ Store.prototype.toArray = function(csv, next) {
l: f(l[3]),
c: f(l[4]),
p: f(l[5])
}
};
});

next(obj);
}
};

//TODO(yin):Exported for tests
Store.Day = Day
Store.Day = new Day();

module.exports = Store;

0 comments on commit d61cc6b

Please sign in to comment.