Commit 2fcce781 authored by JC Brand's avatar JC Brand

Add a `cleanup` hook

- Move view-related cleanup code out of core.
- Call `remove` on `_converse.chatboxviews` during cleanup
parent 5579e82f
...@@ -135,6 +135,11 @@ converse.plugins.add('converse-chatboxviews', { ...@@ -135,6 +135,11 @@ converse.plugins.add('converse-chatboxviews', {
/************************ BEGIN Event Handlers ************************/ /************************ BEGIN Event Handlers ************************/
api.listen.on('cleanup', () => {
_converse?.chatboxviews.remove();
delete _converse.chatboxviews;
});
api.listen.on('chatBoxesInitialized', () => { api.listen.on('chatBoxesInitialized', () => {
_converse.chatboxviews = new _converse.ChatBoxViews({ _converse.chatboxviews = new _converse.ChatBoxViews({
'model': _converse.chatboxes 'model': _converse.chatboxes
......
...@@ -554,6 +554,7 @@ converse.plugins.add('converse-controlbox', { ...@@ -554,6 +554,7 @@ converse.plugins.add('converse-controlbox', {
/******************** Event Handlers ********************/ /******************** Event Handlers ********************/
api.listen.on('cleanup', () => (delete _converse.controlboxtoggle));
api.listen.on('chatBoxViewsInitialized', () => { api.listen.on('chatBoxViewsInitialized', () => {
_converse.chatboxes.on('add', item => { _converse.chatboxes.on('add', item => {
......
...@@ -1307,18 +1307,13 @@ async function getLoginCredentialsFromBrowser () { ...@@ -1307,18 +1307,13 @@ async function getLoginCredentialsFromBrowser () {
} }
function cleanup () { // Make sure everything is reset in case this is a subsequent call to
// Make sure everything is reset in case this is a subsequent call to // converse.initialize (happens during tests).
// converse.initialize (happens during tests). async function cleanup () {
await _converse.api.trigger('cleanup', {'synchronous': true});
_converse.router.history.stop(); _converse.router.history.stop();
unregisterGlobalEventHandlers(); unregisterGlobalEventHandlers();
delete _converse.controlboxtoggle; _converse.connection?.reset();
if (_converse.chatboxviews) {
delete _converse.chatboxviews;
}
if (_converse.connection) {
_converse.connection.reset();
}
_converse.stopListening(); _converse.stopListening();
_converse.off(); _converse.off();
} }
...@@ -1528,7 +1523,7 @@ Object.assign(converse, { ...@@ -1528,7 +1523,7 @@ Object.assign(converse, {
* }); * });
*/ */
async initialize (settings) { async initialize (settings) {
cleanup(); await cleanup();
PROMISES.forEach(name => api.promises.add(name)); PROMISES.forEach(name => api.promises.add(name));
setUnloadEvent(); setUnloadEvent();
initSettings(settings); initSettings(settings);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment