From 23903ba7087006316d39f113503914716a579cad Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Thu, 27 Jun 2019 12:21:47 +0400 Subject: [PATCH] add memoization to few validators --- src/components/forms/validator.js | 20 +++++++++++++++---- .../screens/AddCustomToken/validators.js | 13 +----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/components/forms/validator.js b/src/components/forms/validator.js index d885bc59..de0f30ff 100644 --- a/src/components/forms/validator.js +++ b/src/components/forms/validator.js @@ -2,9 +2,21 @@ import { type FieldValidator } from 'final-form' import { getWeb3 } from '~/logic/wallets/getWeb3' +export const simpleMemoize = (fn: Function) => { + let lastArg + let lastResult + return (arg: any) => { + if (arg !== lastArg) { + lastArg = arg + lastResult = fn(arg) + } + return lastResult + } +} + type Field = boolean | string | null | typeof undefined -export const required = (value: Field) => (value ? undefined : 'Required') +export const required = simpleMemoize((value: Field) => (value ? undefined : 'Required')) export const mustBeInteger = (value: string) => (!Number.isInteger(Number(value)) || value.includes('.') ? 'Must be an integer' : undefined) @@ -46,17 +58,17 @@ export const maxValue = (max: number) => (value: string) => { export const ok = () => undefined -export const mustBeEthereumAddress = (address: Field) => { +export const mustBeEthereumAddress = simpleMemoize((address: Field) => { const isAddress: boolean = getWeb3().utils.isAddress(address) return isAddress ? undefined : 'Address should be a valid Ethereum address' -} +}) export const minMaxLength = (minLen: string | number, maxLen: string | number) => (value: string) => (value.length >= +minLen && value.length <= +maxLen ? undefined : `Should be ${minLen} to ${maxLen} symbols`) export const ADDRESS_REPEATED_ERROR = 'Address already introduced' -export const uniqueAddress = (addresses: string[]) => (value: string) => (addresses.includes(value) ? ADDRESS_REPEATED_ERROR : undefined) +export const uniqueAddress = (addresses: string[]) => simpleMemoize((value: string) => (addresses.includes(value) ? ADDRESS_REPEATED_ERROR : undefined)) export const composeValidators = (...validators: Function[]): FieldValidator => (value: Field) => validators.reduce((error, validator) => error || validator(value), undefined) diff --git a/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/validators.js b/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/validators.js index 07feaadb..d9db666d 100644 --- a/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/validators.js +++ b/src/routes/safe/components/Balances/Tokens/screens/AddCustomToken/validators.js @@ -3,20 +3,9 @@ import { List } from 'immutable' import { getWeb3 } from '~/logic/wallets/getWeb3' import { type Token } from '~/logic/tokens/store/model/token' import { sameAddress } from '~/logic/wallets/ethAddresses' +import { simpleMemoize } from '~/components/forms/validator' // import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens' -export const simpleMemoize = (fn: Function) => { - let lastArg - let lastResult - return (arg: any) => { - if (arg !== lastArg) { - lastArg = arg - lastResult = fn(arg) - } - return lastResult - } -} - // eslint-disable-next-line export const addressIsTokenContract = simpleMemoize(async (tokenAddress: string) => { // SECOND APPROACH: