From fe9180b789626488ff552d639e747028822c0205 Mon Sep 17 00:00:00 2001
From: "Mr. Stallion" <mrstallion@nobody.nowhere.fauxdomain.ext>
Date: Wed, 20 Jan 2021 15:26:15 -0600
Subject: [PATCH] Fixed logout/re-login leading to multiple event listeners

---
 chat/Chat.vue                     | 4 ++++
 chat/preview/event-bus.ts         | 5 +++++
 chat/preview/image-dom-mutator.ts | 2 +-
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/chat/Chat.vue b/chat/Chat.vue
index b634625..57079c3 100644
--- a/chat/Chat.vue
+++ b/chat/Chat.vue
@@ -45,6 +45,7 @@
     import Logs from './Logs.vue';
     import {init as profileApiInit} from './profile_api';
     import { AdManager } from './ads/ad-manager';
+    import { EventBus } from './preview/event-bus';
 
     type BBCodeNode = Node & {bbcodeTag?: string, bbcodeParam?: string};
 
@@ -139,9 +140,11 @@
 
                 AdManager.onConnectionClosed();
                 core.adCoordinator.clear();
+                EventBus.clear();
 
                 // tslint:disable-next-line:no-floating-promises
                 core.siteSession.onConnectionClosed();
+                core.cache.stop();
 
                 document.title = l('title');
             });
@@ -178,6 +181,7 @@
 
                 // tslint:disable-next-line:no-floating-promises
                 core.siteSession.onConnectionEstablished();
+                core.cache.start((core.state as any).generalSettings, true);
             });
             core.watch(() => core.conversations.hasNew, (hasNew) => {
                 document.title = (hasNew ? '💬 ' : '') + l(core.connection.isOpen ? 'title.connected' : 'title', core.connection.character);
diff --git a/chat/preview/event-bus.ts b/chat/preview/event-bus.ts
index 2d651b4..e3bb42e 100644
--- a/chat/preview/event-bus.ts
+++ b/chat/preview/event-bus.ts
@@ -77,6 +77,11 @@ class EventBusManager {
     $emit(eventName: string, eventData: EventBusEvent): void {
         _.each(this.eventCallbacks[eventName] || [], (cb) => (cb(eventData)));
     }
+
+
+    clear(): void {
+        this.eventCallbacks = {};
+    }
 }
 
 export const EventBus = new EventBusManager();
diff --git a/chat/preview/image-dom-mutator.ts b/chat/preview/image-dom-mutator.ts
index 1e8617f..4fbc263 100644
--- a/chat/preview/image-dom-mutator.ts
+++ b/chat/preview/image-dom-mutator.ts
@@ -64,7 +64,7 @@ export class ImageDomMutator {
             mutator = this.hostMutators['default'];
         }
 
-        if (mutator.eventName !== eventName)
+        if ((!mutator) || (mutator.eventName !== eventName))
             return;
 
         // console.log(`Mutator match: ${mutator.match}`, (mutator === this.hostMutators['default']), url);