From bf7c125fb214b52d67527bdd292a4b5bb81b1d32 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 20 Jul 2018 23:36:03 +0900 Subject: My First Clean Architecture --- .../infrastructures/content-message-listener.js | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/background/infrastructures/content-message-listener.js (limited to 'src/background/infrastructures') diff --git a/src/background/infrastructures/content-message-listener.js b/src/background/infrastructures/content-message-listener.js new file mode 100644 index 0000000..a0ed66c --- /dev/null +++ b/src/background/infrastructures/content-message-listener.js @@ -0,0 +1,34 @@ +import messages from '../../shared/messages'; +import CompletionsController from '../controllers/completions'; + +export default class ContentMessageListener { + constructor() { + this.completionsController = new CompletionsController(); + } + + run() { + browser.runtime.onMessage.addListener((message, sender) => { + try { + return this.onMessage(message, sender); + } catch (e) { + return browser.tabs.sendMessage(sender.tab.id, { + type: messages.CONSOLE_SHOW_ERROR, + text: e.message, + }); + } + }); + } + + onMessage(message) { + switch (message.type) { + case messages.CONSOLE_QUERY_COMPLETIONS: + return this.onConsoleQueryCompletions(message); + } + } + + async onConsoleQueryCompletions(message) { + let completions = + await this.completionsController.getCompletions(message.text); + return Promise.resolve(completions.serialize()); + } +} -- cgit v1.2.3