Remove rateLimitedFunc

This commit is contained in:
David Baker 2021-07-01 18:35:38 +01:00
parent fdef1f9b68
commit ae16efcf5b
8 changed files with 24 additions and 73 deletions

View file

@ -4,7 +4,6 @@ src/Markdown.js
src/NodeAnimator.js src/NodeAnimator.js
src/components/structures/RoomDirectory.js src/components/structures/RoomDirectory.js
src/components/views/rooms/MemberList.js src/components/views/rooms/MemberList.js
src/ratelimitedfunc.js
src/utils/DMRoomMap.js src/utils/DMRoomMap.js
src/utils/MultiInviter.js src/utils/MultiInviter.js
test/components/structures/MessagePanel-test.js test/components/structures/MessagePanel-test.js

View file

@ -23,7 +23,6 @@ import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { VerificationRequest } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest"; import { VerificationRequest } from "matrix-js-sdk/src/crypto/verification/request/VerificationRequest";
import dis from '../../dispatcher/dispatcher'; import dis from '../../dispatcher/dispatcher';
import RateLimitedFunc from '../../ratelimitedfunc';
import GroupStore from '../../stores/GroupStore'; import GroupStore from '../../stores/GroupStore';
import { import {
RIGHT_PANEL_PHASES_NO_ARGS, RIGHT_PANEL_PHASES_NO_ARGS,
@ -48,6 +47,7 @@ import FilePanel from "./FilePanel";
import NotificationPanel from "./NotificationPanel"; import NotificationPanel from "./NotificationPanel";
import ResizeNotifier from "../../utils/ResizeNotifier"; import ResizeNotifier from "../../utils/ResizeNotifier";
import PinnedMessagesCard from "../views/right_panel/PinnedMessagesCard"; import PinnedMessagesCard from "../views/right_panel/PinnedMessagesCard";
import { DebouncedFunc, throttle } from 'lodash';
interface IProps { interface IProps {
room?: Room; // if showing panels for a given room, this is set room?: Room; // if showing panels for a given room, this is set
@ -73,7 +73,7 @@ interface IState {
export default class RightPanel extends React.Component<IProps, IState> { export default class RightPanel extends React.Component<IProps, IState> {
static contextType = MatrixClientContext; static contextType = MatrixClientContext;
private readonly delayedUpdate: RateLimitedFunc; private readonly delayedUpdate: DebouncedFunc<() => void>;
private dispatcherRef: string; private dispatcherRef: string;
constructor(props, context) { constructor(props, context) {
@ -85,9 +85,9 @@ export default class RightPanel extends React.Component<IProps, IState> {
member: this.getUserForPanel(), member: this.getUserForPanel(),
}; };
this.delayedUpdate = new RateLimitedFunc(() => { this.delayedUpdate = throttle(() => {
this.forceUpdate(); this.forceUpdate();
}, 500); }, 500, { leading: true, trailing: true });
} }
// Helper function to split out the logic for getPhaseFromProps() and the constructor // Helper function to split out the logic for getPhaseFromProps() and the constructor

View file

@ -37,7 +37,6 @@ import Modal from '../../Modal';
import * as sdk from '../../index'; import * as sdk from '../../index';
import CallHandler, { PlaceCallType } from '../../CallHandler'; import CallHandler, { PlaceCallType } from '../../CallHandler';
import dis from '../../dispatcher/dispatcher'; import dis from '../../dispatcher/dispatcher';
import rateLimitedFunc from '../../ratelimitedfunc';
import * as Rooms from '../../Rooms'; import * as Rooms from '../../Rooms';
import eventSearch, { searchPagination } from '../../Searching'; import eventSearch, { searchPagination } from '../../Searching';
import MainSplit from './MainSplit'; import MainSplit from './MainSplit';
@ -82,6 +81,7 @@ import { IOpts } from "../../createRoom";
import { replaceableComponent } from "../../utils/replaceableComponent"; import { replaceableComponent } from "../../utils/replaceableComponent";
import UIStore from "../../stores/UIStore"; import UIStore from "../../stores/UIStore";
import EditorStateTransfer from "../../utils/EditorStateTransfer"; import EditorStateTransfer from "../../utils/EditorStateTransfer";
import { throttle } from "lodash";
const DEBUG = false; const DEBUG = false;
let debuglog = function(msg: string) {}; let debuglog = function(msg: string) {};
@ -675,8 +675,8 @@ export default class RoomView extends React.Component<IProps, IState> {
); );
} }
// cancel any pending calls to the rate_limited_funcs // cancel any pending calls to the throttled updated
this.updateRoomMembers.cancelPendingCall(); this.updateRoomMembers.cancel();
for (const watcher of this.settingWatchers) { for (const watcher of this.settingWatchers) {
SettingsStore.unwatchSetting(watcher); SettingsStore.unwatchSetting(watcher);
@ -1092,7 +1092,7 @@ export default class RoomView extends React.Component<IProps, IState> {
return; return;
} }
this.updateRoomMembers(member); this.updateRoomMembers();
}; };
private onMyMembership = (room: Room, membership: string, oldMembership: string) => { private onMyMembership = (room: Room, membership: string, oldMembership: string) => {
@ -1114,10 +1114,10 @@ export default class RoomView extends React.Component<IProps, IState> {
} }
// rate limited because a power level change will emit an event for every member in the room. // rate limited because a power level change will emit an event for every member in the room.
private updateRoomMembers = rateLimitedFunc(() => { private updateRoomMembers = throttle(() => {
this.updateDMState(); this.updateDMState();
this.updateE2EStatus(this.state.room); this.updateE2EStatus(this.state.room);
}, 500); }, 500, { leading: true, trailing: true });
private checkDesktopNotifications() { private checkDesktopNotifications() {
const memberCount = this.state.room.getJoinedMemberCount() + this.state.room.getInvitedMemberCount(); const memberCount = this.state.room.getJoinedMemberCount() + this.state.room.getInvitedMemberCount();

View file

@ -21,7 +21,6 @@ import { Room } from 'matrix-js-sdk/src/models/room';
import { MatrixClientPeg } from "../../../MatrixClientPeg"; import { MatrixClientPeg } from "../../../MatrixClientPeg";
import AppsDrawer from './AppsDrawer'; import AppsDrawer from './AppsDrawer';
import RateLimitedFunc from '../../../ratelimitedfunc';
import SettingsStore from "../../../settings/SettingsStore"; import SettingsStore from "../../../settings/SettingsStore";
import AutoHideScrollbar from "../../structures/AutoHideScrollbar"; import AutoHideScrollbar from "../../structures/AutoHideScrollbar";
import { UIFeature } from "../../../settings/UIFeature"; import { UIFeature } from "../../../settings/UIFeature";
@ -29,6 +28,7 @@ import ResizeNotifier from "../../../utils/ResizeNotifier";
import CallViewForRoom from '../voip/CallViewForRoom'; import CallViewForRoom from '../voip/CallViewForRoom';
import { objectHasDiff } from "../../../utils/objects"; import { objectHasDiff } from "../../../utils/objects";
import { replaceableComponent } from "../../../utils/replaceableComponent"; import { replaceableComponent } from "../../../utils/replaceableComponent";
import { throttle } from 'lodash';
interface IProps { interface IProps {
// js-sdk room object // js-sdk room object
@ -99,9 +99,9 @@ export default class AuxPanel extends React.Component<IProps, IState> {
} }
} }
private rateLimitedUpdate = new RateLimitedFunc(() => { private rateLimitedUpdate = throttle(() => {
this.setState({ counters: this.computeCounters() }); this.setState({ counters: this.computeCounters() });
}, 500); }, 500, { leading: true, trailing: true });
private computeCounters() { private computeCounters() {
const counters = []; const counters = [];

View file

@ -22,7 +22,6 @@ import { _t } from '../../../languageHandler';
import SdkConfig from '../../../SdkConfig'; import SdkConfig from '../../../SdkConfig';
import dis from '../../../dispatcher/dispatcher'; import dis from '../../../dispatcher/dispatcher';
import { isValid3pidInvite } from "../../../RoomInvite"; import { isValid3pidInvite } from "../../../RoomInvite";
import rateLimitedFunction from "../../../ratelimitedfunc";
import { MatrixClientPeg } from "../../../MatrixClientPeg"; import { MatrixClientPeg } from "../../../MatrixClientPeg";
import { CommunityPrototypeStore } from "../../../stores/CommunityPrototypeStore"; import { CommunityPrototypeStore } from "../../../stores/CommunityPrototypeStore";
import BaseCard from "../right_panel/BaseCard"; import BaseCard from "../right_panel/BaseCard";
@ -43,6 +42,7 @@ import AccessibleButton from '../elements/AccessibleButton';
import EntityTile from "./EntityTile"; import EntityTile from "./EntityTile";
import MemberTile from "./MemberTile"; import MemberTile from "./MemberTile";
import BaseAvatar from '../avatars/BaseAvatar'; import BaseAvatar from '../avatars/BaseAvatar';
import { throttle } from 'lodash';
const INITIAL_LOAD_NUM_MEMBERS = 30; const INITIAL_LOAD_NUM_MEMBERS = 30;
const INITIAL_LOAD_NUM_INVITED = 5; const INITIAL_LOAD_NUM_INVITED = 5;
@ -133,7 +133,7 @@ export default class MemberList extends React.Component<IProps, IState> {
} }
// cancel any pending calls to the rate_limited_funcs // cancel any pending calls to the rate_limited_funcs
this.updateList.cancelPendingCall(); this.updateList.cancel();
} }
/** /**
@ -237,9 +237,9 @@ export default class MemberList extends React.Component<IProps, IState> {
if (this.canInvite !== this.state.canInvite) this.setState({ canInvite: this.canInvite }); if (this.canInvite !== this.state.canInvite) this.setState({ canInvite: this.canInvite });
}; };
private updateList = rateLimitedFunction(() => { private updateList = throttle(() => {
this.updateListNow(); this.updateListNow();
}, 500); }, 500, { leading: true, trailing: true });
private updateListNow(): void { private updateListNow(): void {
const members = this.roomMembers(); const members = this.roomMembers();

View file

@ -20,7 +20,6 @@ import PropTypes from 'prop-types';
import classNames from 'classnames'; import classNames from 'classnames';
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import { MatrixClientPeg } from '../../../MatrixClientPeg'; import { MatrixClientPeg } from '../../../MatrixClientPeg';
import RateLimitedFunc from '../../../ratelimitedfunc';
import SettingsStore from "../../../settings/SettingsStore"; import SettingsStore from "../../../settings/SettingsStore";
import RoomHeaderButtons from '../right_panel/RoomHeaderButtons'; import RoomHeaderButtons from '../right_panel/RoomHeaderButtons';
@ -31,6 +30,7 @@ import RoomTopic from "../elements/RoomTopic";
import RoomName from "../elements/RoomName"; import RoomName from "../elements/RoomName";
import { PlaceCallType } from "../../../CallHandler"; import { PlaceCallType } from "../../../CallHandler";
import { replaceableComponent } from "../../../utils/replaceableComponent"; import { replaceableComponent } from "../../../utils/replaceableComponent";
import { throttle } from 'lodash';
@replaceableComponent("views.rooms.RoomHeader") @replaceableComponent("views.rooms.RoomHeader")
export default class RoomHeader extends React.Component { export default class RoomHeader extends React.Component {
@ -73,10 +73,9 @@ export default class RoomHeader extends React.Component {
this._rateLimitedUpdate(); this._rateLimitedUpdate();
}; };
_rateLimitedUpdate = new RateLimitedFunc(function() { _rateLimitedUpdate = throttle(() => {
/* eslint-disable @babel/no-invalid-this */
this.forceUpdate(); this.forceUpdate();
}, 500); }, 500, { leading: true, trailing: true });
render() { render() {
let searchStatus = null; let searchStatus = null;

View file

@ -39,7 +39,6 @@ import Modal from '../../../Modal';
import { _t, _td } from '../../../languageHandler'; import { _t, _td } from '../../../languageHandler';
import ContentMessages from '../../../ContentMessages'; import ContentMessages from '../../../ContentMessages';
import MatrixClientContext from "../../../contexts/MatrixClientContext"; import MatrixClientContext from "../../../contexts/MatrixClientContext";
import RateLimitedFunc from '../../../ratelimitedfunc';
import { Action } from "../../../dispatcher/actions"; import { Action } from "../../../dispatcher/actions";
import { containsEmoji } from "../../../effects/utils"; import { containsEmoji } from "../../../effects/utils";
import { CHAT_EFFECTS } from '../../../effects'; import { CHAT_EFFECTS } from '../../../effects';
@ -53,6 +52,7 @@ import { Room } from 'matrix-js-sdk/src/models/room';
import ErrorDialog from "../dialogs/ErrorDialog"; import ErrorDialog from "../dialogs/ErrorDialog";
import QuestionDialog from "../dialogs/QuestionDialog"; import QuestionDialog from "../dialogs/QuestionDialog";
import { ActionPayload } from "../../../dispatcher/payloads"; import { ActionPayload } from "../../../dispatcher/payloads";
import { DebouncedFunc, throttle } from 'lodash';
function addReplyToMessageContent( function addReplyToMessageContent(
content: IContent, content: IContent,
@ -138,7 +138,7 @@ export default class SendMessageComposer extends React.Component<IProps> {
static contextType = MatrixClientContext; static contextType = MatrixClientContext;
context!: React.ContextType<typeof MatrixClientContext>; context!: React.ContextType<typeof MatrixClientContext>;
private readonly prepareToEncrypt?: RateLimitedFunc; private readonly prepareToEncrypt?: DebouncedFunc<() => void>;
private readonly editorRef = createRef<BasicMessageComposer>(); private readonly editorRef = createRef<BasicMessageComposer>();
private model: EditorModel = null; private model: EditorModel = null;
private currentlyComposedEditorState: SerializedPart[] = null; private currentlyComposedEditorState: SerializedPart[] = null;
@ -149,9 +149,9 @@ export default class SendMessageComposer extends React.Component<IProps> {
super(props); super(props);
this.context = context; // otherwise React will only set it prior to render due to type def above this.context = context; // otherwise React will only set it prior to render due to type def above
if (this.context.isCryptoEnabled() && this.context.isRoomEncrypted(this.props.room.roomId)) { if (this.context.isCryptoEnabled() && this.context.isRoomEncrypted(this.props.room.roomId)) {
this.prepareToEncrypt = new RateLimitedFunc(() => { this.prepareToEncrypt = throttle(() => {
this.context.prepareToEncrypt(this.props.room); this.context.prepareToEncrypt(this.props.room);
}, 60000); }, 60000, { leading: true, trailing: false });
} }
window.addEventListener("beforeunload", this.saveStoredEditorState); window.addEventListener("beforeunload", this.saveStoredEditorState);

View file

@ -1,47 +0,0 @@
/*
Copyright 2016 OpenMarket Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* 'debounces' a function to only execute every n milliseconds.
* Useful when react-sdk gets many, many events but only wants
* to update the interface once for all of them.
*
* Note that the function must not take arguments, since the args
* could be different for each invocation of the function.
*
* The returned function has a 'cancelPendingCall' property which can be called
* on unmount or similar to cancel any pending update.
*/
import {throttle} from "lodash";
export default function ratelimitedfunc(fn, time) {
const throttledFn = throttle(fn, time, {
leading: true,
trailing: true,
});
const _bind = throttledFn.bind;
throttledFn.bind = function() {
const boundFn = _bind.apply(throttledFn, arguments);
boundFn.cancelPendingCall = throttledFn.cancelPendingCall;
return boundFn;
};
throttledFn.cancelPendingCall = function() {
throttledFn.cancel();
};
return throttledFn;
}