From e2f5775b07c0c3afe8c17119f81c7d556e7b103e Mon Sep 17 00:00:00 2001 From: Alexis Svinartchouk Date: Fri, 3 Jan 2020 01:18:52 +0100 Subject: [PATCH] Remove no longer needed angular specific utils.memoize Change-type: patch --- .../drive-selector/DriveSelectorModal.jsx | 20 +------ lib/gui/app/pages/main/DriveSelector.tsx | 8 +-- lib/shared/utils.js | 59 ------------------- tests/shared/utils.spec.js | 28 --------- 4 files changed, 4 insertions(+), 111 deletions(-) diff --git a/lib/gui/app/components/drive-selector/DriveSelectorModal.jsx b/lib/gui/app/components/drive-selector/DriveSelectorModal.jsx index ac47c2ac..a63931ea 100644 --- a/lib/gui/app/components/drive-selector/DriveSelectorModal.jsx +++ b/lib/gui/app/components/drive-selector/DriveSelectorModal.jsx @@ -30,7 +30,6 @@ const analytics = require('../../modules/analytics') const availableDrives = require('../../models/available-drives') const selectionState = require('../../models/selection-state') const { bytesToClosestUnit } = require('../../../../shared/units') -const utils = require('../../../../shared/utils') 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} - 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) * @function @@ -113,9 +99,9 @@ const getDrives = utils.memoize(availableDrives.getDrives, _.isEqual) * // do something * } */ -const getDriveStatuses = utils.memoize((drive) => { +const getDriveStatuses = (drive) => { return getDriveImageCompatibilityStatuses(drive, selectionState.getImage()) -}, _.isEqual) +} /** * @summary Keyboard event drive toggling @@ -143,7 +129,7 @@ const keyboardToggleDrive = (drive, evt) => { const DriveSelectorModal = ({ close }) => { const [ confirmModal, setConfirmModal ] = React.useState({ open: false }) - const [ drives, setDrives ] = React.useState(getDrives()) + const [ drives, setDrives ] = React.useState(availableDrives.getDrives()) React.useEffect(() => { const unsubscribe = store.subscribe(() => { diff --git a/lib/gui/app/pages/main/DriveSelector.tsx b/lib/gui/app/pages/main/DriveSelector.tsx index 76cfdfbd..7fc11943 100644 --- a/lib/gui/app/pages/main/DriveSelector.tsx +++ b/lib/gui/app/pages/main/DriveSelector.tsx @@ -19,7 +19,6 @@ import * as propTypes from 'prop-types'; import * as React from 'react'; import styled from 'styled-components'; import * as driveConstraints from '../../../../shared/drive-constraints'; -import * as utils from '../../../../shared/utils'; import * as DriveSelectorModal from '../../components/drive-selector/DriveSelectorModal.jsx'; import * as TargetSelector from '../../components/drive-selector/target-selector.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 = () => { return !settings.get('disableExplicitDriveSelection'); }; @@ -67,7 +61,7 @@ const shouldShowDrivesButton = () => { const getDriveSelectionStateSlice = () => ({ showDrivesButton: shouldShowDrivesButton(), driveListLabel: getDriveListLabel(), - targets: getMemoizedSelectedDrives(), + targets: selectionState.getSelectedDrives(), }); export const DriveSelector = ({ diff --git a/lib/shared/utils.js b/lib/shared/utils.js index c86e6356..2107be80 100755 --- a/lib/shared/utils.js +++ b/lib/shared/utils.js @@ -83,65 +83,6 @@ exports.percentageToFloat = (percentage) => { 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 * @function diff --git a/tests/shared/utils.spec.js b/tests/shared/utils.spec.js index c61094dd..103e6b5d 100644 --- a/tests/shared/utils.spec.js +++ b/tests/shared/utils.spec.js @@ -16,7 +16,6 @@ 'use strict' -const _ = require('lodash') const m = require('mochainon') const utils = require('../../lib/shared/utils') @@ -126,31 +125,4 @@ describe('Shared: Utils', function () { }).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 - }) - }) })