Update frontend API receiver handlers

This commit is contained in:
toasted-nutbread 2019-12-11 21:59:38 -05:00
parent b7144ed879
commit 573f83b65a
2 changed files with 18 additions and 21 deletions

View File

@ -18,9 +18,9 @@
class FrontendApiReceiver { class FrontendApiReceiver {
constructor(source='', handlers={}) { constructor(source='', handlers=new Map()) {
this.source = source; this._source = source;
this.handlers = handlers; this._handlers = handlers;
chrome.runtime.onConnect.addListener(this.onConnect.bind(this)); chrome.runtime.onConnect.addListener(this.onConnect.bind(this));
} }
@ -32,16 +32,13 @@ class FrontendApiReceiver {
} }
onMessage(port, {id, action, params, target, senderId}) { onMessage(port, {id, action, params, target, senderId}) {
if ( if (target !== this._source) { return; }
target !== this.source ||
!hasOwn(this.handlers, action) const handler = this._handlers.get(action);
) { if (typeof handler !== 'function') { return; }
return;
}
this.sendAck(port, id, senderId); this.sendAck(port, id, senderId);
const handler = this.handlers[action];
handler(params).then( handler(params).then(
(result) => { (result) => {
this.sendResult(port, id, senderId, {result}); this.sendResult(port, id, senderId, {result});

View File

@ -36,17 +36,17 @@ class PopupProxyHost {
const {frameId} = await this.frameIdPromise; const {frameId} = await this.frameIdPromise;
if (typeof frameId !== 'number') { return; } if (typeof frameId !== 'number') { return; }
this.apiReceiver = new FrontendApiReceiver(`popup-proxy-host#${frameId}`, { this.apiReceiver = new FrontendApiReceiver(`popup-proxy-host#${frameId}`, new Map([
createNestedPopup: ({parentId}) => this.createNestedPopup(parentId), ['createNestedPopup', ({parentId}) => this.createNestedPopup(parentId)],
setOptions: ({id, options}) => this.setOptions(id, options), ['setOptions', ({id, options}) => this.setOptions(id, options)],
hide: ({id, changeFocus}) => this.hide(id, changeFocus), ['hide', ({id, changeFocus}) => this.hide(id, changeFocus)],
isVisibleAsync: ({id}) => this.isVisibleAsync(id), ['isVisibleAsync', ({id}) => this.isVisibleAsync(id)],
setVisibleOverride: ({id, visible}) => this.setVisibleOverride(id, visible), ['setVisibleOverride', ({id, visible}) => this.setVisibleOverride(id, visible)],
containsPoint: ({id, x, y}) => this.containsPoint(id, x, y), ['containsPoint', ({id, x, y}) => this.containsPoint(id, x, y)],
showContent: ({id, elementRect, writingMode, type, details}) => this.showContent(id, elementRect, writingMode, type, details), ['showContent', ({id, elementRect, writingMode, type, details}) => this.showContent(id, elementRect, writingMode, type, details)],
setCustomCss: ({id, css}) => this.setCustomCss(id, css), ['setCustomCss', ({id, css}) => this.setCustomCss(id, css)],
clearAutoPlayTimer: ({id}) => this.clearAutoPlayTimer(id) ['clearAutoPlayTimer', ({id}) => this.clearAutoPlayTimer(id)]
}); ]));
} }
createPopup(parentId, depth) { createPopup(parentId, depth) {