Open link as Clean Architecture

jh-changes
Shin'ya Ueoka 7 years ago
parent 42d902982a
commit 0846587baf
  1. 9
      src/background/components/background.js
  2. 15
      src/background/controllers/link.js
  3. 14
      src/background/infrastructures/content-message-listener.js
  4. 20
      src/background/presenters/tab.js
  5. 27
      src/background/usecases/link.js

@ -1,6 +1,5 @@
import messages from 'shared/messages';
import * as commandActions from 'background/actions/command';
import * as tabActions from 'background/actions/tab';
export default class BackgroundComponent {
constructor(store) {
@ -22,14 +21,6 @@ export default class BackgroundComponent {
let settings = this.store.getState().setting;
switch (message.type) {
case messages.OPEN_URL:
if (message.newTab) {
let action = tabActions.openNewTab(
message.url, sender.tab.id, message.background,
settings.value.properties.adjacenttab);
return this.store.dispatch(action);
}
return this.store.dispatch(tabActions.openToTab(message.url, sender.tab));
case messages.CONSOLE_ENTER_COMMAND:
this.store.dispatch(
commandActions.exec(sender.tab, message.text, settings.value),

@ -0,0 +1,15 @@
import LinkInteractor from '../usecases/link';
export default class LinkController {
constructor() {
this.linkInteractor = new LinkInteractor();
}
openToTab(url, tabId) {
this.linkInteractor.openToTab(url, tabId);
}
openNewTab(url, openerId, background) {
this.linkInteractor.openNewTab(url, openerId, background);
}
}

@ -3,6 +3,7 @@ import CompletionsController from '../controllers/completions';
import SettingController from '../controllers/setting';
import FindController from '../controllers/find';
import AddonEnabledController from '../controllers/addon-enabled';
import LinkController from '../controllers/link';
export default class ContentMessageListener {
constructor() {
@ -10,6 +11,7 @@ export default class ContentMessageListener {
this.completionsController = new CompletionsController();
this.findController = new FindController();
this.addonEnabledController = new AddonEnabledController();
this.linkController = new LinkController();
}
run() {
@ -25,7 +27,7 @@ export default class ContentMessageListener {
});
}
onMessage(message) {
onMessage(message, sender) {
switch (message.type) {
case messages.CONSOLE_QUERY_COMPLETIONS:
return this.onConsoleQueryCompletions(message.text);
@ -39,6 +41,9 @@ export default class ContentMessageListener {
return this.onFindSetKeyword(message.keyword);
case messages.ADDON_ENABLED_RESPONSE:
return this.onAddonEnabledResponse(message.enabled);
case messages.OPEN_URL:
return this.onOpenUrl(
message.newTab, message.url, sender.tab.id, message.background);
}
}
@ -66,4 +71,11 @@ export default class ContentMessageListener {
onAddonEnabledResponse(enabled) {
return this.addonEnabledController.indicate(enabled);
}
onOpenUrl(newTab, url, openerId, background) {
if (newTab) {
return this.linkController.openNewTab(url, openerId, background);
}
return this.linkController.openToTab(url, openerId);
}
}

@ -1,6 +1,22 @@
export default class TabPresenter {
create(url) {
browser.tabs.create({ url, });
open(url, tabId) {
return browser.tabs.update(tabId, { url });
}
create(url, { openerTabId, active }) {
return browser.tabs.create({ url, openerTabId, active });
}
async createAdjacent(url, { openerTabId, active }) {
let tabs = await browser.tabs.query({
active: true, currentWindow: true
});
return browser.tabs.create({
url,
openerTabId,
active,
index: tabs[0].index + 1
});
}
onSelected(listener) {

@ -0,0 +1,27 @@
import SettingRepository from '../repositories/setting';
import TabPresenter from '../presenters/tab';
export default class LinkInteractor {
constructor() {
this.settingRepository = new SettingRepository();
this.tabPresenter = new TabPresenter();
}
openToTab(url, tabId) {
this.tabPresenter.open(url, tabId);
}
async openNewTab(url, openerId, background) {
let settings = await this.settingRepository.get();
let { adjacenttab } = settings.properties;
if (adjacenttab) {
return this.tabPresenter.create(url, {
openerTabId: openerId, active: !background
});
}
return this.tabPresenter.create(url, {
openerTabId: openerId, active: !background
});
}
}