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/app/ui-utils/client/lib/renderMessageBody.js

35 lines
1.0 KiB

import s from 'underscore.string';
import { callbacks } from '../../../callbacks';
const generateKeyDefault = (...args) => args.map((item) => JSON.stringify(item)).join('-');
const mem = (fn, tm = 500, generateKey = generateKeyDefault) => {
const cache = {};
const timeout = {};
const invalidateCache = (key) => delete cache[key];
return (...args) => {
const key = generateKey(...args);
if (!key) {
return fn(...args);
}
if (!cache[key]) {
cache[key] = fn(...args);
}
if (timeout[key]) {
clearTimeout(timeout[key]);
}
timeout[key] = setTimeout(invalidateCache, tm, key);
return cache[key];
};
};
export const renderMessageBody = mem((message) => {
message.html = s.trim(message.msg) ? s.escapeHTML(message.msg) : '';
const { tokens, html } = callbacks.run('renderMessage', message);
return (Array.isArray(tokens) ? tokens.reverse() : [])
.reduce((html, { token, text }) => html.replace(token, () => text), html);
}, 5000, ({ _id, _updatedAt }) => _id && _updatedAt && _id + _updatedAt);