From 711af3bca352923901c48a4ceb7d9506545ec622 Mon Sep 17 00:00:00 2001
From: Jonas Dellinger <jonas@dellinger.dev>
Date: Mon, 20 Jun 2022 15:34:08 +0200
Subject: Fix onDismount

---
 frontend/src/plugin-loader.tsx | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

(limited to 'frontend/src/plugin-loader.tsx')

diff --git a/frontend/src/plugin-loader.tsx b/frontend/src/plugin-loader.tsx
index c42065c7..b8729168 100644
--- a/frontend/src/plugin-loader.tsx
+++ b/frontend/src/plugin-loader.tsx
@@ -87,16 +87,21 @@ class PluginLoader extends Logger {
       }
 
       this.log(`Trying to load ${name}`);
-      let find = this.plugins.find((x) => x.name == name);
-      if (find) this.plugins.splice(this.plugins.indexOf(find), 1);
+
+      const oldPlugin = this.plugins.find((plugin) => plugin.name === name);
+      if (oldPlugin) {
+        oldPlugin.onDismount?.();
+        this.plugins = this.plugins.filter((plugin) => plugin !== oldPlugin);
+      }
+
       if (name.startsWith('$LEGACY_')) {
         await this.importLegacyPlugin(name.replace('$LEGACY_', ''));
       } else {
         await this.importReactPlugin(name);
       }
-      this.log(`Loaded ${name}`);
 
       this.deckyState.setPlugins(this.plugins);
+      this.log(`Loaded ${name}`);
     } catch (e) {
       throw e;
     } finally {
-- 
cgit v1.2.3

