Merge pull request #135 from resin-io/refactor/drive-scanner-every

Inline DriveScannerRefreshService in DriveScannerService
This commit is contained in:
Juan Cruz Viotti 2016-01-26 12:04:05 -04:00
commit bf63f1ac26
2 changed files with 15 additions and 84 deletions

View File

@ -37,50 +37,9 @@ if (window.mocha) {
const driveScanner = angular.module('ResinEtcher.drive-scanner', []);
driveScanner.service('DriveScannerRefreshService', function($interval, $timeout) {
let interval = null;
/**
* @summary Run a function every certain milliseconds
* @function
* @public
*
* @param {Function} fn - function
* @param {Number} ms - interval milliseconds
*
* @example
* DriveScannerRefreshService.every(function() {
* console.log('I get printed every 2 seconds!');
* }, 2000);
*/
this.every = function(fn, ms) {
// Call fn after in the next process tick
// to be able to capture the first run
// in unit tests.
$timeout(function() {
fn();
interval = $interval(fn, ms);
});
};
/**
* @summary Stop the runnning interval
* @function
* @public
*
* @example
* DriveScannerRefreshService.stop();
*/
this.stop = function() {
$interval.cancel(interval);
};
});
driveScanner.service('DriveScannerService', function($q, DriveScannerRefreshService) {
driveScanner.service('DriveScannerService', function($q, $interval, $timeout) {
let self = this;
let interval = null;
/**
* @summary List of available drives
@ -167,12 +126,20 @@ driveScanner.service('DriveScannerService', function($q, DriveScannerRefreshServ
this.start = function(ms) {
let emitter = new EventEmitter();
DriveScannerRefreshService.every(function() {
const fn = function() {
return self.scan().then(function(drives) {
emitter.emit('scan', drives);
self.setDrives(drives);
});
}, ms);
};
// Call fn after in the next process tick
// to be able to capture the first run
// in unit tests.
$timeout(function() {
fn();
interval = $interval(fn, ms);
});
return emitter;
};
@ -185,6 +152,8 @@ driveScanner.service('DriveScannerService', function($q, DriveScannerRefreshServ
* @example
* DriveScannerService.stop();
*/
this.stop = DriveScannerRefreshService.stop;
this.stop = function() {
$interval.cancel(interval);
};
});

View File

@ -9,44 +9,6 @@ describe('Browser: DriveScanner', function() {
beforeEach(angular.mock.module('ResinEtcher.drive-scanner'));
describe('DriveScannerRefreshService', function() {
let DriveScannerRefreshService;
let $interval;
let $timeout;
beforeEach(angular.mock.inject(function(_$interval_, _$timeout_, _DriveScannerRefreshService_) {
$interval = _$interval_;
$timeout = _$timeout_;
DriveScannerRefreshService = _DriveScannerRefreshService_;
}));
describe('.every()', function() {
it('should call the function right away', function() {
const spy = m.sinon.spy();
DriveScannerRefreshService.every(spy, 1000);
$timeout.flush();
DriveScannerRefreshService.stop();
m.chai.expect(spy).to.have.been.calledOnce;
});
it('should call the function in an interval', function() {
const spy = m.sinon.spy();
DriveScannerRefreshService.every(spy, 100);
$timeout.flush();
// 400ms = 100ms / 4 + 1 (the initial call)
$interval.flush(400);
DriveScannerRefreshService.stop();
m.chai.expect(spy).to.have.callCount(5);
});
});
});
describe('DriveScannerService', function() {
let $interval;