aboutsummaryrefslogtreecommitdiff
path: root/src/background/infrastructures
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2018-07-20 23:36:03 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2018-07-21 16:15:07 +0900
commitbf7c125fb214b52d67527bdd292a4b5bb81b1d32 (patch)
treea491d7b17a6cee685f134c019a0d624d32e13a3a /src/background/infrastructures
parent4d4aaa2c4ba9f09e23882287ce868c820aa900ee (diff)
My First Clean Architecture
Diffstat (limited to 'src/background/infrastructures')
-rw-r--r--src/background/infrastructures/content-message-listener.js34
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());
+ }
+}