Merge pull request #210 from toasted-nutbread/defer-port-creation
Defer creation of communication port until required
This commit is contained in:
commit
2add068ff2
@ -26,9 +26,7 @@ class FrontendApiSender {
|
|||||||
this.disconnected = false;
|
this.disconnected = false;
|
||||||
this.nextId = 0;
|
this.nextId = 0;
|
||||||
|
|
||||||
this.port = chrome.runtime.connect(null, {name: 'backend-api-forwarder'});
|
this.port = null;
|
||||||
this.port.onDisconnect.addListener(this.onDisconnect.bind(this));
|
|
||||||
this.port.onMessage.addListener(this.onMessage.bind(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
invoke(action, params, target) {
|
invoke(action, params, target) {
|
||||||
@ -36,6 +34,10 @@ class FrontendApiSender {
|
|||||||
return Promise.reject('Disconnected');
|
return Promise.reject('Disconnected');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.port === null) {
|
||||||
|
this.createPort();
|
||||||
|
}
|
||||||
|
|
||||||
const id = `${this.nextId}`;
|
const id = `${this.nextId}`;
|
||||||
++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}) {
|
onMessage({type, id, data, senderId}) {
|
||||||
if (senderId !== this.senderId) { return; }
|
if (senderId !== this.senderId) { return; }
|
||||||
switch (type) {
|
switch (type) {
|
||||||
|
Loading…
Reference in New Issue
Block a user