diff --git a/lib/gui/app.js b/lib/gui/app.js index e8871e6a..3fb65219 100644 --- a/lib/gui/app.js +++ b/lib/gui/app.js @@ -97,7 +97,7 @@ app.run((AnalyticsService, UpdateNotifierService, SelectionStateModel) => { app.run((AnalyticsService, OSWindowProgressService, ImageWriterService) => { Store.subscribe(() => { - const state = Store.getState().toJS(); + const flashState = ImageWriterService.getFlashState(); // There is usually a short time period between the `isFlashing()` // property being set, and the flashing actually starting, which @@ -106,17 +106,17 @@ app.run((AnalyticsService, OSWindowProgressService, ImageWriterService) => { // // We use the presence of `.eta` to determine that the actual // writing started. - if (!ImageWriterService.isFlashing() || !state.flashState.eta) { + if (!ImageWriterService.isFlashing() || !flashState.eta) { return; } AnalyticsService.log([ - `Progress (${state.flashState.type}):`, - `${state.flashState.percentage}% at ${state.flashState.speed} MB/s`, - `(eta ${state.flashState.eta}s)` + `Progress (${flashState.type}):`, + `${flashState.percentage}% at ${flashState.speed} MB/s`, + `(eta ${flashState.eta}s)` ].join(' ')); - OSWindowProgressService.set(state.flashState.percentage); + OSWindowProgressService.set(flashState.percentage); }); }); @@ -133,7 +133,6 @@ app.config(($stateProvider, $urlRouterProvider) => { app.controller('AppController', function( $state, - $scope, DriveScannerService, SelectionStateModel, SettingsModel, diff --git a/lib/gui/modules/image-writer.js b/lib/gui/modules/image-writer.js index c356a492..0f029a44 100644 --- a/lib/gui/modules/image-writer.js +++ b/lib/gui/modules/image-writer.js @@ -30,7 +30,7 @@ const imageWriter = angular.module(MODULE_NAME, [ require('../models/settings') ]); -imageWriter.service('ImageWriterService', function($q, $timeout, SettingsModel) { +imageWriter.service('ImageWriterService', function($q, $rootScope, SettingsModel) { /** * @summary Reset flash state @@ -46,25 +46,6 @@ imageWriter.service('ImageWriterService', function($q, $timeout, SettingsModel) }); }; - /** - * @summary Flash progress state - * @type Object - * @public - */ - this.state = { - percentage: 0, - speed: 0 - }; - - Store.subscribe(() => { - - // Safely bring the state to the world of Angular - $timeout(() => { - this.state = Store.getState().toJS().flashState; - }); - - }); - /** * @summary Check if currently flashing * @function @@ -179,6 +160,20 @@ imageWriter.service('ImageWriterService', function($q, $timeout, SettingsModel) return Store.getState().toJS().flashResults; }; + /** + * @summary Get the current flash state + * @function + * @public + * + * @returns {Object} flash state + * + * @example + * const flashState = ImageWriterService.getFlashState(); + */ + this.getFlashState = () => { + return Store.getState().get('flashState').toJS(); + }; + /** * @summary Perform write operation * @function @@ -239,7 +234,17 @@ imageWriter.service('ImageWriterService', function($q, $timeout, SettingsModel) this.setFlashingFlag(); - return this.performWrite(image, drive, this.setProgressState).then((results) => { + return this.performWrite(image, drive, (state) => { + + // Bring this value to the world of angular. + // If we don't trigger a digest loop, + // `.getFlashState()` will not return + // the latest updated progress state. + $rootScope.$apply(() => { + this.setProgressState(state); + }); + + }).then((results) => { // TODO: Make sure `etcher-image-write` always // sends a `cancelled` and `passedValidation` property. diff --git a/lib/gui/partials/main.html b/lib/gui/partials/main.html index 9165e25a..0e143eb0 100644 --- a/lib/gui/partials/main.html +++ b/lib/gui/partials/main.html @@ -84,19 +84,19 @@
- Finishing... - Flash! - Starting... - - + Finishing... + Flash! + Starting... + +
@@ -116,9 +116,9 @@ Retry -
diff --git a/tests/gui/modules/image-writer.spec.js b/tests/gui/modules/image-writer.spec.js index 3430b539..16540dd9 100644 --- a/tests/gui/modules/image-writer.spec.js +++ b/tests/gui/modules/image-writer.spec.js @@ -13,40 +13,29 @@ describe('Browser: ImageWriter', function() { describe('ImageWriterService', function() { let $q; - let $timeout; let $rootScope; let ImageWriterService; - beforeEach(angular.mock.inject(function(_$q_, _$timeout_, _$rootScope_, _ImageWriterService_) { + beforeEach(angular.mock.inject(function(_$q_, _$rootScope_, _ImageWriterService_) { $q = _$q_; - $timeout = _$timeout_; $rootScope = _$rootScope_; ImageWriterService = _ImageWriterService_; })); - describe('.state', function() { - - it('should be reset by default', function() { - m.chai.expect(ImageWriterService.state).to.deep.equal({ - percentage: 0, - speed: 0 - }); - }); - - }); - describe('.resetState()', function() { it('should be able to reset the progress state', function() { - ImageWriterService.state = { + ImageWriterService.setFlashingFlag(); + ImageWriterService.setProgressState({ + type: 'write', percentage: 50, - speed: 3 - }; + eta: 15, + speed: 100000000000 + }); ImageWriterService.resetState(); - $timeout.flush(); - m.chai.expect(ImageWriterService.state).to.deep.equal({ + m.chai.expect(ImageWriterService.getFlashState()).to.deep.equal({ percentage: 0, speed: 0 }); @@ -233,6 +222,33 @@ describe('Browser: ImageWriter', function() { }); + describe('.getFlashState()', function() { + + it('should initially return an empty state', function() { + ImageWriterService.resetState(); + const flashState = ImageWriterService.getFlashState(); + m.chai.expect(flashState).to.deep.equal({ + percentage: 0, + speed: 0 + }); + }); + + it('should return the current flash state', function() { + const state = { + type: 'write', + percentage: 50, + eta: 15, + speed: 0 + }; + + ImageWriterService.setFlashingFlag(); + ImageWriterService.setProgressState(state); + const flashState = ImageWriterService.getFlashState(); + m.chai.expect(flashState).to.deep.equal(state); + }); + + }); + describe('.unsetFlashingFlag()', function() { it('should throw if no flashing results', function() { @@ -348,9 +364,7 @@ describe('Browser: ImageWriter', function() { speed: 100000000000 }); - $timeout.flush(); - - m.chai.expect(ImageWriterService.state).to.not.deep.equal({ + m.chai.expect(ImageWriterService.getFlashState()).to.not.deep.equal({ percentage: 0, speed: 0 }); @@ -361,9 +375,7 @@ describe('Browser: ImageWriter', function() { sourceChecksum: '1234' }); - $timeout.flush(); - - m.chai.expect(ImageWriterService.state).to.deep.equal({ + m.chai.expect(ImageWriterService.getFlashState()).to.deep.equal({ percentage: 0, speed: 0 });