From 304064dae00593856c26812ea30d3d34e33ec7bc Mon Sep 17 00:00:00 2001
From: toasted-nutbread <toasted-nutbread@users.noreply.github.com>
Date: Sat, 14 Sep 2019 11:58:22 -0400
Subject: Defer creation of communication port until required

---
 ext/fg/js/frontend-api-sender.js | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

(limited to 'ext/fg/js')

diff --git a/ext/fg/js/frontend-api-sender.js b/ext/fg/js/frontend-api-sender.js
index a1cb02c4..2e037e62 100644
--- a/ext/fg/js/frontend-api-sender.js
+++ b/ext/fg/js/frontend-api-sender.js
@@ -26,9 +26,7 @@ class FrontendApiSender {
         this.disconnected = false;
         this.nextId = 0;
 
-        this.port = chrome.runtime.connect(null, {name: 'backend-api-forwarder'});
-        this.port.onDisconnect.addListener(this.onDisconnect.bind(this));
-        this.port.onMessage.addListener(this.onMessage.bind(this));
+        this.port = null;
     }
 
     invoke(action, params, target) {
@@ -36,6 +34,10 @@ class FrontendApiSender {
             return Promise.reject('Disconnected');
         }
 
+        if (this.port === null) {
+            this.createPort();
+        }
+
         const id = `${this.nextId}`;
         ++this.nextId;
 
@@ -48,6 +50,12 @@ class FrontendApiSender {
         });
     }
 
+    createPort() {
+        this.port = chrome.runtime.connect(null, {name: 'backend-api-forwarder'});
+        this.port.onDisconnect.addListener(this.onDisconnect.bind(this));
+        this.port.onMessage.addListener(this.onMessage.bind(this));
+    }
+
     onMessage({type, id, data, senderId}) {
         if (senderId !== this.senderId) { return; }
         switch (type) {
-- 
cgit v1.2.3