Skip to content

Commit

Permalink
CompositionInput: Fix high CPU usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kenpowers-signal authored and scottnonnenberg-signal committed Mar 25, 2020
1 parent a127086 commit 17f212f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
5 changes: 3 additions & 2 deletions ts/components/CompositionInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
search,
} from './emoji/lib';
import { LocalizerType } from '../types/Util';
import { mergeRefs } from './_util';
import { createRefMerger } from './_util';

const MAX_LENGTH = 64 * 1024;
const colonsRegex = /(?:^|\s):[a-z0-9-_+]+:?/gi;
Expand Down Expand Up @@ -237,6 +237,7 @@ export const CompositionInput = ({
const focusRef = React.useRef(false);
const editorStateRef = React.useRef<EditorState>(editorRenderState);
const rootElRef = React.useRef<HTMLDivElement>();
const rootElRefMerger = React.useMemo(createRefMerger, []);

// This function sets editorState and also keeps a reference to the newly set
// state so we can reference the state in effects and callbacks without
Expand Down Expand Up @@ -757,7 +758,7 @@ export const CompositionInput = ({
{({ measureRef }) => (
<div
className="module-composition-input__input"
ref={mergeRefs(popperRef, measureRef, rootElRef)}
ref={rootElRefMerger(popperRef, measureRef, rootElRef)}
>
<div
className={classNames(
Expand Down
29 changes: 17 additions & 12 deletions ts/components/_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
);
8 changes: 6 additions & 2 deletions ts/components/conversation/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { ContactType } from '../../types/Contact';
import { getIncrement } from '../../util/timer';
import { isFileDangerous } from '../../util/isFileDangerous';
import { ColorType, LocalizerType } from '../../types/Util';
import { mergeRefs } from '../_util';
import { createRefMerger } from '../_util';
import { ContextMenu, ContextMenuTrigger, MenuItem } from 'react-contextmenu';

interface Trigger {
Expand Down Expand Up @@ -181,6 +181,7 @@ export class Message extends React.PureComponent<Props, State> {
public reactionsContainerRef: React.RefObject<
HTMLDivElement
> = React.createRef();
public reactionsContainerRefMerger = createRefMerger();

public wideMl: MediaQueryList;

Expand Down Expand Up @@ -1572,7 +1573,10 @@ export class Message extends React.PureComponent<Props, State> {
<Reference>
{({ ref: popperRef }) => (
<div
ref={mergeRefs(this.reactionsContainerRef, popperRef)}
ref={this.reactionsContainerRefMerger(
this.reactionsContainerRef,
popperRef
)}
className={classNames(
'module-message__reactions',
outgoing
Expand Down

0 comments on commit 17f212f

Please sign in to comment.