mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-23 11:16:39 +00:00
Remove no longer needed angular specific utils.memoize
Change-type: patch
This commit is contained in:
parent
c27be733a9
commit
e2f5775b07
@ -30,7 +30,6 @@ const analytics = require('../../modules/analytics')
|
|||||||
const availableDrives = require('../../models/available-drives')
|
const availableDrives = require('../../models/available-drives')
|
||||||
const selectionState = require('../../models/selection-state')
|
const selectionState = require('../../models/selection-state')
|
||||||
const { bytesToClosestUnit } = require('../../../../shared/units')
|
const { bytesToClosestUnit } = require('../../../../shared/units')
|
||||||
const utils = require('../../../../shared/utils')
|
|
||||||
const { open: openExternal } = require('../../os/open-external/services/open-external')
|
const { open: openExternal } = require('../../os/open-external/services/open-external')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -81,19 +80,6 @@ const toggleDrive = (drive) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @summary Memoized getDrives function
|
|
||||||
* @function
|
|
||||||
* @public
|
|
||||||
*
|
|
||||||
* @returns {Array<Object>} - memoized list of drives
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* const drives = getDrives()
|
|
||||||
* // Do something with drives
|
|
||||||
*/
|
|
||||||
const getDrives = utils.memoize(availableDrives.getDrives, _.isEqual)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @summary Get a drive's compatibility status object(s)
|
* @summary Get a drive's compatibility status object(s)
|
||||||
* @function
|
* @function
|
||||||
@ -113,9 +99,9 @@ const getDrives = utils.memoize(availableDrives.getDrives, _.isEqual)
|
|||||||
* // do something
|
* // do something
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
const getDriveStatuses = utils.memoize((drive) => {
|
const getDriveStatuses = (drive) => {
|
||||||
return getDriveImageCompatibilityStatuses(drive, selectionState.getImage())
|
return getDriveImageCompatibilityStatuses(drive, selectionState.getImage())
|
||||||
}, _.isEqual)
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @summary Keyboard event drive toggling
|
* @summary Keyboard event drive toggling
|
||||||
@ -143,7 +129,7 @@ const keyboardToggleDrive = (drive, evt) => {
|
|||||||
|
|
||||||
const DriveSelectorModal = ({ close }) => {
|
const DriveSelectorModal = ({ close }) => {
|
||||||
const [ confirmModal, setConfirmModal ] = React.useState({ open: false })
|
const [ confirmModal, setConfirmModal ] = React.useState({ open: false })
|
||||||
const [ drives, setDrives ] = React.useState(getDrives())
|
const [ drives, setDrives ] = React.useState(availableDrives.getDrives())
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
const unsubscribe = store.subscribe(() => {
|
const unsubscribe = store.subscribe(() => {
|
||||||
|
@ -19,7 +19,6 @@ import * as propTypes from 'prop-types';
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
import * as driveConstraints from '../../../../shared/drive-constraints';
|
import * as driveConstraints from '../../../../shared/drive-constraints';
|
||||||
import * as utils from '../../../../shared/utils';
|
|
||||||
import * as DriveSelectorModal from '../../components/drive-selector/DriveSelectorModal.jsx';
|
import * as DriveSelectorModal from '../../components/drive-selector/DriveSelectorModal.jsx';
|
||||||
import * as TargetSelector from '../../components/drive-selector/target-selector.jsx';
|
import * as TargetSelector from '../../components/drive-selector/target-selector.jsx';
|
||||||
import * as SvgIcon from '../../components/svg-icon/svg-icon.jsx';
|
import * as SvgIcon from '../../components/svg-icon/svg-icon.jsx';
|
||||||
@ -55,11 +54,6 @@ const getDriveListLabel = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getMemoizedSelectedDrives = utils.memoize(
|
|
||||||
selectionState.getSelectedDrives,
|
|
||||||
_.isEqual,
|
|
||||||
);
|
|
||||||
|
|
||||||
const shouldShowDrivesButton = () => {
|
const shouldShowDrivesButton = () => {
|
||||||
return !settings.get('disableExplicitDriveSelection');
|
return !settings.get('disableExplicitDriveSelection');
|
||||||
};
|
};
|
||||||
@ -67,7 +61,7 @@ const shouldShowDrivesButton = () => {
|
|||||||
const getDriveSelectionStateSlice = () => ({
|
const getDriveSelectionStateSlice = () => ({
|
||||||
showDrivesButton: shouldShowDrivesButton(),
|
showDrivesButton: shouldShowDrivesButton(),
|
||||||
driveListLabel: getDriveListLabel(),
|
driveListLabel: getDriveListLabel(),
|
||||||
targets: getMemoizedSelectedDrives(),
|
targets: selectionState.getSelectedDrives(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const DriveSelector = ({
|
export const DriveSelector = ({
|
||||||
|
@ -83,65 +83,6 @@ exports.percentageToFloat = (percentage) => {
|
|||||||
return percentage / exports.PERCENTAGE_MAXIMUM
|
return percentage / exports.PERCENTAGE_MAXIMUM
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @summary Memoize a function
|
|
||||||
* @function
|
|
||||||
* @private
|
|
||||||
*
|
|
||||||
* @description
|
|
||||||
* This workaround is needed to avoid AngularJS from getting
|
|
||||||
* caught in an infinite digest loop when using `ngRepeat`
|
|
||||||
* over a function that returns a mutable version of an
|
|
||||||
* ImmutableJS object.
|
|
||||||
*
|
|
||||||
* The problem is that every time you call `myImmutableObject.toJS()`
|
|
||||||
* you will get a new object, whose reference is different from
|
|
||||||
* the one you previously got, even if the data is exactly the same.
|
|
||||||
*
|
|
||||||
* @param {Function} func - function that returns an ImmutableJS list
|
|
||||||
* @param {Function} comparer - function to compare old and new args and state
|
|
||||||
* @returns {Function} memoized function
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* const getList = () => {
|
|
||||||
* return Store.getState().toJS().myList;
|
|
||||||
* };
|
|
||||||
*
|
|
||||||
* const memoizedFunction = memoize(getList, angular.equals);
|
|
||||||
*/
|
|
||||||
exports.memoize = (func, comparer) => {
|
|
||||||
let previousTuples = []
|
|
||||||
|
|
||||||
return (...restArgs) => {
|
|
||||||
let areArgsInTuple = false
|
|
||||||
let state = Reflect.apply(func, this, restArgs)
|
|
||||||
|
|
||||||
previousTuples = _.map(previousTuples, ([ oldArgs, oldState ]) => {
|
|
||||||
if (comparer(oldArgs, restArgs)) {
|
|
||||||
areArgsInTuple = true
|
|
||||||
|
|
||||||
if (comparer(state, oldState)) {
|
|
||||||
// Use the previously memoized state for this argument
|
|
||||||
state = oldState
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update the tuple state
|
|
||||||
return [ oldArgs, state ]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the tuple unchanged
|
|
||||||
return [ oldArgs, oldState ]
|
|
||||||
})
|
|
||||||
|
|
||||||
// Add the state associated with these args to be memoized
|
|
||||||
if (!areArgsInTuple) {
|
|
||||||
previousTuples.push([ restArgs, state ])
|
|
||||||
}
|
|
||||||
|
|
||||||
return state
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @summary Check if obj has one or many specific props
|
* @summary Check if obj has one or many specific props
|
||||||
* @function
|
* @function
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const _ = require('lodash')
|
|
||||||
const m = require('mochainon')
|
const m = require('mochainon')
|
||||||
const utils = require('../../lib/shared/utils')
|
const utils = require('../../lib/shared/utils')
|
||||||
|
|
||||||
@ -126,31 +125,4 @@ describe('Shared: Utils', function () {
|
|||||||
}).to.throw('Invalid percentage: 100.01')
|
}).to.throw('Invalid percentage: 100.01')
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('.memoize()', function () {
|
|
||||||
it('constant true should return memoized true', function () {
|
|
||||||
const memoizedConstTrue = utils.memoize(_.constant(true), _.isEqual)
|
|
||||||
m.chai.expect(memoizedConstTrue()).to.be.true
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should reflect state changes', function () {
|
|
||||||
let stateA = false
|
|
||||||
const memoizedStateA = utils.memoize(() => {
|
|
||||||
return stateA
|
|
||||||
}, _.isEqual)
|
|
||||||
|
|
||||||
m.chai.expect(memoizedStateA()).to.be.false
|
|
||||||
|
|
||||||
stateA = true
|
|
||||||
|
|
||||||
m.chai.expect(memoizedStateA()).to.be.true
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should reflect different arguments', function () {
|
|
||||||
const memoizedParameter = utils.memoize(_.identity, _.isEqual)
|
|
||||||
|
|
||||||
m.chai.expect(memoizedParameter(false)).to.be.false
|
|
||||||
m.chai.expect(memoizedParameter(true)).to.be.true
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user