import { Options } from 'react-redux' import deepEqual from 'fast-deep-equal/es6' import { IMAGE, LABEL } from '@adalo/constants' import { timingsWrapper } from 'utils/timings' type ObjectType = typeof LABEL | typeof IMAGE // add more object types here, as we add more critical prop checks const REDUX_STATE_KEYS = { auth: 'auth', // TODO(michael-adalo): further optimisation here would be to check the binding props for the object type // and only re-render if it references changed collections of data data: 'data', // TODO(michael-adalo): further optimisation here would be to check the binding props for the object type // and only re-render if it references a field in formInputs formInputs: 'formInputs', location: 'location', random: 'random', images: 'images', notification: 'notification', pushTreeMap: 'pushTreeMap', toasts: 'toasts', } const criticalOwnPropsNonFunction = new Map([ [LABEL, ['object', 'listItemId']], [IMAGE, ['object', 'listItemId', 'nodeHeight', 'newY']], ]) const criticalStateProps = new Map([ [LABEL, [REDUX_STATE_KEYS.auth, REDUX_STATE_KEYS.data, REDUX_STATE_KEYS.formInputs, REDUX_STATE_KEYS.location, REDUX_STATE_KEYS.random]], // prettier-ignore [IMAGE, [REDUX_STATE_KEYS.auth, REDUX_STATE_KEYS.data, REDUX_STATE_KEYS.formInputs, REDUX_STATE_KEYS.location, REDUX_STATE_KEYS.random]], // prettier-ignore ]) export const getOptions = (objectType: ObjectType): Options => { const criticalStatePropsForType = criticalStateProps.get(objectType) const criticalOwnPropsNonFunctionForType = criticalOwnPropsNonFunction.get(objectType) // prettier-ignore if (!criticalStatePropsForType || !criticalOwnPropsNonFunctionForType) { return {} } const areStatesEqual = timingsWrapper((next, prev) => { for (const key of criticalStatePropsForType) { if (!deepEqual(next[key], prev[key])) { return false } } return true }, `${objectType}: areStatesEqual`) const areOwnPropsEqual = timingsWrapper((next, prev) => { for (const key of criticalOwnPropsNonFunctionForType) { if (!deepEqual(next[key], prev[key])) { return false } } return true }, `${objectType}: areOwnPropsEqual`) return { areStatesEqual, areOwnPropsEqual, } }