diff options
| author | Alex Yatskov <FooSoft@users.noreply.github.com> | 2019-09-14 17:30:52 -0700 | 
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-09-14 17:30:52 -0700 | 
| commit | 2add068ff24f679f3a2ddabc90ed57b6e0b815aa (patch) | |
| tree | 769fb3e7f7341c2c9a4c5d3fa920ef0f97a82230 | |
| parent | ab2db771adc9f3d45dfe537299f473b391fc85e9 (diff) | |
| parent | 304064dae00593856c26812ea30d3d34e33ec7bc (diff) | |
Merge pull request #210 from toasted-nutbread/defer-port-creation
Defer creation of communication port until required
| -rw-r--r-- | ext/fg/js/frontend-api-sender.js | 14 | 
1 files changed, 11 insertions, 3 deletions
| 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) { |