CompositionInput: Fix high CPU usage

This commit is contained in:
Ken Powers 2020-03-23 17:09:12 -04:00 committed by Scott Nonnenberg
parent a1270867ff
commit 17f212ffcf
3 changed files with 26 additions and 16 deletions

View file

@ -2,20 +2,25 @@
import { Ref } from 'react';
import { isFunction } from 'lodash';
import memoizee from 'memoizee';
export function cleanId(id: string): string {
return id.replace(/[^\u0020-\u007e\u00a0-\u00ff]/g, '_');
}
export function mergeRefs<T>(...refs: Array<Ref<T>>) {
return (t: T) => {
refs.forEach(r => {
if (isFunction(r)) {
r(t);
} else if (r) {
// @ts-ignore: React's typings for ref objects is annoying
r.current = t;
}
});
};
}
export const createRefMerger = () =>
memoizee(
<T>(...refs: Array<Ref<T>>) => {
return (t: T) => {
refs.forEach(r => {
if (isFunction(r)) {
r(t);
} else if (r) {
// @ts-ignore: React's typings for ref objects is annoying
r.current = t;
}
});
};
},
{ length: false, max: 1 }
);