Skip to content

Commit 7a68627

Browse files
authored
chore: Replace ReactiveDict with Map+Emitter (RocketChat#36684)
1 parent d9a685c commit 7a68627

3 files changed

Lines changed: 20 additions & 16 deletions

File tree

apps/meteor/app/ui/client/lib/UserAction.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import type { IExtras, IRoomActivity, IActionsObject, IUser } from '@rocket.chat/core-typings';
1+
import type { IExtras, IRoomActivity, IUser } from '@rocket.chat/core-typings';
2+
import { Emitter } from '@rocket.chat/emitter';
23
import { debounce } from 'lodash';
34
import { Meteor } from 'meteor/meteor';
4-
import { ReactiveDict } from 'meteor/reactive-dict';
55

66
import { Users } from '../../../../client/stores';
77
import { settings } from '../../../settings/client';
@@ -25,7 +25,8 @@ const continuingIntervals = new Map();
2525
const roomActivities = new Map<string, Set<string>>();
2626
const rooms = new Map<string, (username: string, activityType: string[], extras?: object | undefined) => void>();
2727

28-
const performingUsers = new ReactiveDict<IActionsObject>();
28+
const performingUsers = new Map<string, IRoomActivity>();
29+
const performingUsersEmitter = new Emitter<{ changed: void }>();
2930

3031
const shownName = function (user: IUser | null | undefined): string | undefined {
3132
if (!user) {
@@ -64,6 +65,7 @@ function handleStreamAction(rid: string, username: string, activityTypes: string
6465
}
6566

6667
performingUsers.set(rid, roomActivities);
68+
performingUsersEmitter.emit('changed');
6769
}
6870
export const UserAction = new (class {
6971
addStream(rid: string): () => void {
@@ -169,4 +171,8 @@ export const UserAction = new (class {
169171
get(roomId: string): IRoomActivity | undefined {
170172
return performingUsers.get(roomId);
171173
}
174+
175+
subscribe(onChanged: () => void): () => void {
176+
return performingUsersEmitter.on('changed', onChanged);
177+
}
172178
})();

apps/meteor/client/views/room/composer/ComposerUserActionIndicator/ComposerUserActionIndicator.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { Box } from '@rocket.chat/fuselage';
22
import type { ReactElement } from 'react';
3-
import { useCallback, Fragment } from 'react';
3+
import { useCallback, Fragment, useSyncExternalStore, useMemo } from 'react';
44
import { useTranslation } from 'react-i18next';
55

66
import { UserAction } from '../../../../../app/ui/client/lib/UserAction';
7-
import { useReactiveValue } from '../../../../hooks/useReactiveValue';
87

98
const maxUsernames = 5;
109

1110
const ComposerUserActionIndicator = ({ rid, tmid }: { rid: string; tmid?: string }): ReactElement => {
1211
const { t } = useTranslation();
13-
const actions = useReactiveValue(
14-
useCallback(() => {
15-
const roomAction = UserAction.get(tmid || rid) || {};
16-
17-
const activities = Object.entries(roomAction);
18-
19-
return activities
12+
const roomAction = useSyncExternalStore(
13+
UserAction.subscribe,
14+
useCallback(() => UserAction.get(tmid || rid), [rid, tmid]),
15+
);
16+
const actions = useMemo(
17+
() =>
18+
Object.entries(roomAction ?? {})
2019
.map(([key, _users]) => {
2120
const action = key.split('-')[1];
2221

@@ -33,9 +32,10 @@ const ComposerUserActionIndicator = ({ rid, tmid }: { rid: string; tmid?: string
3332
.filter(Boolean) as {
3433
action: 'typing' | 'recording' | 'uploading' | 'playing';
3534
users: string[];
36-
}[];
37-
}, [rid, tmid]),
35+
}[],
36+
[roomAction],
3837
);
38+
3939
return (
4040
<Box
4141
h='x20'

packages/core-typings/src/IUserAction.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@ export type IExtras = {
1111
export type IActivity = Record<string, NodeJS.Timeout>;
1212

1313
export type IRoomActivity = Record<string, IActivity>;
14-
15-
export type IActionsObject = Record<string, IRoomActivity>;

0 commit comments

Comments
 (0)