Skip to content

Commit

Permalink
feat: history implementations return promise for getAll
Browse files Browse the repository at this point in the history
closes #5
  • Loading branch information
cromwellryan authored and robtarr committed Apr 8, 2015
1 parent fa04acc commit d3438c3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
25 changes: 12 additions & 13 deletions lib/dashing.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ var fs = require('fs')
, express = require('express')
, Mincer = require('mincer')
, coffee = require('coffee-script')
, InMemoryHistory = require('./inMemoryHistory');

global.SCHEDULER = require('node-schedule');
, InMemoryHistory = require('./inMemoryHistory')
, Promise = require('bluebird');

module.exports.logger = logger = require('./logger');
module.exports.Dashing = function Dashing() {
Expand Down Expand Up @@ -103,12 +102,13 @@ module.exports.Dashing = function Dashing() {
});
res.write('\n');
res.write(Array(2049).join(' ') + '\n'); // 2kb padding for IE
res.write(latest_events());
res.flush(); // need to flush with .compress()
latest_events()
.done( function(events) {
res.write(events);
res.flush(); // need to flush with .compress()
});

req.on('close', function() {
delete connections[conn.id];
});
req.on('close', function() { delete connections[conn.id]; });
});

app.get('/', function(req, res) {
Expand Down Expand Up @@ -190,11 +190,10 @@ module.exports.Dashing = function Dashing() {
}

function latest_events() {
var str = [];
dashing.history.forEach( function(_id, data) {
str.push(data);
});
return str.join('');
return Promise.resolve(dashing.history.getAll()) // assure trusted promise
.reduce( function(agg, next) {
return agg + next;
});
}

function first_dashboard(fn) {
Expand Down
16 changes: 10 additions & 6 deletions lib/inMemoryHistory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var logger = require('./logger');
var Promise = require("bluebird")
, logger = require('./logger');

var InMemoryHistory = module.exports = function() {
var self = this;
Expand All @@ -13,11 +14,14 @@ var InMemoryHistory = module.exports = function() {
return self._event_data[id];
};

self.forEach = function(func) {
var history = self._event_data;
for (var id in history) {
func(id, history[id]);
}
self.getAll = function() {
var values = Object
.keys(self._event_data)
.map( function(key) {
return self._event_data[key];
});

return new Promise.resolve(values);
};

return self;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"dependencies": {
"async": "~0.2.7",
"bluebird": "^2.9.24",
"coffee-script": "~1.6.2",
"commander": "~2.1.0",
"consolidate": "~0.10.0",
Expand All @@ -16,7 +17,6 @@
"jade": "~0.35.0",
"mincer": "~0.5.12",
"node-sass": "~0.7.0",
"node-schedule": "~0.1.8",
"prompt": "~0.2.11",
"request": "~2.30.0",
"unzip": "~0.1.7",
Expand Down

0 comments on commit d3438c3

Please sign in to comment.