diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2020-12-10 12:52:17 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-10 12:52:17 +0000 |
commit | 5a0444d7bb7eae27fdca5c2be8fc3ec6c36d53bd (patch) | |
tree | 46d70e19f9720d237f4423c1debfcacdd088ce0b /src/background/usecases | |
parent | a3c34a309c4b1421eb4914c3fbeba327a5400021 (diff) | |
parent | d2fb674566393d9a8b88d71dba9f5081786b118c (diff) |
Merge pull request #917 from ueokande/operation-as-a-operator
refactor: Make each operation as an operator
Diffstat (limited to 'src/background/usecases')
-rw-r--r-- | src/background/usecases/AddonEnabledUseCase.ts | 2 | ||||
-rw-r--r-- | src/background/usecases/CommandUseCase.ts | 23 | ||||
-rw-r--r-- | src/background/usecases/ConsoleUseCase.ts | 63 | ||||
-rw-r--r-- | src/background/usecases/FindUseCase.ts | 15 | ||||
-rw-r--r-- | src/background/usecases/MarkUseCase.ts | 12 | ||||
-rw-r--r-- | src/background/usecases/NavigateUseCase.ts | 56 | ||||
-rw-r--r-- | src/background/usecases/RepeatUseCase.ts | 7 | ||||
-rw-r--r-- | src/background/usecases/TabSelectUseCase.ts | 51 | ||||
-rw-r--r-- | src/background/usecases/TabUseCase.ts | 102 | ||||
-rw-r--r-- | src/background/usecases/ZoomUseCase.ts | 51 |
10 files changed, 28 insertions, 354 deletions
diff --git a/src/background/usecases/AddonEnabledUseCase.ts b/src/background/usecases/AddonEnabledUseCase.ts index f563ab0..f9bafde 100644 --- a/src/background/usecases/AddonEnabledUseCase.ts +++ b/src/background/usecases/AddonEnabledUseCase.ts @@ -22,7 +22,7 @@ export default class AddonEnabledUseCase { return this.indicatorPresentor.indicate(enabled); } - onIndicatorClick(tabId: number): Promise<void> { + private onIndicatorClick(tabId: number): Promise<void> { return this.contentMessageClient.toggleAddonEnabled(tabId); } diff --git a/src/background/usecases/CommandUseCase.ts b/src/background/usecases/CommandUseCase.ts index 811ec77..18ddd4d 100644 --- a/src/background/usecases/CommandUseCase.ts +++ b/src/background/usecases/CommandUseCase.ts @@ -12,17 +12,20 @@ import ContentMessageClient from "../infrastructures/ContentMessageClient"; import RepeatUseCase from "../usecases/RepeatUseCase"; @injectable() -export default class CommandIndicator { +export default class CommandUseCase { constructor( - @inject("TabPresenter") private tabPresenter: TabPresenter, - private windowPresenter: WindowPresenter, - private helpPresenter: HelpPresenter, + @inject("TabPresenter") + private readonly tabPresenter: TabPresenter, + @inject("WindowPresenter") + private readonly windowPresenter: WindowPresenter, + private readonly helpPresenter: HelpPresenter, @inject("CachedSettingRepository") - private cachedSettingRepository: CachedSettingRepository, - private bookmarkRepository: BookmarkRepository, - private consoleClient: ConsoleClient, - private contentMessageClient: ContentMessageClient, - private repeatUseCase: RepeatUseCase + private readonly cachedSettingRepository: CachedSettingRepository, + private readonly bookmarkRepository: BookmarkRepository, + @inject("ConsoleClient") + private readonly consoleClient: ConsoleClient, + private readonly contentMessageClient: ContentMessageClient, + private readonly repeatUseCase: RepeatUseCase ) {} async open(keywords: string): Promise<browser.tabs.Tab> { @@ -44,7 +47,7 @@ export default class CommandIndicator { return this.tabPresenter.create(url); } - async winopen(keywords: string): Promise<browser.windows.Window> { + async winopen(keywords: string): Promise<void> { const url = await this.urlOrSearch(keywords); this.repeatUseCase.storeLastOperation({ type: operations.INTERNAL_OPEN_URL, diff --git a/src/background/usecases/ConsoleUseCase.ts b/src/background/usecases/ConsoleUseCase.ts deleted file mode 100644 index 195c70f..0000000 --- a/src/background/usecases/ConsoleUseCase.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { inject, injectable } from "tsyringe"; -import TabPresenter from "../presenters/TabPresenter"; -import ConsoleClient from "../infrastructures/ConsoleClient"; - -@injectable() -export default class ConsoleUseCase { - constructor( - @inject("TabPresenter") private tabPresenter: TabPresenter, - private consoleClient: ConsoleClient - ) {} - - async showCommand(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.consoleClient.showCommand(tab.id as number, ""); - } - - async showOpenCommand(alter: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - let command = "open "; - if (alter) { - command += tab.url || ""; - } - return this.consoleClient.showCommand(tab.id as number, command); - } - - async showTabopenCommand(alter: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - let command = "tabopen "; - if (alter) { - command += tab.url || ""; - } - return this.consoleClient.showCommand(tab.id as number, command); - } - - async showWinopenCommand(alter: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - let command = "winopen "; - if (alter) { - command += tab.url || ""; - } - return this.consoleClient.showCommand(tab.id as number, command); - } - - async showBufferCommand(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - const command = "buffer "; - return this.consoleClient.showCommand(tab.id as number, command); - } - - async showAddbookmarkCommand(alter: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - let command = "addbookmark "; - if (alter) { - command += tab.title || ""; - } - return this.consoleClient.showCommand(tab.id as number, command); - } - - async hideConsole(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.consoleClient.hide(tab.id as number); - } -} diff --git a/src/background/usecases/FindUseCase.ts b/src/background/usecases/FindUseCase.ts index facc461..ce96e03 100644 --- a/src/background/usecases/FindUseCase.ts +++ b/src/background/usecases/FindUseCase.ts @@ -1,15 +1,9 @@ -import { inject, injectable } from "tsyringe"; +import { injectable } from "tsyringe"; import FindRepository from "../repositories/FindRepository"; -import TabPresenter from "../presenters/TabPresenter"; -import ConsoleClient from "../infrastructures/ConsoleClient"; @injectable() export default class FindUseCase { - constructor( - @inject("TabPresenter") private tabPresenter: TabPresenter, - private findRepository: FindRepository, - private consoleClient: ConsoleClient - ) {} + constructor(private readonly findRepository: FindRepository) {} getKeyword(): Promise<string> { return this.findRepository.getKeyword(); @@ -18,9 +12,4 @@ export default class FindUseCase { setKeyword(keyword: string): Promise<any> { return this.findRepository.setKeyword(keyword); } - - async findStart(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.consoleClient.showFind(tab.id as number); - } } diff --git a/src/background/usecases/MarkUseCase.ts b/src/background/usecases/MarkUseCase.ts index 9da9a21..57e04b4 100644 --- a/src/background/usecases/MarkUseCase.ts +++ b/src/background/usecases/MarkUseCase.ts @@ -1,16 +1,18 @@ import { inject, injectable } from "tsyringe"; import TabPresenter from "../presenters/TabPresenter"; import MarkRepository from "../repositories/MarkRepository"; -import ConsoleClient from "../infrastructures/ConsoleClient"; import ContentMessageClient from "../infrastructures/ContentMessageClient"; +import ConsoleClient from "../infrastructures/ConsoleClient"; @injectable() export default class MarkUseCase { constructor( - @inject("TabPresenter") private tabPresenter: TabPresenter, - private markRepository: MarkRepository, - private consoleClient: ConsoleClient, - private contentMessageClient: ContentMessageClient + @inject("TabPresenter") + private readonly tabPresenter: TabPresenter, + private readonly markRepository: MarkRepository, + @inject("ConsoleClient") + private readonly consoleClient: ConsoleClient, + private readonly contentMessageClient: ContentMessageClient ) {} async setGlobal(key: string, x: number, y: number): Promise<any> { diff --git a/src/background/usecases/NavigateUseCase.ts b/src/background/usecases/NavigateUseCase.ts deleted file mode 100644 index 974606c..0000000 --- a/src/background/usecases/NavigateUseCase.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { inject, injectable } from "tsyringe"; -import NavigateClient from "../clients/NavigateClient"; -import TabPresenter from "../presenters/TabPresenter"; - -@injectable() -export default class NavigateUseCase { - constructor( - @inject("TabPresenter") private tabPresenter: TabPresenter, - private navigateClient: NavigateClient - ) {} - - async openHistoryNext(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - await this.navigateClient.historyNext(tab.id!); - } - - async openHistoryPrev(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - await this.navigateClient.historyPrev(tab.id!); - } - - async openLinkNext(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - await this.navigateClient.linkNext(tab.id!); - } - - async openLinkPrev(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - await this.navigateClient.linkPrev(tab.id!); - } - - async openParent(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - const url = new URL(tab.url!); - if (url.hash.length > 0) { - url.hash = ""; - } else if (url.search.length > 0) { - url.search = ""; - } else { - const basenamePattern = /\/[^/]+$/; - const lastDirPattern = /\/[^/]+\/$/; - if (basenamePattern.test(url.pathname)) { - url.pathname = url.pathname.replace(basenamePattern, "/"); - } else if (lastDirPattern.test(url.pathname)) { - url.pathname = url.pathname.replace(lastDirPattern, "/"); - } - } - await this.tabPresenter.open(url.href); - } - - async openRoot(): Promise<void> { - const tab = await this.tabPresenter.getCurrent(); - const url = new URL(tab.url!); - await this.tabPresenter.open(url.origin); - } -} diff --git a/src/background/usecases/RepeatUseCase.ts b/src/background/usecases/RepeatUseCase.ts index d7235ee..992e76b 100644 --- a/src/background/usecases/RepeatUseCase.ts +++ b/src/background/usecases/RepeatUseCase.ts @@ -1,4 +1,4 @@ -import { injectable } from "tsyringe"; +import { inject, injectable } from "tsyringe"; import * as operations from "../../shared/operations"; import RepeatRepository from "../repositories/RepeatRepository"; @@ -6,7 +6,10 @@ type Operation = operations.Operation; @injectable() export default class RepeatUseCase { - constructor(private repeatRepository: RepeatRepository) {} + constructor( + @inject("RepeatRepository") + private readonly repeatRepository: RepeatRepository + ) {} storeLastOperation(op: Operation): void { this.repeatRepository.setLastOperation(op); diff --git a/src/background/usecases/TabSelectUseCase.ts b/src/background/usecases/TabSelectUseCase.ts deleted file mode 100644 index 663ceb8..0000000 --- a/src/background/usecases/TabSelectUseCase.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { inject, injectable } from "tsyringe"; -import TabPresenter from "../presenters/TabPresenter"; - -@injectable() -export default class TabSelectUseCase { - constructor(@inject("TabPresenter") private tabPresenter: TabPresenter) {} - - async selectPrev(count: number): Promise<any> { - const tabs = await this.tabPresenter.getAll(); - if (tabs.length < 2) { - return; - } - const tab = tabs.find((t) => t.active); - if (!tab) { - return; - } - const select = (tab.index - count + tabs.length) % tabs.length; - return this.tabPresenter.select(tabs[select].id as number); - } - - async selectNext(count: number): Promise<any> { - const tabs = await this.tabPresenter.getAll(); - if (tabs.length < 2) { - return; - } - const tab = tabs.find((t) => t.active); - if (!tab) { - return; - } - const select = (tab.index + count) % tabs.length; - return this.tabPresenter.select(tabs[select].id as number); - } - - async selectFirst(): Promise<any> { - const tabs = await this.tabPresenter.getAll(); - return this.tabPresenter.select(tabs[0].id as number); - } - - async selectLast(): Promise<any> { - const tabs = await this.tabPresenter.getAll(); - return this.tabPresenter.select(tabs[tabs.length - 1].id as number); - } - - async selectPrevSelected(): Promise<any> { - const tabId = await this.tabPresenter.getLastSelectedId(); - if (tabId === null || typeof tabId === "undefined") { - return Promise.resolve(); - } - return this.tabPresenter.select(tabId); - } -} diff --git a/src/background/usecases/TabUseCase.ts b/src/background/usecases/TabUseCase.ts deleted file mode 100644 index 1439107..0000000 --- a/src/background/usecases/TabUseCase.ts +++ /dev/null @@ -1,102 +0,0 @@ -import { inject, injectable } from "tsyringe"; -import TabPresenter from "../presenters/TabPresenter"; -import WindowPresenter from "../presenters/WindowPresenter"; -import BrowserSettingRepository from "../repositories/BrowserSettingRepository"; - -@injectable() -export default class TabUseCase { - constructor( - @inject("TabPresenter") private tabPresenter: TabPresenter, - private windowPresenter: WindowPresenter, - private browserSettingRepository: BrowserSettingRepository - ) {} - - async close(force: boolean, selectLeft = false): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - if (!force && tab.pinned) { - return Promise.resolve(); - } - if (selectLeft && tab.index > 0) { - const tabs = await this.tabPresenter.getAll(); - await this.tabPresenter.select(tabs[tab.index - 1].id as number); - } - return this.tabPresenter.remove([tab.id as number]); - } - - async closeRight(): Promise<any> { - const tabs = await this.tabPresenter.getAll(); - tabs.sort((t1, t2) => t1.index - t2.index); - const index = tabs.findIndex((t) => t.active); - if (index < 0) { - return; - } - for (let i = index + 1; i < tabs.length; ++i) { - const tab = tabs[i]; - if (!tab.pinned) { - this.tabPresenter.remove([tab.id as number]); - } - } - } - - reopen(): Promise<any> { - return this.tabPresenter.reopen(); - } - - async reload(cache: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.reload(tab.id as number, cache); - } - - async setPinned(pinned: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.setPinned(tab.id as number, pinned); - } - - async togglePinned(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.setPinned(tab.id as number, !tab.pinned); - } - - async duplicate(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.duplicate(tab.id as number); - } - - async openPageSource(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - const url = "view-source:" + tab.url; - return this.tabPresenter.create(url); - } - - async openHome(newTab: boolean): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - const urls = await this.browserSettingRepository.getHomepageUrls(); - if (urls.length === 1 && urls[0] === "about:home") { - // eslint-disable-next-line max-len - throw new Error( - "Cannot open Firefox Home (about:home) by WebExtensions, set your custom URLs" - ); - } - if (urls.length === 1 && !newTab) { - return this.tabPresenter.open(urls[0], tab.id); - } - for (const url of urls) { - this.tabPresenter.create(url); - } - } - - async openURL( - url: string, - newTab?: boolean, - newWindow?: boolean - ): Promise<void> { - if (newWindow) { - await this.windowPresenter.create(url); - } else if (newTab) { - await this.tabPresenter.create(url); - } else { - const tab = await this.tabPresenter.getCurrent(); - await this.tabPresenter.open(url, tab.id); - } - } -} diff --git a/src/background/usecases/ZoomUseCase.ts b/src/background/usecases/ZoomUseCase.ts deleted file mode 100644 index 173e4d7..0000000 --- a/src/background/usecases/ZoomUseCase.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { inject, injectable } from "tsyringe"; -import TabPresenter from "../presenters/TabPresenter"; - -const ZOOM_SETTINGS: number[] = [ - 0.33, - 0.5, - 0.66, - 0.75, - 0.8, - 0.9, - 1.0, - 1.1, - 1.25, - 1.5, - 1.75, - 2.0, - 2.5, - 3.0, -]; - -@injectable() -export default class ZoomUseCase { - constructor(@inject("TabPresenter") private tabPresenter: TabPresenter) {} - - async zoomIn(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - const tabId = tab.id as number; - const current = await this.tabPresenter.getZoom(tabId); - const factor = ZOOM_SETTINGS.find((f) => f > current); - if (factor) { - return this.tabPresenter.setZoom(tabId as number, factor); - } - } - - async zoomOut(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - const tabId = tab.id as number; - const current = await this.tabPresenter.getZoom(tabId); - const factor = ZOOM_SETTINGS.slice(0) - .reverse() - .find((f) => f < current); - if (factor) { - return this.tabPresenter.setZoom(tabId as number, factor); - } - } - - async zoomNutoral(): Promise<any> { - const tab = await this.tabPresenter.getCurrent(); - return this.tabPresenter.setZoom(tab.id as number, 1); - } -} |