From 84d87e5a10b7f22efefd88155399a60c7c860c5e Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Fri, 22 Jan 2016 12:30:42 -0400 Subject: [PATCH] Move burn state log to app controller This change required re-architecting the way we store the burn state. --- lib/browser/app.js | 12 ++- lib/browser/modules/image-writer.js | 88 +++++----------------- lib/index.html | 18 ++--- tests/browser/modules/image-writer.spec.js | 71 ----------------- 4 files changed, 39 insertions(+), 150 deletions(-) diff --git a/lib/browser/app.js b/lib/browser/app.js index bd66d95f..7ea28088 100644 --- a/lib/browser/app.js +++ b/lib/browser/app.js @@ -76,7 +76,12 @@ app.controller('AppController', function($q, $log, DriveScannerService, Selectio this.restart = function(options) { $log.debug('Restarting'); this.selection.clear(options); - this.writer.reset(); + + self.state = { + progress: 0, + percentage: 0 + }; + this.scanner.start(2000).on('scan', function(drives) { // Notice we only autoselect the drive if there is an image, @@ -154,7 +159,10 @@ app.controller('AppController', function($q, $log, DriveScannerService, Selectio self.scanner.stop(); $log.debug(`Burning ${image} to ${drive.device}`); - return self.writer.burn(image, drive).then(function() { + return self.writer.burn(image, drive, function(state) { + self.state = state; + $log.debug(`Progress: ${self.state.progress}% at ${self.state.speed} MB/s`); + }).then(function() { $log.debug('Done!'); }).catch(dialog.showError); }; diff --git a/lib/browser/modules/image-writer.js b/lib/browser/modules/image-writer.js index 9b6f9363..21ae82e7 100644 --- a/lib/browser/modules/image-writer.js +++ b/lib/browser/modules/image-writer.js @@ -31,75 +31,10 @@ if (window.mocha) { const imageWriter = angular.module('ResinEtcher.image-writer', []); -imageWriter.service('ImageWriterService', function($q, $timeout, $log) { +imageWriter.service('ImageWriterService', function($q, $timeout) { let self = this; let burning = false; - /* - * @summary Progress state - * @type Object - * @public - */ - this.state = { - - /** - * @summary Progress percentage - * @type Number - * @public - */ - progress: 0, - - /** - * @summary Progress speed - * @type Number - * @public - */ - speed: 0 - - }; - - /** - * @summary Set progress state - * @function - * @private - * - * @param {Object} state - progress state - * @param {Number} state.percentage - progress percentage - * - * @example - * ImageWriterService.setProgressState({ - * percentage: 50 - * }); - */ - this.setProgressState = function(state) { - - // Safely bring the state to the world of Angular - $timeout(function() { - self.state.progress = Math.floor(state.percentage); - - // Transform bytes to megabytes preserving only two decimal places - self.state.speed = Math.floor(state.speed / 1e+6 * 100) / 100 || 0; - - $log.debug(`Progress: ${self.state.progress}% at ${self.state.speed} MB/s`); - }); - - }; - - /** - * @summary Reset progress state - * @function - * @public - * - * @example - * ImageWriterService.reset(); - */ - this.reset = function() { - self.setProgressState({ - percentage: 0, - speed: 0 - }); - }; - /** * @summary Check if currently burning * @function @@ -121,6 +56,9 @@ imageWriter.service('ImageWriterService', function($q, $timeout, $log) { * @function * @private * + * @description + * This function is extracted for testing purposes. + * * @param {Boolean} status - burning status * * @example @@ -175,7 +113,7 @@ imageWriter.service('ImageWriterService', function($q, $timeout, $log) { * console.log('Write completed!'); * }); */ - this.burn = function(image, drive) { + this.burn = function(image, drive, onProgress) { // Avoid writing more than once if (self.isBurning()) { @@ -184,7 +122,21 @@ imageWriter.service('ImageWriterService', function($q, $timeout, $log) { self.setBurning(true); - return self.performWrite(image, drive, self.setProgressState).finally(function() { + return self.performWrite(image, drive, function(state) { + + // Safely bring the state to the world of Angular + $timeout(function() { + + return onProgress({ + progress: Math.floor(state.percentage), + + // Transform bytes to megabytes preserving only two decimal places + speed: Math.floor(state.speed / 1e+6 * 100) / 100 || 0 + + }); + }); + + }).finally(function() { self.setBurning(false); }); }; diff --git a/lib/index.html b/lib/index.html index a2261f40..151b2b50 100644 --- a/lib/index.html +++ b/lib/index.html @@ -20,7 +20,7 @@
-
+
@@ -78,23 +78,23 @@ 3
- - Finishing... - Burn! - Starting... - + Finishing... + Burn! + Starting... + - +
-
+

Burn Complete!

diff --git a/tests/browser/modules/image-writer.spec.js b/tests/browser/modules/image-writer.spec.js index 04676da2..d75f1773 100644 --- a/tests/browser/modules/image-writer.spec.js +++ b/tests/browser/modules/image-writer.spec.js @@ -23,14 +23,6 @@ describe('Browser: ImageWriter', function() { ImageWriterService = _ImageWriterService_; })); - it('should set progress to zero by default', function() { - m.chai.expect(ImageWriterService.state.progress).to.equal(0); - }); - - it('should set speed to zero by default', function() { - m.chai.expect(ImageWriterService.state.speed).to.equal(0); - }); - describe('.isBurning()', function() { it('should return false by default', function() { @@ -66,69 +58,6 @@ describe('Browser: ImageWriter', function() { }); - describe('.setProgressState()', function() { - - it('should be able to set the progress', function() { - ImageWriterService.setProgressState({ - percentage: 50, - speed: 949624 - }); - - $timeout.flush(); - m.chai.expect(ImageWriterService.state.progress).to.equal(50); - m.chai.expect(ImageWriterService.state.speed).to.equal(0.94); - }); - - it('should floor the percentage', function() { - ImageWriterService.setProgressState({ - percentage: 49.9999 - }); - - $timeout.flush(); - m.chai.expect(ImageWriterService.state.progress).to.equal(49); - }); - - it('should assume speed zero if no speed', function() { - ImageWriterService.setProgressState({ - percentage: 25 - }); - - $timeout.flush(); - m.chai.expect(ImageWriterService.state.speed).to.equal(0); - }); - - }); - - describe('.reset()', function() { - - it('should reset progress percentage to 0', function() { - ImageWriterService.setProgressState({ - percentage: 50, - speed: 949624 - }); - - $timeout.flush(); - m.chai.expect(ImageWriterService.state.progress).to.equal(50); - ImageWriterService.reset(); - $timeout.flush(); - m.chai.expect(ImageWriterService.state.progress).to.equal(0); - }); - - it('should reset speed to 0', function() { - ImageWriterService.setProgressState({ - percentage: 50, - speed: 949624 - }); - - $timeout.flush(); - m.chai.expect(ImageWriterService.state.speed).to.equal(0.94); - ImageWriterService.reset(); - $timeout.flush(); - m.chai.expect(ImageWriterService.state.speed).to.equal(0); - }); - - }); - describe('.burn()', function() { describe('given a succesful write', function() {