diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-07-20 23:36:03 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-07-21 16:15:07 +0900 |
commit | bf7c125fb214b52d67527bdd292a4b5bb81b1d32 (patch) | |
tree | a491d7b17a6cee685f134c019a0d624d32e13a3a /src/background/infrastructures | |
parent | 4d4aaa2c4ba9f09e23882287ce868c820aa900ee (diff) |
My First Clean Architecture
Diffstat (limited to 'src/background/infrastructures')
-rw-r--r-- | src/background/infrastructures/content-message-listener.js | 34 |
1 files changed, 34 insertions, 0 deletions
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()); + } +} |