aboutsummaryrefslogtreecommitdiff
path: root/src/background/infrastructures/content-message-listener.js
blob: a0ed66c9dd416a95dd660c05fea70430e03eaebd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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());
  }
}