converse-roster: Reconnection bugfixes.

Don't remove cached presences in `afterTearDown` event.
We might reconnect again and resume the session, in which case we need
that data still.

Instead, we remove cached presences when `clearSession` fires, because
then we know we're not reconnecting.

When reconnecting, we don't fetch presences from cache, we still have
them (since we no longer remove them in `afterTearDown`).

When reconnecting and not resuming, we clear the presences from cache,
since we're starting a new session.
This commit is contained in:
JC Brand 2019-08-07 16:05:22 +02:00
parent 3dc2b2b695
commit b176161ecf

View File

@ -941,19 +941,17 @@ converse.plugins.add('converse-roster', {
}); });
}); });
_converse.api.listen.on('afterTearDown', () => { function clearPresences () {
if (_converse.presences) { if (_converse.presences) {
_converse.presences.each(p => { _converse.presences.forEach(p => {
p.resources.reject(r => r === undefined).forEach(r => r.destroy({'silent': true})); p.resources.reject(r => r === undefined).forEach(r => r.destroy({'silent': true}));
p.save({'show': 'offline'}, {'silent': true})
}); });
_converse.presences.clearSession();
} }
}); }
_converse.api.listen.on('clearSession', () => { _converse.api.listen.on('clearSession', () => {
if (_converse.presences) { clearPresences();
_converse.presences.browserStorage._clear();
}
if (_converse.shouldClearCache()) { if (_converse.shouldClearCache()) {
if (_converse.roster) { if (_converse.roster) {
_.invoke(_converse, 'roster.data.destroy'); _.invoke(_converse, 'roster.data.destroy');
@ -969,16 +967,18 @@ converse.plugins.add('converse-roster', {
}); });
_converse.api.listen.on('statusInitialized', (reconnecting) => { _converse.api.listen.on('statusInitialized', (reconnecting) => {
if (!reconnecting) { if (reconnecting) {
_converse.presences = new _converse.Presences(); // When reconnecting and not resuming a previous session,
} // we clear all cached presence data, since it might be stale
_converse.presences.browserStorage = // and we'll receive new presence updates
new BrowserStorage.session(`converse.presences-${_converse.bare_jid}`); !_converse.haveResumed() && clearPresences();
if (_converse.haveResumed()) {
_converse.presences.fetch();
} else { } else {
_converse.presences.clearSession(); _converse.presences = new _converse.Presences();
const id = `converse.presences-${_converse.bare_jid}`;
_converse.presences.browserStorage = new BrowserStorage.session(id);
// We might be continuing an existing session, so we fetch
// cached presence data.
_converse.presences.fetch();
} }
/** /**
* Triggered once the _converse.Presences collection has been * Triggered once the _converse.Presences collection has been