diff --git a/lib/browser/modules/drive-scanner.js b/lib/browser/modules/drive-scanner.js index b2d11337..437bdf07 100644 --- a/lib/browser/modules/drive-scanner.js +++ b/lib/browser/modules/drive-scanner.js @@ -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); + }; }); diff --git a/tests/browser/modules/drive-scanner.spec.js b/tests/browser/modules/drive-scanner.spec.js index 12ab6ffc..0a204155 100644 --- a/tests/browser/modules/drive-scanner.spec.js +++ b/tests/browser/modules/drive-scanner.spec.js @@ -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;