Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit f641394

Browse files
authored
Optimize redundant chat fetch (#9584)
* optimize redundant chat fetch * fix reverse altering returned data, update typing of data to readonly
1 parent ca1a507 commit f641394

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

β€Žpackages/client-core/src/components/InstanceChat/index.tsxβ€Ž

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,15 @@ export const useChatHooks = ({ chatWindowOpen, setUnreadMessages, messageRefInpu
6565

6666
const targetChannelId = useHookstate(getMutableState(ChannelState).targetChannelId)
6767

68+
/** @todo allow paginated scrolling to retrieve earlier messages */
6869
const messages = useFind(messagePath, {
6970
query: {
70-
channelId: targetChannelId.value
71+
channelId: targetChannelId.value,
72+
$limit: 20,
73+
$sort: { createdAt: -1 }
7174
}
7275
})
73-
const mutateMessage = useMutation(messagePath)
76+
const mutateMessage = useMutation(messagePath, false)
7477

7578
useEffect(() => {
7679
if (messages.data?.length && !chatWindowOpen) setUnreadMessages(true)
@@ -215,9 +218,7 @@ export const InstanceChat = ({ styles = defaultStyles }: InstanceChatProps) => {
215218
messageRefInput: messageRefInput as any
216219
})
217220

218-
const sortedMessages = messages.data
219-
? messages.data.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime())
220-
: []
221+
const sortedMessages = [...messages.data].reverse()
221222

222223
const user = useHookstate(getMutableState(AuthState).user)
223224

@@ -305,21 +306,21 @@ export const InstanceChat = ({ styles = defaultStyles }: InstanceChatProps) => {
305306
} ${styles.dFlex}`}
306307
>
307308
<div className={styles.msgWrapper}>
308-
{messages[index - 1] && messages[index - 1].isNotification ? (
309+
{sortedMessages[index - 1] && sortedMessages[index - 1].isNotification ? (
309310
<h3 className={styles.sender}>{message.sender.name as UserName}</h3>
310311
) : (
311-
messages[index - 1] &&
312-
message.senderId !== messages[index - 1].senderId && (
312+
sortedMessages[index - 1] &&
313+
message.senderId !== sortedMessages[index - 1].senderId && (
313314
<h3 className={styles.sender}>{message.sender.name as UserName}</h3>
314315
)
315316
)}
316317
<p className={styles.text}>{message.text}</p>
317318
</div>
318-
{index !== 0 && messages[index - 1] && messages[index - 1].isNotification ? (
319+
{index !== 0 && sortedMessages[index - 1] && sortedMessages[index - 1].isNotification ? (
319320
<Avatar src={userThumbnail} className={styles.avatar} />
320321
) : (
321-
messages[index - 1] &&
322-
message.senderId !== messages[index - 1].senderId && (
322+
sortedMessages[index - 1] &&
323+
message.senderId !== sortedMessages[index - 1].senderId && (
323324
<Avatar src={userThumbnail} className={styles.avatar} />
324325
)
325326
)}

β€Žpackages/client-core/src/systems/ui/RecordingsWidgetUI.tsxβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ const RecordingPlayback = () => {
330330
const RecordingsList = () => {
331331
const recording = useFind(recordingPath)
332332

333-
const sortedRecordings = recording.data.sort(
333+
const sortedRecordings = [...recording.data].sort(
334334
(a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()
335335
)
336336

β€Žpackages/engine/src/common/functions/FeathersHooks.tsxβ€Ž

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export const useFind = <S extends keyof ServiceTypes>(serviceName: S, params: Pa
208208
skip: paginate.query.$skip,
209209
limit: paginate.query.$limit,
210210
sort: paginate.query.$sort,
211-
data: data as ArrayOrPaginatedType<(typeof response)['data']>
211+
data: data as Readonly<ArrayOrPaginatedType<(typeof response)['data']>>
212212
}
213213
}
214214

@@ -244,17 +244,32 @@ type RemoveMethodParameters<S extends keyof ServiceTypes> = ServiceTypes[S]['rem
244244
* by the caller. as you create/update/patch/remove
245245
* entities using this helper, the entities cache gets updated
246246
*/
247-
export function useMutation<S extends keyof ServiceTypes>(serviceName: S) {
247+
export function useMutation<S extends keyof ServiceTypes>(serviceName: S, forceRefetch = true) {
248248
const state = useHookstate({
249249
status: 'idle',
250250
data: null as unknown | null,
251251
error: null as string | null
252252
})
253253

254-
const create = useMethod('create', created, serviceName, state) as CreateMethodParameters<S>
255-
const update = useMethod('update', updated, serviceName, state) as UpdateMethodParameters<S>
256-
const patch = useMethod('patch', updated, serviceName, state) as PatchMethodParameters<S>
257-
const remove = useMethod('remove', removed, serviceName, state) as RemoveMethodParameters<S>
254+
const create = useMethod(
255+
'create',
256+
forceRefetch ? created : undefined,
257+
serviceName,
258+
state
259+
) as CreateMethodParameters<S>
260+
const update = useMethod(
261+
'update',
262+
forceRefetch ? updated : undefined,
263+
serviceName,
264+
state
265+
) as UpdateMethodParameters<S>
266+
const patch = useMethod('patch', forceRefetch ? updated : undefined, serviceName, state) as PatchMethodParameters<S>
267+
const remove = useMethod(
268+
'remove',
269+
forceRefetch ? removed : undefined,
270+
serviceName,
271+
state
272+
) as RemoveMethodParameters<S>
258273

259274
const mutation = useMemo(
260275
() => ({
@@ -274,7 +289,7 @@ export function useMutation<S extends keyof ServiceTypes>(serviceName: S) {
274289

275290
function useMethod(
276291
method: Methods,
277-
action: (props: { serviceName: keyof ServiceTypes; item: any }) => void,
292+
action: undefined | ((props: { serviceName: keyof ServiceTypes; item: any }) => void),
278293
serviceName: keyof ServiceTypes,
279294
state: State<any>
280295
) {
@@ -284,7 +299,7 @@ function useMethod(
284299
state.merge({ status: 'loading', loading: true, data: null, error: null })
285300
return service[method](...args)
286301
.then((item) => {
287-
action({ serviceName, item })
302+
action && action({ serviceName, item })
288303
state.merge({ status: 'success', loading: false, data: item })
289304
return item
290305
})

β€Žpackages/ui/src/components/tailwind/RecordingList/index.tsxβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const RecordingsList = (props: {
6060
query: { $sort: { createdAt: -1 }, $limit: 10, $skip: 0 }
6161
})
6262

63-
const sortedRecordings = recording.data.sort(sortByNewest)
63+
const sortedRecordings = [...recording.data].sort(sortByNewest)
6464

6565
const RecordingItem = (props: { recording: RecordingType }) => {
6666
const { recording } = props

0 commit comments

Comments
Β (0)