mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-27 13:16:36 +00:00
Merge pull request #135 from resin-io/refactor/drive-scanner-every
Inline DriveScannerRefreshService in DriveScannerService
This commit is contained in:
commit
bf63f1ac26
@ -37,50 +37,9 @@ if (window.mocha) {
|
|||||||
|
|
||||||
const driveScanner = angular.module('ResinEtcher.drive-scanner', []);
|
const driveScanner = angular.module('ResinEtcher.drive-scanner', []);
|
||||||
|
|
||||||
driveScanner.service('DriveScannerRefreshService', function($interval, $timeout) {
|
driveScanner.service('DriveScannerService', function($q, $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) {
|
|
||||||
let self = this;
|
let self = this;
|
||||||
|
let interval = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @summary List of available drives
|
* @summary List of available drives
|
||||||
@ -167,12 +126,20 @@ driveScanner.service('DriveScannerService', function($q, DriveScannerRefreshServ
|
|||||||
this.start = function(ms) {
|
this.start = function(ms) {
|
||||||
let emitter = new EventEmitter();
|
let emitter = new EventEmitter();
|
||||||
|
|
||||||
DriveScannerRefreshService.every(function() {
|
const fn = function() {
|
||||||
return self.scan().then(function(drives) {
|
return self.scan().then(function(drives) {
|
||||||
emitter.emit('scan', drives);
|
emitter.emit('scan', drives);
|
||||||
self.setDrives(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;
|
return emitter;
|
||||||
};
|
};
|
||||||
@ -185,6 +152,8 @@ driveScanner.service('DriveScannerService', function($q, DriveScannerRefreshServ
|
|||||||
* @example
|
* @example
|
||||||
* DriveScannerService.stop();
|
* DriveScannerService.stop();
|
||||||
*/
|
*/
|
||||||
this.stop = DriveScannerRefreshService.stop;
|
this.stop = function() {
|
||||||
|
$interval.cancel(interval);
|
||||||
|
};
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -9,44 +9,6 @@ describe('Browser: DriveScanner', function() {
|
|||||||
|
|
||||||
beforeEach(angular.mock.module('ResinEtcher.drive-scanner'));
|
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() {
|
describe('DriveScannerService', function() {
|
||||||
|
|
||||||
let $interval;
|
let $interval;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user