The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/apps/meteor/app/ui-message/client/findParentMessage.ts

41 lines
1.1 KiB

import type { IMessage } from '@rocket.chat/core-typings';
import { callWithErrorHandling } from '../../../client/lib/utils/callWithErrorHandling';
import { ChatMessage } from '../../models/client';
import { withDebouncing } from '../../../lib/utils/highOrderFunctions';
export const findParentMessage = (() => {
const waiting: string[] = [];
let resolve: (resolved: IMessage[] | PromiseLike<IMessage[]>) => void;
let pending = new Promise<IMessage[]>((r) => {
resolve = r;
});
const getMessages = withDebouncing({ wait: 500 })(async function () {
const _tmp = [...waiting];
waiting.length = 0;
resolve(callWithErrorHandling('getMessages', _tmp));
pending = new Promise<IMessage[]>((r) => {
resolve = r;
});
});
const get = async (tmid: IMessage['_id']) => {
void getMessages();
const messages = await pending;
return messages.find(({ _id }) => _id === tmid);
};
return async (tmid: IMessage['_id']) => {
const message = ChatMessage.findOne({ _id: tmid });
if (message) {
return message;
}
if (waiting.indexOf(tmid) === -1) {
waiting.push(tmid);
}
return get(tmid);
};
})();