diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-20 22:21:31 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-21 20:30:37 +0900 |
commit | 1666e626b0d90a53399bf2e79e47b486dbca795c (patch) | |
tree | dc49a80f02bdec71639e5389c25e96a14835c856 | |
parent | 71f3a1ea2d3d648db9f628bb1deddc41263ab438 (diff) |
Use TSyringe on content script
35 files changed, 399 insertions, 434 deletions
diff --git a/src/content/Application.ts b/src/content/Application.ts new file mode 100644 index 0000000..470bf53 --- /dev/null +++ b/src/content/Application.ts @@ -0,0 +1,111 @@ +import { injectable } from 'tsyringe'; +import MessageListener from './MessageListener'; +import FindController from './controllers/FindController'; +import MarkController from './controllers/MarkController'; +import FollowMasterController from './controllers/FollowMasterController'; +import FollowSlaveController from './controllers/FollowSlaveController'; +import FollowKeyController from './controllers/FollowKeyController'; +import InputDriver from './InputDriver'; +import KeymapController from './controllers/KeymapController'; +import AddonEnabledUseCase from './usecases/AddonEnabledUseCase'; +import MarkKeyController from './controllers/MarkKeyController'; +import AddonEnabledController from './controllers/AddonEnabledController'; +import SettingController from './controllers/SettingController'; +import ConsoleFrameController from './controllers/ConsoleFrameController'; +import * as messages from '../shared/messages'; + +type Message = messages.Message; + +@injectable() +export default class Application { + + // eslint-disable-next-line max-params + constructor( + private messageListener: MessageListener, + private findController: FindController, + private markController: MarkController, + private followMasterController: FollowMasterController, + private followSlaveController: FollowSlaveController, + private followKeyController: FollowKeyController, + private keymapController: KeymapController, + private addonEnabledUseCase: AddonEnabledUseCase, + private markKeyController: MarkKeyController, + private addonEnabledController: AddonEnabledController, + private settingController: SettingController, + private consoleFrameController: ConsoleFrameController, + ) { + } + + run() { + this.routeCommonComponents(); + if (window.self === window.top) { + this.routeMasterComponents(); + } + } + + private routeMasterComponents() { + this.messageListener.onWebMessage((msg: Message, sender: Window) => { + switch (msg.type) { + case messages.CONSOLE_ENTER_FIND: + return this.findController.start(msg); + case messages.FIND_NEXT: + return this.findController.next(msg); + case messages.FIND_PREV: + return this.findController.prev(msg); + case messages.CONSOLE_UNFOCUS: + return this.consoleFrameController.unfocus(msg); + case messages.FOLLOW_START: + return this.followMasterController.followStart(msg); + case messages.FOLLOW_RESPONSE_COUNT_TARGETS: + return this.followMasterController.responseCountTargets(msg, sender); + case messages.FOLLOW_KEY_PRESS: + return this.followMasterController.keyPress(msg); + } + return undefined; + }); + + this.messageListener.onBackgroundMessage((msg: Message) => { + switch (msg.type) { + case messages.ADDON_ENABLED_QUERY: + return this.addonEnabledController.getAddonEnabled(msg); + case messages.TAB_SCROLL_TO: + return this.markController.scrollTo(msg); + } + return undefined; + }); + } + + private routeCommonComponents() { + this.messageListener.onWebMessage((msg: Message) => { + switch (msg.type) { + case messages.FOLLOW_REQUEST_COUNT_TARGETS: + return this.followSlaveController.countTargets(msg); + case messages.FOLLOW_CREATE_HINTS: + return this.followSlaveController.createHints(msg); + case messages.FOLLOW_SHOW_HINTS: + return this.followSlaveController.showHints(msg); + case messages.FOLLOW_ACTIVATE: + return this.followSlaveController.activate(msg); + case messages.FOLLOW_REMOVE_HINTS: + return this.followSlaveController.clear(msg); + } + return undefined; + }); + + this.messageListener.onBackgroundMessage((msg: Message): any => { + switch (msg.type) { + case messages.SETTINGS_CHANGED: + return this.settingController.reloadSettings(msg); + case messages.ADDON_TOGGLE_ENABLED: + return this.addonEnabledUseCase.toggle(); + } + }); + + let inputDriver = new InputDriver(window.document.body); + inputDriver.onKey(key => this.followKeyController.press(key)); + inputDriver.onKey(key => this.markKeyController.press(key)); + inputDriver.onKey(key => this.keymapController.press(key)); + + this.settingController.initSettings(); + } +} diff --git a/src/content/MessageListener.ts b/src/content/MessageListener.ts index e545cab..d035825 100644 --- a/src/content/MessageListener.ts +++ b/src/content/MessageListener.ts @@ -1,7 +1,9 @@ +import { injectable } from 'tsyringe'; import { Message, valueOf } from '../shared/messages'; export type WebExtMessageSender = browser.runtime.MessageSender; +@injectable() export default class MessageListener { onWebMessage( listener: (msg: Message, sender: Window) => void, diff --git a/src/content/client/BackgroundClient.ts b/src/content/client/BackgroundClient.ts index 2fe8d01..4a41184 100644 --- a/src/content/client/BackgroundClient.ts +++ b/src/content/client/BackgroundClient.ts @@ -1,6 +1,8 @@ +import { injectable } from 'tsyringe'; import * as operations from '../../shared/operations'; import * as messages from '../../shared/messages'; +@injectable() export default class BackgroundClient { execBackgroundOp(op: operations.Operation): Promise<void> { return browser.runtime.sendMessage({ diff --git a/src/content/controllers/AddonEnabledController.ts b/src/content/controllers/AddonEnabledController.ts index 4e19b6a..bae95bf 100644 --- a/src/content/controllers/AddonEnabledController.ts +++ b/src/content/controllers/AddonEnabledController.ts @@ -1,13 +1,12 @@ +import { injectable } from 'tsyringe'; import * as messages from '../../shared/messages'; import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase'; +@injectable() export default class AddonEnabledController { - private addonEnabledUseCase: AddonEnabledUseCase; - - constructor({ - addonEnabledUseCase = new AddonEnabledUseCase(), - } = {}) { - this.addonEnabledUseCase = addonEnabledUseCase; + constructor( + private addonEnabledUseCase: AddonEnabledUseCase, + ) { } getAddonEnabled( diff --git a/src/content/controllers/ConsoleFrameController.ts b/src/content/controllers/ConsoleFrameController.ts index fafadf4..ae26b7c 100644 --- a/src/content/controllers/ConsoleFrameController.ts +++ b/src/content/controllers/ConsoleFrameController.ts @@ -1,13 +1,12 @@ +import { injectable } from 'tsyringe'; import ConsoleFrameUseCase from '../usecases/ConsoleFrameUseCase'; import * as messages from '../../shared/messages'; +@injectable() export default class ConsoleFrameController { - private consoleFrameUseCase: ConsoleFrameUseCase; - - constructor({ - consoleFrameUseCase = new ConsoleFrameUseCase(), - } = {}) { - this.consoleFrameUseCase = consoleFrameUseCase; + constructor( + private consoleFrameUseCase: ConsoleFrameUseCase, + ) { } unfocus(_message: messages.Message) { diff --git a/src/content/controllers/FindController.ts b/src/content/controllers/FindController.ts index cf27a8d..7735176 100644 --- a/src/content/controllers/FindController.ts +++ b/src/content/controllers/FindController.ts @@ -1,13 +1,12 @@ +import { injectable } from 'tsyringe'; import * as messages from '../../shared/messages'; import FindUseCase from '../usecases/FindUseCase'; +@injectable() export default class FindController { - private findUseCase: FindUseCase; - - constructor({ - findUseCase = new FindUseCase(), - } = {}) { - this.findUseCase = findUseCase; + constructor( + private findUseCase: FindUseCase, + ) { } async start(m: messages.ConsoleEnterFindMessage): Promise<void> { diff --git a/src/content/controllers/FollowKeyController.ts b/src/content/controllers/FollowKeyController.ts index eb45e01..59d2271 100644 --- a/src/content/controllers/FollowKeyController.ts +++ b/src/content/controllers/FollowKeyController.ts @@ -1,13 +1,12 @@ +import { injectable } from 'tsyringe'; import FollowSlaveUseCase from '../usecases/FollowSlaveUseCase'; import Key from '../domains/Key'; +@injectable() export default class FollowKeyController { - private followSlaveUseCase: FollowSlaveUseCase; - - constructor({ - followSlaveUseCase = new FollowSlaveUseCase(), - } = {}) { - this.followSlaveUseCase = followSlaveUseCase; + constructor( + private followSlaveUseCase: FollowSlaveUseCase, + ) { } press(key: Key): boolean { diff --git a/src/content/controllers/FollowMasterController.ts b/src/content/controllers/FollowMasterController.ts index 89294ff..fa99820 100644 --- a/src/content/controllers/FollowMasterController.ts +++ b/src/content/controllers/FollowMasterController.ts @@ -1,13 +1,12 @@ +import { injectable } from 'tsyringe'; import FollowMasterUseCase from '../usecases/FollowMasterUseCase'; import * as messages from '../../shared/messages'; +@injectable() export default class FollowMasterController { - private followMasterUseCase: FollowMasterUseCase; - - constructor({ - followMasterUseCase = new FollowMasterUseCase(), - } = {}) { - this.followMasterUseCase = followMasterUseCase; + constructor( + private followMasterUseCase: FollowMasterUseCase, + ) { } followStart(m: messages.FollowStartMessage): void { diff --git a/src/content/controllers/FollowSlaveController.ts b/src/content/controllers/FollowSlaveController.ts index 88dccf3..65ce477 100644 --- a/src/content/controllers/FollowSlaveController.ts +++ b/src/content/controllers/FollowSlaveController.ts @@ -1,13 +1,12 @@ +import { injectable } from 'tsyringe'; import * as messages from '../../shared/messages'; import FollowSlaveUseCase from '../usecases/FollowSlaveUseCase'; +@injectable() export default class FollowSlaveController { - private usecase: FollowSlaveUseCase; - - constructor({ - usecase = new FollowSlaveUseCase(), - } = {}) { - this.usecase = usecase; + constructor( + private usecase: FollowSlaveUseCase, + ) { } countTargets(m: messages.FollowRequestCountTargetsMessage): void { diff --git a/src/content/controllers/KeymapController.ts b/src/content/controllers/KeymapController.ts index 20c24c0..1835546 100644 --- a/src/content/controllers/KeymapController.ts +++ b/src/content/controllers/KeymapController.ts @@ -1,3 +1,4 @@ +import { injectable, inject } from 'tsyringe'; import * as operations from '../../shared/operations'; import KeymapUseCase from '../usecases/KeymapUseCase'; import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase'; @@ -8,53 +9,25 @@ import FocusUseCase from '../usecases/FocusUseCase'; import ClipboardUseCase from '../usecases/ClipboardUseCase'; import BackgroundClient from '../client/BackgroundClient'; import MarkKeyyUseCase from '../usecases/MarkKeyUseCase'; -import FollowMasterClient, { FollowMasterClientImpl } - from '../client/FollowMasterClient'; +import FollowMasterClient from '../client/FollowMasterClient'; import Key from '../domains/Key'; +@injectable() export default class KeymapController { - private keymapUseCase: KeymapUseCase; + constructor( + private keymapUseCase: KeymapUseCase, + private addonEnabledUseCase: AddonEnabledUseCase, + private findSlaveUseCase: FindSlaveUseCase, + private scrollUseCase: ScrollUseCase, + private navigateUseCase: NavigateUseCase, + private focusUseCase: FocusUseCase, + private clipbaordUseCase: ClipboardUseCase, + private backgroundClient: BackgroundClient, + private markKeyUseCase: MarkKeyyUseCase, - private addonEnabledUseCase: AddonEnabledUseCase; - - private findSlaveUseCase: FindSlaveUseCase; - - private scrollUseCase: ScrollUseCase; - - private navigateUseCase: NavigateUseCase; - - private focusUseCase: FocusUseCase; - - private clipbaordUseCase: ClipboardUseCase; - - private backgroundClient: BackgroundClient; - - private markKeyUseCase: MarkKeyyUseCase; - - private followMasterClient: FollowMasterClient; - - constructor({ - keymapUseCase = new KeymapUseCase(), - addonEnabledUseCase = new AddonEnabledUseCase(), - findSlaveUseCase = new FindSlaveUseCase(), - scrollUseCase = new ScrollUseCase(), - navigateUseCase = new NavigateUseCase(), - focusUseCase = new FocusUseCase(), - clipbaordUseCase = new ClipboardUseCase(), - backgroundClient = new BackgroundClient(), - markKeyUseCase = new MarkKeyyUseCase(), - followMasterClient = new FollowMasterClientImpl(window.top), - } = {}) { - this.keymapUseCase = keymapUseCase; - this.addonEnabledUseCase = addonEnabledUseCase; - this.findSlaveUseCase = findSlaveUseCase; - this.scrollUseCase = scrollUseCase; - this.navigateUseCase = navigateUseCase; - this.focusUseCase = focusUseCase; - this.clipbaordUseCase = clipbaordUseCase; - this.backgroundClient = backgroundClient; - this.markKeyUseCase = markKeyUseCase; - this.followMasterClient = followMasterClient; + @inject('FollowMasterClient') + private followMasterClient: FollowMasterClient, + ) { } // eslint-disable-next-line complexity, max-lines-per-function diff --git a/src/content/controllers/MarkController.ts b/src/content/controllers/MarkController.ts index 365794c..5eb898a 100644 --- a/src/content/controllers/MarkController.ts +++ b/src/content/controllers/MarkController.ts @@ -1,13 +1,12 @@ +import { injectable } from 'tsyringe'; import * as messages from '../../shared/messages'; import MarkUseCase from '../usecases/MarkUseCase'; +@injectable() export default class MarkController { - private markUseCase: MarkUseCase; - - constructor({ - markUseCase = new MarkUseCase(), - } = {}) { - this.markUseCase = markUseCase; + constructor( + private markUseCase: MarkUseCase, + ) { } scrollTo(message: messages.TabScrollToMessage) { diff --git a/src/content/controllers/MarkKeyController.ts b/src/content/controllers/MarkKeyController.ts index 395dee3..886e5ff 100644 --- a/src/content/controllers/MarkKeyController.ts +++ b/src/content/controllers/MarkKeyController.ts @@ -1,18 +1,14 @@ +import { injectable } from 'tsyringe'; import MarkUseCase from '../usecases/MarkUseCase'; import MarkKeyyUseCase from '../usecases/MarkKeyUseCase'; import Key from '../domains/Key'; +@injectable() export default class MarkKeyController { - private markUseCase: MarkUseCase; - - private markKeyUseCase: MarkKeyyUseCase; - - constructor({ - markUseCase = new MarkUseCase(), - markKeyUseCase = new MarkKeyyUseCase(), - } = {}) { - this.markUseCase = markUseCase; - this.markKeyUseCase = markKeyUseCase; + constructor( + private markUseCase: MarkUseCase, + private markKeyUseCase: MarkKeyyUseCase, + ) { } press(key: Key): boolean { diff --git a/src/content/controllers/SettingController.ts b/src/content/controllers/SettingController.ts index f0e770b..7fb045b 100644 --- a/src/content/controllers/SettingController.ts +++ b/src/content/controllers/SettingController.ts @@ -1,20 +1,17 @@ +import { injectable } from 'tsyringe'; import AddonEnabledUseCase from '../usecases/AddonEnabledUseCase'; import SettingUseCase from '../usecases/SettingUseCase'; import * as blacklists from '../../shared/blacklists'; import * as messages from '../../shared/messages'; +@injectable() export default class SettingController { - private addonEnabledUseCase: AddonEnabledUseCase; - private settingUseCase: SettingUseCase; - - constructor({ - addonEnabledUseCase = new AddonEnabledUseCase(), - settingUseCase = new SettingUseCase(), - } = {}) { - this.addonEnabledUseCase = addonEnabledUseCase; - this.settingUseCase = settingUseCase; + constructor( + private addonEnabledUseCase: AddonEnabledUseCase, + private settingUseCase: SettingUseCase, + ) { } async initSettings(): Promise<void> { diff --git a/src/content/di.ts b/src/content/di.ts new file mode 100644 index 0000000..a890666 --- /dev/null +++ b/src/content/di.ts @@ -0,0 +1,52 @@ +/* eslint-disable max-len */ + +import { FollowMasterClientImpl } from './client/FollowMasterClient'; +import { AddonIndicatorClientImpl } from './client/AddonIndicatorClient'; +import { AddonEnabledRepositoryImpl } from './repositories/AddonEnabledRepository'; +import { ClipboardRepositoryImpl } from './repositories/ClipboardRepository'; +import { SettingRepositoryImpl } from './repositories/SettingRepository'; +import { TabsClientImpl } from './client/TabsClient'; +import { ConsoleClientImpl } from './client/ConsoleClient'; +import { ConsoleFramePresenterImpl } from './presenters/ConsoleFramePresenter'; +import { FindMasterClientImpl } from './client/FindMasterClient'; +import { FindPresenterImpl } from './presenters/FindPresenter'; +import { FindRepositoryImpl } from './repositories/FindRepository'; +import { FindClientImpl } from './client/FindClient'; +import { FocusPresenterImpl } from './presenters/FocusPresenter'; +import { FollowKeyRepositoryImpl } from './repositories/FollowKeyRepository'; +import { FollowMasterRepositoryImpl } from './repositories/FollowMasterRepository'; +import { FollowSlaveRepositoryImpl } from './repositories/FollowSlaveRepository'; +import { FollowPresenterImpl } from './presenters/FollowPresenter'; +import { KeymapRepositoryImpl } from './repositories/KeymapRepository'; +import { MarkKeyRepositoryImpl } from './repositories/MarkKeyRepository'; +import { ScrollPresenterImpl } from './presenters/ScrollPresenter'; +import { MarkClientImpl } from './client/MarkClient'; +import { MarkRepositoryImpl } from './repositories/MarkRepository'; +import { NavigationPresenterImpl } from './presenters/NavigationPresenter'; +import { SettingClientImpl } from './client/SettingClient'; +import { container } from 'tsyringe'; + +container.register('FollowMasterClient', { useValue: new FollowMasterClientImpl(window.top) }); +container.register('AddonIndicatorClient', { useClass: AddonIndicatorClientImpl }); +container.register('AddonEnabledRepository', { useClass: AddonEnabledRepositoryImpl }); +container.register('ClipboardRepository', { useClass: ClipboardRepositoryImpl }); +container.register('SettingRepository', { useClass: SettingRepositoryImpl }); +container.register('TabsClient', { useClass: TabsClientImpl }); +container.register('ConsoleClient', { useClass: ConsoleClientImpl }); +container.register('ConsoleFramePresenter', { useClass: ConsoleFramePresenterImpl }); +container.register('FindMasterClient', { useClass: FindMasterClientImpl }); +container.register('FindPresenter', { useClass: FindPresenterImpl }); +container.register('FindRepository', { useClass: FindRepositoryImpl }); +container.register('FindClient', { useClass: FindClientImpl }); +container.register('FocusPresenter', { useClass: FocusPresenterImpl }); +container.register('FollowKeyRepository', { useClass: FollowKeyRepositoryImpl }); +container.register('FollowMasterRepository', { useClass: FollowMasterRepositoryImpl }); +container.register('FollowSlaveRepository', { useClass: FollowSlaveRepositoryImpl }); +container.register('FollowPresenter', { useClass: FollowPresenterImpl }); +container.register('KeymapRepository', { useClass: KeymapRepositoryImpl }); +container.register('MarkKeyRepository', { useClass: MarkKeyRepositoryImpl }); +container.register('ScrollPresenter', { useClass: ScrollPresenterImpl }); +container.register('MarkClient', { useClass: MarkClientImpl }); +container.register('MarkRepository', { useClass: MarkRepositoryImpl }); +container.register('NavigationPresenter', { useClass: NavigationPresenterImpl }); +container.register('SettingClient', { useClass: SettingClientImpl }); diff --git a/src/content/index.ts b/src/content/index.ts index 660ebf5..5b9b92c 100644 --- a/src/content/index.ts +++ b/src/content/index.ts @@ -1,15 +1,19 @@ -import { ConsoleFramePresenterImpl } from './presenters/ConsoleFramePresenter'; +import 'reflect-metadata'; + +import Application from './Application'; import consoleFrameStyle from './site-style'; -import * as routes from './routes'; +import { ConsoleFramePresenterImpl } from './presenters/ConsoleFramePresenter'; +import { container } from 'tsyringe'; +import './di'; if (window.self === window.top) { - routes.routeMasterComponents(); - new ConsoleFramePresenterImpl().initialize(); } -routes.routeComponents(); - +try { + let app = container.resolve(Application); + app.run(); +} catch (e) { console.error(e); } let style = window.document.createElement('style'); style.textContent = consoleFrameStyle; diff --git a/src/content/routes.ts b/src/content/routes.ts deleted file mode 100644 index 0bce4f5..0000000 --- a/src/content/routes.ts +++ /dev/null @@ -1,97 +0,0 @@ -import MessageListener from './MessageListener'; -import FindController from './controllers/FindController'; -import MarkController from './controllers/MarkController'; -import FollowMasterController from './controllers/FollowMasterController'; -import FollowSlaveController from './controllers/FollowSlaveController'; -import FollowKeyController from './controllers/FollowKeyController'; -import InputDriver from './InputDriver'; -import KeymapController from './controllers/KeymapController'; -import AddonEnabledUseCase from './usecases/AddonEnabledUseCase'; -import MarkKeyController from './controllers/MarkKeyController'; -import AddonEnabledController from './controllers/AddonEnabledController'; -import SettingController from './controllers/SettingController'; -import ConsoleFrameController from './controllers/ConsoleFrameController'; -import * as messages from '../shared/messages'; - -export const routeComponents = () => { - let listener = new MessageListener(); - - let followSlaveController = new FollowSlaveController(); - listener.onWebMessage((message: messages.Message) => { - switch (message.type) { - case messages.FOLLOW_REQUEST_COUNT_TARGETS: - return followSlaveController.countTargets(message); - case messages.FOLLOW_CREATE_HINTS: - return followSlaveController.createHints(message); - case messages.FOLLOW_SHOW_HINTS: - return followSlaveController.showHints(message); - case messages.FOLLOW_ACTIVATE: - return followSlaveController.activate(message); - case messages.FOLLOW_REMOVE_HINTS: - return followSlaveController.clear(message); - } - return undefined; - }); - - let keymapController = new KeymapController(); - let markKeyController = new MarkKeyController(); - let followKeyController = new FollowKeyController(); - let inputDriver = new InputDriver(document.body); - inputDriver.onKey(key => followKeyController.press(key)); - inputDriver.onKey(key => markKeyController.press(key)); - inputDriver.onKey(key => keymapController.press(key)); - - let settingController = new SettingController(); - settingController.initSettings(); - - listener.onBackgroundMessage((message: messages.Message): any => { - let addonEnabledUseCase = new AddonEnabledUseCase(); - - switch (message.type) { - case messages.SETTINGS_CHANGED: - return settingController.reloadSettings(message); - case messages.ADDON_TOGGLE_ENABLED: - return addonEnabledUseCase.toggle(); - } - }); -}; - -export const routeMasterComponents = () => { - let listener = new MessageListener(); - - let findController = new FindController(); - let followMasterController = new FollowMasterController(); - let markController = new MarkController(); - let addonEnabledController = new AddonEnabledController(); - let consoleFrameController = new ConsoleFrameController(); - - listener.onWebMessage((message: messages.Message, sender: Window) => { - switch (message.type) { - case messages.CONSOLE_ENTER_FIND: - return findController.start(message); - case messages.FIND_NEXT: - return findController.next(message); - case messages.FIND_PREV: - return findController.prev(message); - case messages.CONSOLE_UNFOCUS: - return consoleFrameController.unfocus(message); - case messages.FOLLOW_START: - return followMasterController.followStart(message); - case messages.FOLLOW_RESPONSE_COUNT_TARGETS: - return followMasterController.responseCountTargets(message, sender); - case messages.FOLLOW_KEY_PRESS: - return followMasterController.keyPress(message); - } - return undefined; - }); - - listener.onBackgroundMessage((message: messages.Message) => { - switch (message.type) { - case messages.ADDON_ENABLED_QUERY: - return addonEnabledController.getAddonEnabled(message); - case messages.TAB_SCROLL_TO: - return markController.scrollTo(message); - } - return undefined; - }); -}; diff --git a/src/content/usecases/AddonEnabledUseCase.ts b/src/content/usecases/AddonEnabledUseCase.ts index e9ce0a6..2d6fa11 100644 --- a/src/content/usecases/AddonEnabledUseCase.ts +++ b/src/content/usecases/AddonEnabledUseCase.ts @@ -1,19 +1,17 @@ -import AddonIndicatorClient, { AddonIndicatorClientImpl } - from '../client/AddonIndicatorClient'; -import AddonEnabledRepository, { AddonEnabledRepositoryImpl } - from '../repositories/AddonEnabledRepository'; +import { injectable, inject } from 'tsyringe'; +import AddonIndicatorClient from '../client/AddonIndicatorClient'; +import AddonEnabledRepository from '../repositories/AddonEnabledRepository'; +@injectable() export default class AddonEnabledUseCase { - private indicator: AddonIndicatorClient; - private repository: AddonEnabledRepository; + constructor( + @inject('AddonIndicatorClient') + private indicator: AddonIndicatorClient, - constructor({ - indicator = new AddonIndicatorClientImpl(), - repository = new AddonEnabledRepositoryImpl(), - } = {}) { - this.indicator = indicator; - this.repository = repository; + @inject('AddonEnabledRepository') + private repository: AddonEnabledRepository, + ) { } async enable(): Promise<void> { diff --git a/src/content/usecases/ClipboardUseCase.ts b/src/content/usecases/ClipboardUseCase.ts index b2ece2f..8c4d621 100644 --- a/src/content/usecases/ClipboardUseCase.ts +++ b/src/content/usecases/ClipboardUseCase.ts @@ -1,31 +1,18 @@ +import { injectable, inject } from 'tsyringe'; import * as urls from '../../shared/urls'; -import ClipboardRepository, { ClipboardRepositoryImpl } - from '../repositories/ClipboardRepository'; -import SettingRepository, { SettingRepositoryImpl } - from '../repositories/SettingRepository'; -import TabsClient, { TabsClientImpl } - from '../client/TabsClient'; -import ConsoleClient, { ConsoleClientImpl } from '../client/ConsoleClient'; +import ClipboardRepository from '../repositories/ClipboardRepository'; +import SettingRepository from '../repositories/SettingRepository'; +import TabsClient from '../client/TabsClient'; +import ConsoleClient from '../client/ConsoleClient'; +@injectable() export default class ClipboardUseCase { - private repository: ClipboardRepository; - - private settingRepository: SettingRepository; - - private client: TabsClient; - - private consoleClient: ConsoleClient; - - constructor({ - repository = new ClipboardRepositoryImpl(), - settingRepository = new SettingRepositoryImpl(), - client = new TabsClientImpl(), - consoleClient = new ConsoleClientImpl(), - } = {}) { - this.repository = repository; - this.settingRepository = settingRepository; - this.client = client; - this.consoleClient = consoleClient; + constructor( + @inject('ClipboardRepository') private repository: ClipboardRepository, + @inject('SettingRepository') private settingRepository: SettingRepository, + @inject('TabsClient') private client: TabsClient, + @inject('ConsoleClient') private consoleClient: ConsoleClient, + ) { } async yankCurrentURL(): Promise<string> { diff --git a/src/content/usecases/ConsoleFrameUseCase.ts b/src/content/usecases/ConsoleFrameUseCase.ts index b4c756c..3c4b0a1 100644 --- a/src/content/usecases/ConsoleFrameUseCase.ts +++ b/src/content/usecases/ConsoleFrameUseCase.ts @@ -1,13 +1,12 @@ -import ConsoleFramePresenter, { ConsoleFramePresenterImpl } - from '../presenters/ConsoleFramePresenter'; +import { injectable, inject } from 'tsyringe'; +import ConsoleFramePresenter from '../presenters/ConsoleFramePresenter'; +@injectable() export default class ConsoleFrameUseCase { - private consoleFramePresenter: ConsoleFramePresenter; - - constructor({ - consoleFramePresenter = new ConsoleFramePresenterImpl(), - } = {}) { - this.consoleFramePresenter = consoleFramePresenter; + constructor( + @inject('ConsoleFramePresenter') + private consoleFramePresenter: ConsoleFramePresenter, + ) { } unfocus() { diff --git a/src/content/usecases/FindSlaveUseCase.ts b/src/content/usecases/FindSlaveUseCase.ts index b733cbd..0a5c2ce 100644 --- a/src/content/usecases/FindSlaveUseCase.ts +++ b/src/content/usecases/FindSlaveUseCase.ts @@ -1,13 +1,11 @@ -import FindMasterClient, { FindMasterClientImpl } - from '../client/FindMasterClient'; +import { injectable, inject } from 'tsyringe'; +import FindMasterClient from '../client/FindMasterClient'; +@injectable() export default class FindSlaveUseCase { - private findMasterClient: FindMasterClient; - - constructor({ - findMasterClient = new FindMasterClientImpl(), - } = {}) { - this.findMasterClient = findMasterClient; + constructor( + @inject('FindMasterClient') private findMasterClient: FindMasterClient, + ) { } findNext() { diff --git a/src/content/usecases/FindUseCase.ts b/src/content/usecases/FindUseCase.ts index 74cbc97..88b516c 100644 --- a/src/content/usecases/FindUseCase.ts +++ b/src/content/usecases/FindUseCase.ts @@ -1,28 +1,17 @@ -import FindPresenter, { FindPresenterImpl } from '../presenters/FindPresenter'; -import FindRepository, { FindRepositoryImpl } - from '../repositories/FindRepository'; -import FindClient, { FindClientImpl } from '../client/FindClient'; -import ConsoleClient, { ConsoleClientImpl } from '../client/ConsoleClient'; +import { injectable, inject } from 'tsyringe'; +import FindPresenter from '../presenters/FindPresenter'; +import FindRepository from '../repositories/FindRepository'; +import FindClient from '../client/FindClient'; +import ConsoleClient from '../client/ConsoleClient'; +@injectable() export default class FindUseCase { - private presenter: FindPresenter; - - private repository: FindRepository; - - private client: FindClient; - - private consoleClient: ConsoleClient; - - constructor({ - presenter = new FindPresenterImpl() as FindPresenter, - repository = new FindRepositoryImpl(), - client = new FindClientImpl(), - consoleClient = new ConsoleClientImpl(), - } = {}) { - this.presenter = presenter; - this.repository = repository; - this.client = client; - this.consoleClient = consoleClient; + constructor( + @inject('FindPresenter') private presenter: FindPresenter, + @inject('FindRepository') private repository: FindRepository, + @inject('FindClient') private client: FindClient, + @inject('ConsoleClient') private consoleClient: ConsoleClient, + ) { } async startFind(keyword?: string): Promise<void> { diff --git a/src/content/usecases/FocusUseCase.ts b/src/content/usecases/FocusUseCase.ts index 0ad4021..0158672 100644 --- a/src/content/usecases/FocusUseCase.ts +++ b/src/content/usecases/FocusUseCase.ts @@ -1,13 +1,11 @@ -import FocusPresenter, { FocusPresenterImpl } - from '../presenters/FocusPresenter'; +import { injectable, inject } from 'tsyringe'; +import FocusPresenter from '../presenters/FocusPresenter'; +@injectable() export default class FocusUseCases { - private presenter: FocusPresenter; - - constructor({ - presenter = new FocusPresenterImpl(), - } = {}) { - this.presenter = presenter; + constructor( + @inject('FocusPresenter') private presenter: FocusPresenter, + ) { } focusFirstInput() { diff --git a/src/content/usecases/FollowMasterUseCase.ts b/src/content/usecases/FollowMasterUseCase.ts index 9cbb790..38c41b7 100644 --- a/src/content/usecases/FollowMasterUseCase.ts +++ b/src/content/usecases/FollowMasterUseCase.ts @@ -1,31 +1,26 @@ -import FollowKeyRepository, { FollowKeyRepositoryImpl } - from '../repositories/FollowKeyRepository'; -import FollowMasterRepository, { FollowMasterRepositoryImpl } - from '../repositories/FollowMasterRepository'; +import { injectable, inject } from 'tsyringe'; +import FollowKeyRepository from '../repositories/FollowKeyRepository'; +import FollowMasterRepository from '../repositories/FollowMasterRepository'; import FollowSlaveClient, { FollowSlaveClientImpl } from '../client/FollowSlaveClient'; +import SettingRepository from '../repositories/SettingRepository'; import HintKeyProducer from './HintKeyProducer'; -import SettingRepository, { SettingRepositoryImpl } - from '../repositories/SettingRepository'; +@injectable() export default class FollowMasterUseCase { - private followKeyRepository: FollowKeyRepository; - - private followMasterRepository: FollowMasterRepository; - - private settingRepository: SettingRepository; - // TODO Make repository private producer: HintKeyProducer | null; - constructor({ - followKeyRepository = new FollowKeyRepositoryImpl(), - followMasterRepository = new FollowMasterRepositoryImpl(), - settingRepository = new SettingRepositoryImpl(), - } = {}) { - this.followKeyRepository = followKeyRepository; - this.followMasterRepository = followMasterRepository; - this.settingRepository = settingRepository; + constructor( + @inject('FollowKeyRepository') + private followKeyRepository: FollowKeyRepository, + + @inject('FollowMasterRepository') + private followMasterRepository: FollowMasterRepository, + + @inject('SettingRepository') + private settingRepository: SettingRepository, + ) { this.producer = null; } diff --git a/src/content/usecases/FollowSlaveUseCase.ts b/src/content/usecases/FollowSlaveUseCase.ts index eb011de..a2cc80e 100644 --- a/src/content/usecases/FollowSlaveUseCase.ts +++ b/src/content/usecases/FollowSlaveUseCase.ts @@ -1,11 +1,9 @@ -import FollowSlaveRepository, { FollowSlaveRepositoryImpl } - from '../repositories/FollowSlaveRepository'; -import FollowPresenter, { FollowPresenterImpl } - from '../presenters/FollowPresenter'; -import TabsClient, { TabsClientImpl } from '../client/TabsClient'; +import { injectable, inject } from 'tsyringe'; +import FollowSlaveRepository from '../repositories/FollowSlaveRepository'; +import FollowPresenter from '../presenters/FollowPresenter'; +import TabsClient from '../client/TabsClient'; +import FollowMasterClient from '../client/FollowMasterClient'; import { LinkHint, InputHint } from '../presenters/Hint'; -import FollowMasterClient, { FollowMasterClientImpl } - from '../client/FollowMasterClient'; import Key from '../domains/Key'; interface Size { @@ -18,25 +16,21 @@ interface Point { y: number; } +@injectable() export default class FollowSlaveUseCase { - private presenter: FollowPresenter; + constructor( + @inject('FollowPresenter') + private presenter: FollowPresenter, - private tabsClient: TabsClient; + @inject('TabsClient') + private tabsClient: TabsClient, - private followMasterClient: FollowMasterClient; + @inject('FollowMasterClient') + private followMasterClient: FollowMasterClient, - private followSlaveRepository: FollowSlaveRepository; - - constructor({ - presenter = new FollowPresenterImpl(), - tabsClient = new TabsClientImpl(), - followMasterClient = new FollowMasterClientImpl(window.top), - followSlaveRepository = new FollowSlaveRepositoryImpl(), - } = {}) { - this.presenter = presenter; - this.tabsClient = tabsClient; - this.followMasterClient = followMasterClient; - this.followSlaveRepository = followSlaveRepository; + @inject('FollowSlaveRepository') + private followSlaveRepository: FollowSlaveRepository, + ) { } countTargets(viewSize: Size, framePosition: Point): void { diff --git a/src/content/usecases/KeymapUseCase.ts b/src/content/usecases/KeymapUseCase.ts index af0ad77..d0d039a 100644 --- a/src/content/usecases/KeymapUseCase.ts +++ b/src/content/usecases/KeymapUseCase.ts @@ -1,10 +1,7 @@ -import KeymapRepository, { KeymapRepositoryImpl } - from '../repositories/KeymapRepository'; -import SettingRepository, { SettingRepositoryImpl } - from '../repositories/SettingRepository'; -import AddonEnabledRepository, { AddonEnabledRepositoryImpl } - from '../repositories/AddonEnabledRepository'; - +import { injectable, inject } from 'tsyringe'; +import KeymapRepository from '../repositories/KeymapRepository'; +import SettingRepository from '../repositories/SettingRepository'; +import AddonEnabledRepository from '../repositories/AddonEnabledRepository'; import * as operations from '../../shared/operations'; import { Keymaps } from '../../shared/Settings'; import Key from '../domains/Key'; @@ -17,22 +14,18 @@ const reservedKeymaps: Keymaps = { '<C-[>': { type: operations.CANCEL }, }; - +@injectable() export default class KeymapUseCase { - private repository: KeymapRepository; - - private settingRepository: SettingRepository; + constructor( + @inject('KeymapRepository') + private repository: KeymapRepository, - private addonEnabledRepository: AddonEnabledRepository; + @inject('SettingRepository') + private settingRepository: SettingRepository, - constructor({ - repository = new KeymapRepositoryImpl(), - settingRepository = new SettingRepositoryImpl(), - addonEnabledRepository = new AddonEnabledRepositoryImpl(), - } = {}) { - this.repository = repository; - this.settingRepository = settingRepository; - this.addonEnabledRepository = addonEnabledRepository; + @inject('AddonEnabledRepository') + private addonEnabledRepository: AddonEnabledRepository, + ) { } nextOp(key: Key): operations.Operation | null { diff --git a/src/content/usecases/MarkKeyUseCase.ts b/src/content/usecases/MarkKeyUseCase.ts index c0aa655..6cef6f2 100644 --- a/src/content/usecases/MarkKeyUseCase.ts +++ b/src/content/usecases/MarkKeyUseCase.ts @@ -1,13 +1,11 @@ -import MarkKeyRepository, { MarkKeyRepositoryImpl } - from '../repositories/MarkKeyRepository'; +import { injectable, inject } from 'tsyringe'; +import MarkKeyRepository from '../repositories/MarkKeyRepository'; +@injectable() export default class MarkKeyUseCase { - private repository: MarkKeyRepository; - - constructor({ - repository = new MarkKeyRepositoryImpl() - } = {}) { - this.repository = repository; + constructor( + @inject('MarkKeyRepository') private repository: MarkKeyRepository, + ) { } isSetMode(): boolean { diff --git a/src/content/usecases/MarkUseCase.ts b/src/content/usecases/MarkUseCase.ts index 530f141..a7d5ad8 100644 --- a/src/content/usecases/MarkUseCase.ts +++ b/src/content/usecases/MarkUseCase.ts @@ -1,35 +1,19 @@ -import ScrollPresenter, { ScrollPresenterImpl } - from '../presenters/ScrollPresenter'; -import MarkClient, { MarkClientImpl } from '../client/MarkClient'; -import MarkRepository, { MarkRepositoryImpl } - from '../repositories/MarkRepository'; -import SettingRepository, { SettingRepositoryImpl } - from '../repositories/SettingRepository'; -import ConsoleClient, { ConsoleClientImpl } from '../client/ConsoleClient'; - +import { injectable, inject } from 'tsyringe'; +import ScrollPresenter from '../presenters/ScrollPresenter'; +import MarkClient from '../client/MarkClient'; +import MarkRepository from '../repositories/MarkRepository'; +import SettingRepository from '../repositories/SettingRepository'; +import ConsoleClient from '../client/ConsoleClient'; + +@injectable() export default class MarkUseCase { - private scrollPresenter: ScrollPresenter; - - private client: MarkClient; - - private repository: MarkRepository; - - private settingRepository: SettingRepository; - - private consoleClient: ConsoleClient; - - constructor({ - scrollPresenter = new ScrollPresenterImpl(), - client = new MarkClientImpl(), - repository = new MarkRepositoryImpl(), - settingRepository = new SettingRepositoryImpl(), - consoleClient = new ConsoleClientImpl(), - } = {}) { - this.scrollPresenter = scrollPresenter; - this.client = client; - this.repository = repository; - this.settingRepository = settingRepository; - this.consoleClient = consoleClient; + constructor( + @inject('ScrollPresenter') private scrollPresenter: ScrollPresenter, + @inject('MarkClient') private client: MarkClient, + @inject('MarkRepository') private repository: MarkRepository, + @inject('SettingRepository') private settingRepository: SettingRepository, + @inject('ConsoleClient') private consoleClient: ConsoleClient, + ) { } async set(key: string): Promise<void> { diff --git a/src/content/usecases/NavigateUseCase.ts b/src/content/usecases/NavigateUseCase.ts index 6f82d3f..4711c5e 100644 --- a/src/content/usecases/NavigateUseCase.ts +++ b/src/content/usecases/NavigateUseCase.ts @@ -1,13 +1,12 @@ -import NavigationPresenter, { NavigationPresenterImpl } - from '../presenters/NavigationPresenter'; +import { injectable, inject } from 'tsyringe'; +import NavigationPresenter from '../presenters/NavigationPresenter'; +@injectable() export default class NavigateUseCase { - private navigationPresenter: NavigationPresenter; - - constructor({ - navigationPresenter = new NavigationPresenterImpl(), - } = {}) { - this.navigationPresenter = navigationPresenter; + constructor( + @inject('NavigationPresenter') + private navigationPresenter: NavigationPresenter, + ) { } openHistoryPrev(): void { diff --git a/src/content/usecases/ScrollUseCase.ts b/src/content/usecases/ScrollUseCase.ts index 6a1f801..32cbef1 100644 --- a/src/content/usecases/ScrollUseCase.ts +++ b/src/content/usecases/ScrollUseCase.ts @@ -1,19 +1,13 @@ -import ScrollPresenter, { ScrollPresenterImpl } - from '../presenters/ScrollPresenter'; -import SettingRepository, { SettingRepositoryImpl } - from '../repositories/SettingRepository'; +import { injectable, inject } from 'tsyringe'; +import ScrollPresenter from '../presenters/ScrollPresenter'; +import SettingRepository from '../repositories/SettingRepository'; +@injectable() export default class ScrollUseCase { - private presenter: ScrollPresenter; - - private settingRepository: SettingRepository; - - constructor({ - presenter = new ScrollPresenterImpl(), - settingRepository = new SettingRepositoryImpl(), - } = {}) { - this.presenter = presenter; - this.settingRepository = settingRepository; + constructor( + @inject('ScrollPresenter') private presenter: ScrollPresenter, + @inject('SettingRepository') private settingRepository: SettingRepository, + ) { } scrollVertically(count: number): void { diff --git a/src/content/usecases/SettingUseCase.ts b/src/content/usecases/SettingUseCase.ts index 765cb45..d5f66c6 100644 --- a/src/content/usecases/SettingUseCase.ts +++ b/src/content/usecases/SettingUseCase.ts @@ -1,19 +1,14 @@ -import SettingRepository, { SettingRepositoryImpl } - from '../repositories/SettingRepository'; -import SettingClient, { SettingClientImpl } from '../client/SettingClient'; +import { injectable, inject } from 'tsyringe'; +import SettingRepository from '../repositories/SettingRepository'; +import SettingClient from '../client/SettingClient'; import Settings from '../../shared/Settings'; +@injectable() export default class SettingUseCase { - private repository: SettingRepository; - - private client: SettingClient; - - constructor({ - repository = new SettingRepositoryImpl(), - client = new SettingClientImpl(), - } = {}) { - this.repository = repository; - this.client = client; + constructor( + @inject('SettingRepository') private repository: SettingRepository, + @inject('SettingClient') private client: SettingClient, + ) { } async reload(): Promise<Settings> { diff --git a/test/content/usecases/AddonEnabledUseCase.test.ts b/test/content/usecases/AddonEnabledUseCase.test.ts index 912bddf..8c15099 100644 --- a/test/content/usecases/AddonEnabledUseCase.test.ts +++ b/test/content/usecases/AddonEnabledUseCase.test.ts @@ -40,7 +40,7 @@ describe('AddonEnabledUseCase', () => { beforeEach(() => { repository = new MockAddonEnabledRepository(true); indicator = new MockAddonIndicatorClient(false); - sut = new AddonEnabledUseCase({ repository, indicator }); + sut = new AddonEnabledUseCase(indicator, repository); }); describe('#enable', () => { diff --git a/test/content/usecases/ClipboardUseCase.test.ts b/test/content/usecases/ClipboardUseCase.test.ts index 862ee8a..551c3f7 100644 --- a/test/content/usecases/ClipboardUseCase.test.ts +++ b/test/content/usecases/ClipboardUseCase.test.ts @@ -1,4 +1,5 @@ import ClipboardRepository from '../../../src/content/repositories/ClipboardRepository'; +import { SettingRepositoryImpl } from '../../../src/content/repositories/SettingRepository'; import TabsClient from '../../../src/content/client/TabsClient'; import MockConsoleClient from '../mock/MockConsoleClient'; import ClipboardUseCase from '../../../src/content/usecases/ClipboardUseCase'; @@ -43,7 +44,12 @@ describe('ClipboardUseCase', () => { repository = new MockClipboardRepository(); client = new MockTabsClient(); consoleClient = new MockConsoleClient(); - sut = new ClipboardUseCase({ repository, client: client, consoleClient }); + sut = new ClipboardUseCase( + repository, + new SettingRepositoryImpl(), + client, + consoleClient + ); }); describe('#yankCurrentURL', () => { diff --git a/test/content/usecases/FindUseCase.test.ts b/test/content/usecases/FindUseCase.test.ts index c7bfd39..ddd4cd4 100644 --- a/test/content/usecases/FindUseCase.test.ts +++ b/test/content/usecases/FindUseCase.test.ts @@ -71,7 +71,7 @@ describe('FindUseCase', () => { presenter = new MockFindPresenter(); client = new MockFindClient(); consoleClient = new MockConsoleClient(); - sut = new FindUseCase({ repository, presenter, client, consoleClient }); + sut = new FindUseCase(presenter, repository, client, consoleClient); }); describe('#startFind', () => { diff --git a/test/content/usecases/MarkUseCase.test.ts b/test/content/usecases/MarkUseCase.test.ts index 4f2dee4..494497a 100644 --- a/test/content/usecases/MarkUseCase.test.ts +++ b/test/content/usecases/MarkUseCase.test.ts @@ -1,4 +1,5 @@ import MarkRepository from '../../../src/content/repositories/MarkRepository'; +import { SettingRepositoryImpl } from '../../../src/content/repositories/SettingRepository'; import MarkUseCase from '../../../src/content/usecases/MarkUseCase'; import MarkClient from '../../../src/content/client/MarkClient'; import MockConsoleClient from '../mock/MockConsoleClient'; @@ -54,9 +55,13 @@ describe('MarkUseCase', () => { client = new MockMarkClient(); consoleClient = new MockConsoleClient(); scrollPresenter = new MockScrollPresenter(); - sut = new MarkUseCase({ - repository, client, consoleClient, scrollPresenter, - }); + sut = new MarkUseCase( + scrollPresenter, + client, + repository, + new SettingRepositoryImpl(), + consoleClient, + ); }); describe('#set', () => { diff --git a/test/content/usecases/SettingUseCaase.test.ts b/test/content/usecases/SettingUseCaase.test.ts index 02cef78..e9633f4 100644 --- a/test/content/usecases/SettingUseCaase.test.ts +++ b/test/content/usecases/SettingUseCaase.test.ts @@ -56,7 +56,7 @@ describe('AddonEnabledUseCase', () => { repository = new MockSettingRepository(); client = new MockSettingClient(testSettings); - sut = new SettingUseCase({ repository, client }); + sut = new SettingUseCase(repository, client); }); describe('#reload', () => { |