From a95bf94d878f61869895bb22cbff1b4f524c5dca Mon Sep 17 00:00:00 2001
From: Jonas Dellinger <jonas.dellinger@2trde.com>
Date: Mon, 13 Jun 2022 10:57:16 +0200
Subject: fix(loader): multiprocessing.set_start_method once, queue for plugin
 import

---
 frontend/src/plugin-loader.tsx | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

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

diff --git a/frontend/src/plugin-loader.tsx b/frontend/src/plugin-loader.tsx
index bdefc0b6..73f65415 100644
--- a/frontend/src/plugin-loader.tsx
+++ b/frontend/src/plugin-loader.tsx
@@ -21,6 +21,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');
@@ -68,17 +72,33 @@ class PluginLoader extends Logger {
   }
 
   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) {
-- 
cgit v1.2.3

