Skip to content

Commit

Permalink
Use svelte store to change states
Browse files Browse the repository at this point in the history
  • Loading branch information
hao1300 committed May 30, 2020
1 parent ce9fda7 commit 69f265f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 22 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "eslint:recommended",
"parser": "babel-eslint",
"parserOptions": {
"sourceType": "module"
"rules": {
"no-undef": 0
}
}
43 changes: 29 additions & 14 deletions src/js/commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import annyang from "./annyang";
import { DEBUG, storage } from "./common";
import NotificationManager from "./notification";
import { updateBrowserAction } from "./browser_actions";
import { activeListening } from './store';

function getHost(url) {
return new URL(url).host;
Expand All @@ -22,16 +23,24 @@ class Commander {
annyang.addGrammars(grammars);
}

this.notificationManager_ = new NotificationManager(this);
this.notificationManager_ = new NotificationManager();
this.allPlugins_ = allPlugins;
this.lastCommand_ = "";
this.commandPriorities_ = {};

chrome.tabs.onActivated.addListener(activeInfo => {
activeListening.subscribe(value => {
if (value) {
this.startListeningToAllCommands();
} else {
this.clearNotifications();
}
});

chrome.tabs.onActivated.addListener(() => {
this.notificationManager_.resendMessageIfAvailable();
});

chrome.runtime.onMessage.addListener(async (request, sender) => {
chrome.runtime.onMessage.addListener(async (request) => {
if (DEBUG) {
console.log(`Received message: ${JSON.stringify(request)}`);
}
Expand All @@ -48,7 +57,7 @@ class Commander {
annyang.trigger(request.query);
break;
case "CLEAR_NOTIFICATION":
this.clearNotifications();
activeListening.set(false);
break;
case "TAB_LOADED":
this.notificationManager_.resendMessageIfAvailable();
Expand Down Expand Up @@ -101,15 +110,21 @@ class Commander {
}

clearNotifications() {
chrome.tabs.query({ muted: true }, (tabs) => {
for (const tab of tabs) {
if (tab.mutedInfo.extensionId === chrome.runtime.id) {
chrome.tabs.update(tab.id, { muted: false });
}
}
});
this.startListeningToTriggerCommands();
this.notificationManager_.clearMessage();
}

startListeningToAllCommands() {
this.notificationManager_.sendMessage({
type: "START_LISTENING",
title: "Listening",
content: "Hi, how can I help you?"
chrome.tabs.query({ audible: true }, (tabs) => {
for (const tab of tabs) {
chrome.tabs.update(tab.id, { muted: true });
}
});
updateBrowserAction(true);
this.performActionWithDelay(() => {
Expand Down Expand Up @@ -243,11 +258,11 @@ class Commander {
this.getActiveTab(activeTab => {
if (!activeTab.url || activeTab.url.startsWith("chrome")) {
// We can't use content script on chrome URLs, so need to create a new tab.
chrome.tabs.create({ url: "https://www.google.com" }, () =>
this.startListeningToAllCommands()
);
chrome.tabs.create({ url: "https://www.google.com" }, () => {
activeListening.set(true);
});
} else {
this.startListeningToAllCommands();
activeListening.set(true);
}
});
}
Expand Down Expand Up @@ -330,7 +345,7 @@ class Commander {
this.performActionWithDelay(() => {
chrome.storage.local.get(["autoOff"], result => {
if (result.autoOff) {
commander.clearNotifications();
activeListening.set(false);
}
});
})
Expand Down
17 changes: 14 additions & 3 deletions src/js/notification.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { DEBUG } from "./common";
import { updateBrowserAction } from "./browser_actions";
import { activeListening } from './store';

const NOTIFICATION_TIMEOUT = 15000;

export default class NotificationManager {
constructor(commander) {
constructor() {
this.lastData_ = undefined;
this.commander_ = commander;
activeListening.subscribe(value => {
if (value) {
this.sendMessage({
type: "START_LISTENING",
title: "Listening",
content: "Hi, how can I help you?"
});
} else {
this.clearMessage();
}
});
}

hasMessage() {
Expand All @@ -26,7 +37,7 @@ export default class NotificationManager {
await this.innerSendMessage(request);
setTimeout(() => {
if (this.lastData_ === request) {
this.commander_.clearNotifications();
activeListening.set(false);
}
}, NOTIFICATION_TIMEOUT);
}
Expand Down
3 changes: 2 additions & 1 deletion src/js/plugins/extension.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import commander from "../commander";
import { activeListening } from '../store';

const commands = [
{
commands: ["bye", "bye bye", "goodbye", "good bye", "close"],
callback: () => {
commander.clearNotifications();
activeListening.set(false);
}
},
{
Expand Down
5 changes: 3 additions & 2 deletions src/js/plugins/query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import commander from "../commander";
import cheerio from "cheerio";
import axios from "axios";
import commander from "../commander";
import { activeListening } from '../store';

/** ------- Search query ------- */
const prependQueryPhrase = queries => {
Expand Down Expand Up @@ -272,7 +273,7 @@ const commands = [
"https://www.google.com/search?gs_ivs=1&q=" +
encodeURIComponent(query)
);
commander.clearNotifications();
activeListening.set(false);
} else {
commander.openTabWithUrl(
"https://www.google.com/search?q=" +
Expand Down
3 changes: 3 additions & 0 deletions src/js/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { writable } from 'svelte/store';

export const activeListening = writable(false);

0 comments on commit 69f265f

Please sign in to comment.