diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-05-22 20:16:21 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-22 20:16:21 +0900 |
commit | c1f64927b63d18048790abd3ba907083dbca3084 (patch) | |
tree | 3a64b677763f62094c49527d8182f8756a52fbd1 /src/content/Application.ts | |
parent | ced89134e32d793d8e091113cfb20867e1c3b572 (diff) | |
parent | 7be8bc71784b8dedd0fee03dd72dd8936e2f3929 (diff) |
Merge pull request #588 from ueokande/tsyringe
Use tsyringe for DI container
Diffstat (limited to 'src/content/Application.ts')
-rw-r--r-- | src/content/Application.ts | 111 |
1 files changed, 111 insertions, 0 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(); + } +} |