From d3438c32df722d092bd3c6ec2181fd23fbb2c1ec Mon Sep 17 00:00:00 2001 From: Ryan Cromwell Date: Sun, 5 Apr 2015 00:37:27 -0400 Subject: [PATCH] feat: history implementations return promise for getAll closes #5 --- lib/dashing.js | 25 ++++++++++++------------- lib/inMemoryHistory.js | 16 ++++++++++------ package.json | 2 +- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/lib/dashing.js b/lib/dashing.js index e41cd44..c178e4f 100644 --- a/lib/dashing.js +++ b/lib/dashing.js @@ -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() { @@ -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) { @@ -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) { diff --git a/lib/inMemoryHistory.js b/lib/inMemoryHistory.js index fd0ecb4..39aab41 100644 --- a/lib/inMemoryHistory.js +++ b/lib/inMemoryHistory.js @@ -1,4 +1,5 @@ -var logger = require('./logger'); +var Promise = require("bluebird") + , logger = require('./logger'); var InMemoryHistory = module.exports = function() { var self = this; @@ -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; diff --git a/package.json b/package.json index a058476..a4bf4a4 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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",