Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Chats holds to load history for some time #26425

Merged
merged 4 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions apps/meteor/app/ui-utils/client/lib/RoomHistoryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import differenceInMilliseconds from 'date-fns/differenceInMilliseconds';
import { Emitter } from '@rocket.chat/emitter';
import { escapeHTML } from '@rocket.chat/string-helpers';

import { waitUntilWrapperExists } from './waitUntilWrapperExists';
import { RoomManager } from './RoomManager';
import { readMessage } from './readMessages';
import { renderMessageBody } from '../../../../client/lib/utils/renderMessageBody';
Expand Down Expand Up @@ -40,26 +41,6 @@ export const normalizeThreadMessage = ({ ...message }) => {
}
};

export const waitUntilWrapperExists = async (selector = '.messages-box .wrapper') => {
const element = document.querySelector(selector);
if (element?.length) {
return element;
}
return new Promise((resolve) => {
const observer = new MutationObserver(function (mutations, obs) {
const element = document.querySelector(selector);
if (element) {
obs.disconnect(); // stop observing
return resolve(element);
}
});
observer.observe(document, {
childList: true,
subtree: true,
});
});
};

export const upsertMessage = async ({ msg, subscription, uid = Tracker.nonreactive(() => Meteor.userId()) }, collection = ChatMessage) => {
const userId = msg.u && msg.u._id;

Expand Down
19 changes: 19 additions & 0 deletions apps/meteor/app/ui-utils/client/lib/waitUntilWrapperExists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export const waitUntilWrapperExists = async (selector = '.messages-box .wrapper'): Promise<Element> => {
const element = document.querySelector(selector);
return new Promise((resolve) => {
if (element) {
return resolve(element);
}
const observer = new MutationObserver(function (_, obs) {
const element = document.querySelector(selector);
if (element) {
obs.disconnect(); // stop observing
return resolve(element);
}
});
observer.observe(document, {
childList: true,
subtree: true,
});
});
};
41 changes: 41 additions & 0 deletions apps/meteor/tests/unit/app/ui-utils/client.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* eslint-env mocha */
import { expect } from 'chai';
import { JSDOM } from 'jsdom';

import { waitUntilWrapperExists } from '../../../../app/ui-utils/client/lib/waitUntilWrapperExists';

describe('waitUntilWrapperExists', () => {
const globalDocument = global.document;
const mutationObserver = global.MutationObserver;

beforeEach(() => {
const dom = new JSDOM(
`<html>
<body>
<span class="ready" />
</body>
</html>`,
{ url: 'http://localhost' },
);
global.document = dom.window.document;
global.MutationObserver = dom.window.MutationObserver;
});

afterEach(() => {
global.document = globalDocument;
global.MutationObserver = mutationObserver;
});

it('should return the element when it is already in the dom', async () => {
expect(await waitUntilWrapperExists('.ready')).to.be.equal(document.querySelector('.ready'));
});

it('should await until the element be in the dom and return it', async () => {
setTimeout(() => {
const element = document.createElement('div');
element.setAttribute('class', 'not-ready');
document.body.appendChild(element);
}, 5);
expect(await waitUntilWrapperExists('.not-ready')).to.be.equal(document.querySelector('.not-ready'));
});
});