Resolve fetched promise when clearing messages

This fixes an issue where chats stay empty after reconnection when
`clear_messages_on_reconnection` is `true`.
This commit is contained in:
JC Brand 2022-04-08 11:00:36 +02:00
parent ca0953e4bd
commit 0c049dd364
3 changed files with 8 additions and 14 deletions

View File

@ -94,11 +94,11 @@ const ChatBox = ModelWithContact.extend({
this.messages.fetched.then(() => { this.messages.fetched.then(() => {
this.pruneHistoryWhenScrolledDown(); this.pruneHistoryWhenScrolledDown();
/** /**
* Triggered whenever a `_converse.ChatBox` instance has fetched its messages from * Triggered whenever a { @link _converse.ChatBox } or ${ @link _converse.ChatRoom }
* `sessionStorage` but **NOT** from the server. * has fetched its messages from the local cache.
* @event _converse#afterMessagesFetched * @event _converse#afterMessagesFetched
* @type {_converse.ChatBoxView | _converse.ChatRoomView} * @type { _converse.ChatBox| _converse.ChatRoom }
* @example _converse.api.listen.on('afterMessagesFetched', view => { ... }); * @example _converse.api.listen.on('afterMessagesFetched', (chat) => { ... });
*/ */
api.trigger('afterMessagesFetched', this); api.trigger('afterMessagesFetched', this);
}); });
@ -270,9 +270,9 @@ const ChatBox = ModelWithContact.extend({
this.messages.trigger('reset'); this.messages.trigger('reset');
log.error(e); log.error(e);
} finally { } finally {
delete this.msg_chain; // No point in fetching messages from the cache if it's been cleared.
delete this.messages.fetched_flag; // Make sure to resolve the fetched promise to avoid freezes.
this.messages.fetched = getOpenPromise(); this.messages.fetched.resolve();
} }
}, },

View File

@ -325,12 +325,7 @@ const ChatRoomMixin = {
async onRoomEntered () { async onRoomEntered () {
await this.occupants.fetchMembers(); await this.occupants.fetchMembers();
if (api.settings.get('clear_messages_on_reconnection')) { if (api.settings.get('clear_messages_on_reconnection')) {
// Don't call this.clearMessages because we don't want to await this.clearMessages();
// recreate promises, since that will cause some existing
// awaiters to never proceed.
await this.messages.clearStore();
// A bit hacky. No need to fetch messages after clearing
this.messages.fetched.resolve();
} else { } else {
await this.fetchMessages(); await this.fetchMessages();
} }

View File

@ -18,7 +18,6 @@ export default class MessageForm extends ElementView {
this.listenTo(this.model, 'change:composing_spoiler', () => this.render()); this.listenTo(this.model, 'change:composing_spoiler', () => this.render());
this.handleEmojiSelection = ({ detail }) => { this.handleEmojiSelection = ({ detail }) => {
debugger;
if (this.model.get('jid') === detail.jid) { if (this.model.get('jid') === detail.jid) {
this.insertIntoTextArea(detail.value, detail.autocompleting, false, detail.ac_position); this.insertIntoTextArea(detail.value, detail.autocompleting, false, detail.ac_position);
} }