Make DriveScanner.start() return an EventEmitter instance

This commit is contained in:
Juan Cruz Viotti 2016-01-18 13:27:27 -04:00
parent 491a0ae9c9
commit f218cc1b59
3 changed files with 55 additions and 4 deletions

View File

@ -117,6 +117,7 @@ app.controller('AppController', function($q, DriveScannerService, SelectionState
var angular = require('angular');
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var remote = window.require('remote');
if (window.mocha) {
@ -247,15 +248,32 @@ driveScanner.service('DriveScannerService', function($q, DriveScannerRefreshServ
* @function
* @public
*
* @description
* This function returns an event emitter instance
* that emits a `scan` event everything it scans
* the drives successfully.
*
* @param {Number} ms - interval milliseconds
* @returns {EventEmitter} event emitter instance
*
* @example
* DriveScannerService.start(2000);
* var emitter = DriveScannerService.start(2000);
*
* emitter.on('scan', function(drives) {
* console.log(drives);
* });
*/
this.start = function(ms) {
var emitter = new EventEmitter();
DriveScannerRefreshService.every(function() {
return self.scan().then(self.setDrives);
return self.scan().then(function(drives) {
emitter.emit('scan', drives);
self.setDrives(drives);
});
}, ms);
return emitter;
};
/**

View File

@ -20,6 +20,7 @@
var angular = require('angular');
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var remote = window.require('remote');
if (window.mocha) {
@ -150,15 +151,32 @@ driveScanner.service('DriveScannerService', function($q, DriveScannerRefreshServ
* @function
* @public
*
* @description
* This function returns an event emitter instance
* that emits a `scan` event everything it scans
* the drives successfully.
*
* @param {Number} ms - interval milliseconds
* @returns {EventEmitter} event emitter instance
*
* @example
* DriveScannerService.start(2000);
* var emitter = DriveScannerService.start(2000);
*
* emitter.on('scan', function(drives) {
* console.log(drives);
* });
*/
this.start = function(ms) {
var emitter = new EventEmitter();
DriveScannerRefreshService.every(function() {
return self.scan().then(self.setDrives);
return self.scan().then(function(drives) {
emitter.emit('scan', drives);
self.setDrives(drives);
});
}, ms);
return emitter;
};
/**

View File

@ -181,6 +181,21 @@ describe('Browser: DriveScanner', function() {
DriveScannerService.stop();
});
describe('.start()', function() {
it('should emit a `scan` event with the drives', function() {
var emitter = DriveScannerService.start(2000);
var scanSpy = m.sinon.spy();
emitter.on('scan', scanSpy);
$timeout.flush();
$interval.flush(1000);
m.chai.expect(scanSpy).to.have.been.calledOnce;
m.chai.expect(scanSpy).to.have.been.calledWith(this.drives);
DriveScannerService.stop();
});
});
});
});