mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-23 11:16:39 +00:00
Convert flash-state.js to typescript
Change-type: patch
This commit is contained in:
parent
d0d4ee843d
commit
1c46ee2988
@ -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')
|
||||
|
@ -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
|
||||
}
|
130
lib/gui/app/models/flash-state.ts
Normal file
130
lib/gui/app/models/flash-state.ts
Normal file
@ -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;
|
||||
}
|
@ -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')
|
||||
|
@ -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 () {
|
||||
|
@ -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')
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user