diff --git a/lib/gui/app/app.js b/lib/gui/app/app.js index 0db0e3a2..e2eb71ce 100644 --- a/lib/gui/app/app.js +++ b/lib/gui/app/app.js @@ -36,6 +36,7 @@ const { // eslint-disable-next-line node/no-missing-require } = require('./models/store') const packageJSON = require('../../../package.json') +// eslint-disable-next-line node/no-missing-require const flashState = require('./models/flash-state') // eslint-disable-next-line node/no-missing-require const settings = require('./models/settings') diff --git a/lib/gui/app/models/flash-state.js b/lib/gui/app/models/flash-state.js deleted file mode 100644 index 574cdfd7..00000000 --- a/lib/gui/app/models/flash-state.js +++ /dev/null @@ -1,246 +0,0 @@ -/* - * Copyright 2016 balena.io - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -'use strict' - -const _ = require('lodash') -// eslint-disable-next-line node/no-missing-require -const { Actions, store } = require('./store') -// eslint-disable-next-line node/no-missing-require -const units = require('../../../shared/units') - -/** - * @summary Reset flash state - * @function - * @public - * - * @example - * flashState.resetState(); - */ -exports.resetState = () => { - store.dispatch({ - type: Actions.RESET_FLASH_STATE - }) -} - -/** - * @summary Check if currently flashing - * @function - * @private - * - * @returns {Boolean} whether is flashing or not - * - * @example - * if (flashState.isFlashing()) { - * console.log('We\'re currently flashing'); - * } - */ -exports.isFlashing = () => { - return store.getState().toJS().isFlashing -} - -/** - * @summary Set the flashing flag - * @function - * @private - * - * @description - * This function is extracted for testing purposes. - * - * The flag is used to signify that we're going to - * start a flash process. - * - * @example - * flashState.setFlashingFlag(); - */ -exports.setFlashingFlag = () => { - store.dispatch({ - type: Actions.SET_FLASHING_FLAG - }) -} - -/** - * @summary Unset the flashing flag - * @function - * @private - * - * @description - * This function is extracted for testing purposes. - * - * The flag is used to signify that the write process ended. - * - * @param {Object} results - flash results - * - * @example - * flashState.unsetFlashingFlag({ - * cancelled: false, - * sourceChecksum: 'a1b45d' - * }); - */ -exports.unsetFlashingFlag = (results) => { - store.dispatch({ - type: Actions.UNSET_FLASHING_FLAG, - data: results - }) -} - -/** - * @summary Set the flashing state - * @function - * @private - * - * @description - * This function is extracted for testing purposes. - * - * @param {Object} state - flashing state - * - * @example - * flashState.setProgressState({ - * type: 'write', - * percentage: 50, - * eta: 15, - * speed: 100000000000 - * }); - */ -exports.setProgressState = (state) => { - // Preserve only one decimal place - const PRECISION = 1 - const data = _.assign({}, state, { - percentage: _.isFinite(state.percentage) - ? Math.floor(state.percentage) - // eslint-disable-next-line no-undefined - : undefined, - - speed: _.attempt(() => { - if (_.isFinite(state.speed)) { - return _.round(units.bytesToMegabytes(state.speed), PRECISION) - } - - return null - }), - - totalSpeed: _.attempt(() => { - if (_.isFinite(state.totalSpeed)) { - return _.round(units.bytesToMegabytes(state.totalSpeed), PRECISION) - } - - return null - }) - }) - - store.dispatch({ - type: Actions.SET_FLASH_STATE, - data - }) -} - -/** - * @summary Get the flash results - * @function - * @private - * - * @returns {Object} flash results - * - * @example - * const results = flashState.getFlashResults(); - */ -exports.getFlashResults = () => { - return store.getState().toJS().flashResults -} - -/** - * @summary Get the current flash state - * @function - * @public - * - * @returns {Object} flash state - * - * @example - * const flashState = flashState.getFlashState(); - */ -exports.getFlashState = () => { - return store.getState().get('flashState').toJS() -} - -/** - * @summary Determine if the last flash was cancelled - * @function - * @public - * - * @description - * This function returns false if there was no last flash. - * - * @returns {Boolean} whether the last flash was cancelled - * - * @example - * if (flashState.wasLastFlashCancelled()) { - * console.log('The last flash was cancelled'); - * } - */ -exports.wasLastFlashCancelled = () => { - return _.get(exports.getFlashResults(), [ 'cancelled' ], false) -} - -/** - * @summary Get last flash source checksum - * @function - * @public - * - * @description - * This function returns undefined if there was no last flash. - * - * @returns {(String|Undefined)} the last flash source checksum - * - * @example - * const checksum = flashState.getLastFlashSourceChecksum(); - */ -exports.getLastFlashSourceChecksum = () => { - return exports.getFlashResults().sourceChecksum -} - -/** - * @summary Get last flash error code - * @function - * @public - * - * @description - * This function returns undefined if there was no last flash. - * - * @returns {(String|Undefined)} the last flash error code - * - * @example - * const errorCode = flashState.getLastFlashErrorCode(); - */ -exports.getLastFlashErrorCode = () => { - return exports.getFlashResults().errorCode -} - -/** - * @summary Get current (or last) flash uuid - * @function - * @public - * - * @description - * This function returns undefined if no flash has been started yet. - * - * @returns {String} the last flash uuid - * - * @example - * const uuid = flashState.getFlashUuid(); - */ -exports.getFlashUuid = () => { - return store.getState().toJS().flashUuid -} diff --git a/lib/gui/app/models/flash-state.ts b/lib/gui/app/models/flash-state.ts new file mode 100644 index 00000000..928b9969 --- /dev/null +++ b/lib/gui/app/models/flash-state.ts @@ -0,0 +1,130 @@ +/* + * Copyright 2016 balena.io + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as sdk from 'etcher-sdk'; +import * as _ from 'lodash'; + +import { bytesToMegabytes } from '../../../shared/units'; +import { Actions, store } from './store'; + +/** + * @summary Reset flash state + */ +export function resetState() { + store.dispatch({ + type: Actions.RESET_FLASH_STATE, + }); +} + +/** + * @summary Check if currently flashing + */ +export function isFlashing(): boolean { + return store.getState().toJS().isFlashing; +} + +/** + * @summary Set the flashing flag + * + * @description + * The flag is used to signify that we're going to + * start a flash process. + */ +export function setFlashingFlag() { + store.dispatch({ + type: Actions.SET_FLASHING_FLAG, + }); +} + +/** + * @summary Unset the flashing flag + * + * @description + * The flag is used to signify that the write process ended. + */ +export function unsetFlashingFlag(results: { + cancelled: boolean; + sourceChecksum?: number; +}) { + store.dispatch({ + type: Actions.UNSET_FLASHING_FLAG, + data: results, + }); +} + +/** + * @summary Set the flashing state + */ +export function setProgressState( + state: sdk.multiWrite.MultiDestinationProgress, +) { + // Preserve only one decimal place + const PRECISION = 1; + const data = _.assign({}, state, { + percentage: + state.percentage !== undefined && _.isFinite(state.percentage) + ? Math.floor(state.percentage) + : undefined, + + speed: _.attempt(() => { + if (_.isFinite(state.speed)) { + return _.round(bytesToMegabytes(state.speed), PRECISION); + } + + return null; + }), + + totalSpeed: _.attempt(() => { + if (_.isFinite(state.totalSpeed)) { + return _.round(bytesToMegabytes(state.totalSpeed), PRECISION); + } + + return null; + }), + }); + + store.dispatch({ + type: Actions.SET_FLASH_STATE, + data, + }); +} + +export function getFlashResults() { + return store.getState().toJS().flashResults; +} + +export function getFlashState() { + return store + .getState() + .get('flashState') + .toJS(); +} + +export function wasLastFlashCancelled() { + return _.get(exports.getFlashResults(), ['cancelled'], false); +} + +export function getLastFlashSourceChecksum(): string { + return exports.getFlashResults().sourceChecksum; +} + +export function getLastFlashErrorCode() { + return exports.getFlashResults().errorCode; +} + +export function getFlashUuid() { + return store.getState().toJS().flashUuid; +} diff --git a/lib/gui/app/modules/image-writer.js b/lib/gui/app/modules/image-writer.js index 9ed341b2..9023e473 100644 --- a/lib/gui/app/modules/image-writer.js +++ b/lib/gui/app/modules/image-writer.js @@ -26,6 +26,7 @@ const electron = require('electron') const { store } = require('../models/store') // eslint-disable-next-line node/no-missing-require const settings = require('../models/settings') +// eslint-disable-next-line node/no-missing-require const flashState = require('../models/flash-state') // eslint-disable-next-line node/no-missing-require const errors = require('../../../shared/errors') diff --git a/tests/gui/models/flash-state.spec.js b/tests/gui/models/flash-state.spec.js index e9814a71..3d791cee 100644 --- a/tests/gui/models/flash-state.spec.js +++ b/tests/gui/models/flash-state.spec.js @@ -17,6 +17,7 @@ 'use strict' const m = require('mochainon') +// eslint-disable-next-line node/no-missing-require const flashState = require('../../../lib/gui/app/models/flash-state') describe('Model: flashState', function () { diff --git a/tests/gui/modules/image-writer.spec.js b/tests/gui/modules/image-writer.spec.js index 7235cb18..457f9701 100644 --- a/tests/gui/modules/image-writer.spec.js +++ b/tests/gui/modules/image-writer.spec.js @@ -4,6 +4,7 @@ const _ = require('lodash') const m = require('mochainon') const ipc = require('node-ipc') const Bluebird = require('bluebird') +// eslint-disable-next-line node/no-missing-require const flashState = require('../../../lib/gui/app/models/flash-state') const imageWriter = require('../../../lib/gui/app/modules/image-writer')