From d9dd09c69b94e3a642670c06bfd2417d9f4f4c4a Mon Sep 17 00:00:00 2001
From: Jonas Dellinger <jonas@dellinger.dev>
Date: Mon, 20 Jun 2022 15:28:30 +0200
Subject: Revert "fix onDismount"

This reverts commit daca482ed853c1b74649c6579b1094d8a8c27141.
---
 frontend/src/plugin-loader.tsx | 50 ++++++++++++++++++++++++++++++++----------
 1 file changed, 39 insertions(+), 11 deletions(-)

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

diff --git a/frontend/src/plugin-loader.tsx b/frontend/src/plugin-loader.tsx
index 6d0d24c6..eb3344a3 100644
--- a/frontend/src/plugin-loader.tsx
+++ b/frontend/src/plugin-loader.tsx
@@ -4,6 +4,7 @@ import { FaPlug } from 'react-icons/fa';
 import { DeckyState, DeckyStateContextProvider } from './components/DeckyState';
 import LegacyPlugin from './components/LegacyPlugin';
 import PluginView from './components/PluginView';
+import StorePage from './components/store/Store';
 import TitleView from './components/TitleView';
 import Logger from './logger';
 import { Plugin } from './plugin';
@@ -21,6 +22,10 @@ class PluginLoader extends Logger {
   private routerHook: RouterHook = new RouterHook();
   private deckyState: DeckyState = new DeckyState();
 
+  private reloadLock: boolean = false;
+  // stores a list of plugin names which requested to be reloaded
+  private pluginReloadQueue: string[] = [];
+
   constructor() {
     super(PluginLoader.name);
     this.log('Initialized');
@@ -39,6 +44,8 @@ class PluginLoader extends Logger {
       ),
       icon: <FaPlug />,
     });
+
+    this.routerHook.addRoute('/decky/store', () => <StorePage />);
   }
 
   public addPluginInstallPrompt(artifact: string, version: string, request_id: string) {
@@ -67,18 +74,38 @@ class PluginLoader extends Logger {
     }
   }
 
+  public deinit() {
+    this.routerHook.removeRoute('/decky/store');
+  }
+
   public async importPlugin(name: string) {
-    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);
-    if (name.startsWith('$LEGACY_')) {
-      await this.importLegacyPlugin(name.replace('$LEGACY_', ''));
-    } else {
-      await this.importReactPlugin(name);
+    try {
+      if (this.reloadLock) {
+        this.log('Reload currently in progress, adding to queue', name);
+        this.pluginReloadQueue.push(name);
+        return;
+      }
+
+      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);
+      if (name.startsWith('$LEGACY_')) {
+        await this.importLegacyPlugin(name.replace('$LEGACY_', ''));
+      } else {
+        await this.importReactPlugin(name);
+      }
+      this.log(`Loaded ${name}`);
+
+      this.deckyState.setPlugins(this.plugins);
+    } catch (e) {
+      throw e;
+    } finally {
+      this.reloadLock = false;
+      const nextPlugin = this.pluginReloadQueue.shift();
+      if (nextPlugin) {
+        this.importPlugin(nextPlugin);
+      }
     }
-    this.log(`Loaded ${name}`);
-
-    this.deckyState.setPlugins(this.plugins);
   }
 
   private async importReactPlugin(name: string) {
@@ -87,7 +114,8 @@ class PluginLoader extends Logger {
       let content = await eval(await res.text())(this.createPluginAPI(name));
       this.plugins.push({
         name: name,
-        ...content,
+        icon: content.icon,
+        content: content.content,
       });
     } else throw new Error(`${name} frontend_bundle not OK`);
   }
-- 
cgit v1.2.3

