From d1bfd92b66c1263f5486af2bcc750815c941184f Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sun, 21 Jul 2024 09:04:37 +0000 Subject: [PATCH] use provider module of inversify in content scripts --- src/content/client/BackgroundKeyClient.ts | 5 +- src/content/client/OperationClient.ts | 5 +- src/content/client/SettingClient.ts | 5 +- src/content/client/TopFrameClient.ts | 5 +- src/content/di.ts | 91 +++++-------------- .../presenters/ConsoleFramePresenter.ts | 4 +- src/content/presenters/FindPresenter.ts | 4 +- src/content/presenters/FocusPresenter.ts | 4 +- src/content/presenters/HintPresenter.ts | 5 +- src/content/presenters/NavigationPresenter.ts | 4 +- .../presenters/ReadyStatusPresenter.ts | 4 +- src/content/presenters/ScrollPresenter.ts | 4 +- .../repositories/AddonEnabledRepository.ts | 4 +- src/content/repositories/AddressRepository.ts | 4 +- src/content/repositories/FrameIdRepository.ts | 4 +- src/content/repositories/KeymapRepository.ts | 4 +- src/content/repositories/ModeRepository.ts | 4 +- src/content/repositories/SettingRepository.ts | 5 +- 18 files changed, 61 insertions(+), 104 deletions(-) diff --git a/src/content/client/BackgroundKeyClient.ts b/src/content/client/BackgroundKeyClient.ts index 90fbebb8..d147f3d1 100644 --- a/src/content/client/BackgroundKeyClient.ts +++ b/src/content/client/BackgroundKeyClient.ts @@ -1,4 +1,5 @@ -import { injectable, inject } from "inversify"; +import { inject } from "inversify"; +import { provide } from "inversify-binding-decorators"; import type { Key } from "../../shared/key"; import { BackgroundMessageSender } from "./BackgroundMessageSender"; @@ -8,7 +9,7 @@ export interface BackgroundKeyClient { export const BackgroundKeyClient = Symbol("BackgroundKeyClient"); -@injectable() +@provide(BackgroundKeyClient) export class BackgroundKeyClientImpl implements BackgroundKeyClient { constructor( @inject(BackgroundMessageSender) diff --git a/src/content/client/OperationClient.ts b/src/content/client/OperationClient.ts index 1c2c5974..071c59bd 100644 --- a/src/content/client/OperationClient.ts +++ b/src/content/client/OperationClient.ts @@ -1,4 +1,5 @@ -import { injectable, inject } from "inversify"; +import { inject } from "inversify"; +import { provide } from "inversify-binding-decorators"; import type { Operation } from "../../shared/operation"; import { BackgroundMessageSender } from "./BackgroundMessageSender"; @@ -8,7 +9,7 @@ export interface OperationClient { export const OperationClient = Symbol("OperationClient"); -@injectable() +@provide(OperationClient) export class OperationClientImpl implements OperationClient { constructor( @inject(BackgroundMessageSender) diff --git a/src/content/client/SettingClient.ts b/src/content/client/SettingClient.ts index 118083ef..1c93f624 100644 --- a/src/content/client/SettingClient.ts +++ b/src/content/client/SettingClient.ts @@ -1,4 +1,5 @@ -import { injectable, inject } from "inversify"; +import { inject } from "inversify"; +import { provide } from "inversify-binding-decorators"; import { deserialize } from "../../settings"; import type { Settings } from "../../shared/settings"; import { BackgroundMessageSender } from "./BackgroundMessageSender"; @@ -9,7 +10,7 @@ export interface SettingClient { export const SettingClient = Symbol("SettingClient"); -@injectable() +@provide(SettingClient) export class SettingClientImpl { constructor( @inject(BackgroundMessageSender) diff --git a/src/content/client/TopFrameClient.ts b/src/content/client/TopFrameClient.ts index b200af46..a0fecdf2 100644 --- a/src/content/client/TopFrameClient.ts +++ b/src/content/client/TopFrameClient.ts @@ -1,4 +1,5 @@ -import { injectable, inject } from "inversify"; +import { inject } from "inversify"; +import { provide } from "inversify-binding-decorators"; import { WindowMessageSender } from "./WindowMessageSender"; export interface TopFrameClient { @@ -7,7 +8,7 @@ export interface TopFrameClient { export const TopFrameClient = Symbol("TopFrameClient"); -@injectable() +@provide(TopFrameClient) export class TopFrameClientImpl implements TopFrameClient { constructor( @inject(WindowMessageSender) diff --git a/src/content/di.ts b/src/content/di.ts index 025ff076..160af221 100644 --- a/src/content/di.ts +++ b/src/content/di.ts @@ -1,59 +1,7 @@ /* eslint-disable max-len */ -import { - AddonEnabledRepository, - AddonEnabledRepositoryImpl, -} from "./repositories/AddonEnabledRepository"; -import { - AddressRepository, - AddressRepositoryImpl, -} from "./repositories/AddressRepository"; -import { - ConsoleFramePresenter, - ConsoleFramePresenterImpl, -} from "./presenters/ConsoleFramePresenter"; -import { - FocusPresenter, - FocusPresenterImpl, -} from "./presenters/FocusPresenter"; -import { HintPresenter, HintPresenterImpl } from "./presenters/HintPresenter"; -import { - KeymapRepository, - KeymapRepositoryImpl, -} from "./repositories/KeymapRepository"; -import { - NavigationPresenter, - NavigationPresenterImpl, -} from "./presenters/NavigationPresenter"; -import { OperationClient, OperationClientImpl } from "./client/OperationClient"; -import { - ScrollPresenter, - ScrollPresenterImpl, -} from "./presenters/ScrollPresenter"; -import { SettingClient, SettingClientImpl } from "./client/SettingClient"; -import { - SettingRepository, - SettingRepositoryImpl, -} from "./repositories/SettingRepository"; -import { FindPresenter, FindPresenterImpl } from "./presenters/FindPresenter"; -import { - BackgroundKeyClient, - BackgroundKeyClientImpl, -} from "./client/BackgroundKeyClient"; -import { - ModeRepository, - ModeRepositoryImpl, -} from "./repositories/ModeRepository"; -import { TopFrameClient, TopFrameClientImpl } from "./client/TopFrameClient"; -import { - FrameIdRepository, - FrameIdRepositoryImpl, -} from "./repositories/FrameIdRepository"; -import { - ReadyStatusPresenter, - ReadyStatusPresenterImpl, -} from "./presenters/ReadyStatusPresenter"; import { Container } from "inversify"; +import { buildProviderModule } from "inversify-binding-decorators"; import { BackgroundMessageSender, newSender as newBackgroundMessageSender, @@ -63,25 +11,28 @@ import { newSender as newWindowMessageSender, } from "./client/WindowMessageSender"; +import "./client/BackgroundKeyClient"; +import "./client/OperationClient"; +import "./client/SettingClient"; +import "./client/TopFrameClient"; +import "./presenters/ConsoleFramePresenter"; +import "./presenters/FindPresenter"; +import "./presenters/FocusPresenter"; +import "./presenters/HintPresenter"; +import "./presenters/NavigationPresenter"; +import "./presenters/ReadyStatusPresenter"; +import "./presenters/ScrollPresenter"; +import "./repositories/AddonEnabledRepository"; +import "./repositories/AddressRepository"; +import "./repositories/FrameIdRepository"; +import "./repositories/KeymapRepository"; +import "./repositories/ModeRepository"; +import "./repositories/SettingRepository"; + const container = new Container({ autoBindInjectable: true }); -container.bind(AddonEnabledRepository).to(AddonEnabledRepositoryImpl); -container.bind(AddressRepository).to(AddressRepositoryImpl); -container.bind(ConsoleFramePresenter).to(ConsoleFramePresenterImpl); -container.bind(FocusPresenter).to(FocusPresenterImpl); -container.bind(HintPresenter).to(HintPresenterImpl); -container.bind(KeymapRepository).to(KeymapRepositoryImpl); -container.bind(NavigationPresenter).to(NavigationPresenterImpl); -container.bind(OperationClient).to(OperationClientImpl); -container.bind(ScrollPresenter).to(ScrollPresenterImpl); -container.bind(FindPresenter).to(FindPresenterImpl); -container.bind(BackgroundKeyClient).to(BackgroundKeyClientImpl); -container.bind(ModeRepository).to(ModeRepositoryImpl); -container.bind(TopFrameClient).to(TopFrameClientImpl); -container.bind(FrameIdRepository).to(FrameIdRepositoryImpl); -container.bind(ReadyStatusPresenter).to(ReadyStatusPresenterImpl); -container.bind(SettingClient).to(SettingClientImpl); -container.bind(SettingRepository).to(SettingRepositoryImpl); +container.load(buildProviderModule()); + container .bind(BackgroundMessageSender) .toConstantValue(newBackgroundMessageSender()); diff --git a/src/content/presenters/ConsoleFramePresenter.ts b/src/content/presenters/ConsoleFramePresenter.ts index b7dc5ce6..a063711d 100644 --- a/src/content/presenters/ConsoleFramePresenter.ts +++ b/src/content/presenters/ConsoleFramePresenter.ts @@ -1,4 +1,4 @@ -import { injectable } from "inversify"; +import { provide } from "inversify-binding-decorators"; export interface ConsoleFramePresenter { attach(): void; @@ -28,7 +28,7 @@ const FRAME_STYLES = { export const ConsoleFramePresenter = Symbol("ConsoleFramePresenter"); -@injectable() +@provide(ConsoleFramePresenter) export class ConsoleFramePresenterImpl implements ConsoleFramePresenter { private static readonly IframeId = "vimmatic-console-frame" as const; diff --git a/src/content/presenters/FindPresenter.ts b/src/content/presenters/FindPresenter.ts index 85840740..b34fc9e7 100644 --- a/src/content/presenters/FindPresenter.ts +++ b/src/content/presenters/FindPresenter.ts @@ -1,4 +1,4 @@ -import { injectable } from "inversify"; +import { provide } from "inversify-binding-decorators"; import type { FindQuery } from "../../shared/findQuery"; export interface FindPresenter { @@ -23,7 +23,7 @@ let textGroups: Array> | undefined; export const FindPresenter = Symbol("FindPresenter"); -@injectable() +@provide(FindPresenter) export class FindPresenterImpl implements FindPresenter { findNext({ keyword, mode, ignoreCase }: FindQuery): boolean { this.initFinder({ keyword, mode, ignoreCase }); diff --git a/src/content/presenters/FocusPresenter.ts b/src/content/presenters/FocusPresenter.ts index 23a86e86..742022c4 100644 --- a/src/content/presenters/FocusPresenter.ts +++ b/src/content/presenters/FocusPresenter.ts @@ -1,4 +1,4 @@ -import { injectable } from "inversify"; +import { provide } from "inversify-binding-decorators"; export interface FocusPresenter { focusFirstElement(): boolean; @@ -6,7 +6,7 @@ export interface FocusPresenter { export const FocusPresenter = Symbol("FocusPresenter"); -@injectable() +@provide(FocusPresenter) export class FocusPresenterImpl implements FocusPresenter { focusFirstElement(): boolean { const inputTypes = ["email", "number", "search", "tel", "text", "url"]; diff --git a/src/content/presenters/HintPresenter.ts b/src/content/presenters/HintPresenter.ts index 40f60699..1c18b8f0 100644 --- a/src/content/presenters/HintPresenter.ts +++ b/src/content/presenters/HintPresenter.ts @@ -1,4 +1,5 @@ -import { inject, injectable } from "inversify"; +import { inject } from "inversify"; +import { provide } from "inversify-binding-decorators"; import { SettingRepository } from "../repositories/SettingRepository"; import { Hint } from "./Hint"; import * as doms from "../../shared/utils/dom"; @@ -82,7 +83,7 @@ export interface HintPresenter { export const HintPresenter = Symbol("HintPresenter"); -@injectable() +@provide(HintPresenter) export class HintPresenterImpl implements HintPresenter { constructor( @inject(SettingRepository) diff --git a/src/content/presenters/NavigationPresenter.ts b/src/content/presenters/NavigationPresenter.ts index e976c4cd..7c84d82c 100644 --- a/src/content/presenters/NavigationPresenter.ts +++ b/src/content/presenters/NavigationPresenter.ts @@ -1,4 +1,4 @@ -import { injectable } from "inversify"; +import { provide } from "inversify-binding-decorators"; export interface NavigationPresenter { openHistoryPrev(): void; @@ -34,7 +34,7 @@ function selectLast( export const NavigationPresenter = Symbol("NavigationPresenter"); -@injectable() +@provide(NavigationPresenter) export class NavigationPresenterImpl implements NavigationPresenter { openHistoryPrev(): void { window.history.back(); diff --git a/src/content/presenters/ReadyStatusPresenter.ts b/src/content/presenters/ReadyStatusPresenter.ts index a288fe91..5c81d92c 100644 --- a/src/content/presenters/ReadyStatusPresenter.ts +++ b/src/content/presenters/ReadyStatusPresenter.ts @@ -1,4 +1,4 @@ -import { injectable } from "inversify"; +import { provide } from "inversify-binding-decorators"; export interface ReadyStatusPresenter { setContentReady(): void; @@ -8,7 +8,7 @@ export interface ReadyStatusPresenter { export const ReadyStatusPresenter = Symbol("ReadyStatusPresenter"); -@injectable() +@provide(ReadyStatusPresenter) export class ReadyStatusPresenterImpl { constructor(private readonly doc: Document = window.document) {} setContentReady() { diff --git a/src/content/presenters/ScrollPresenter.ts b/src/content/presenters/ScrollPresenter.ts index 6b49a648..7df74d45 100644 --- a/src/content/presenters/ScrollPresenter.ts +++ b/src/content/presenters/ScrollPresenter.ts @@ -1,4 +1,4 @@ -import { injectable } from "inversify"; +import { provide } from "inversify-binding-decorators"; const SCROLL_DELTA_X = 64; const SCROLL_DELTA_Y = 64; @@ -130,7 +130,7 @@ export interface ScrollPresenter { export const ScrollPresenter = Symbol("ScrollPresenter"); -@injectable() +@provide(ScrollPresenter) export class ScrollPresenterImpl implements ScrollPresenter { getScroll(): Point { const target = scrollTarget(); diff --git a/src/content/repositories/AddonEnabledRepository.ts b/src/content/repositories/AddonEnabledRepository.ts index e196c1a0..f6db0193 100644 --- a/src/content/repositories/AddonEnabledRepository.ts +++ b/src/content/repositories/AddonEnabledRepository.ts @@ -1,4 +1,4 @@ -import { injectable } from "inversify"; +import { provide } from "inversify-binding-decorators"; let enabled = false; @@ -10,7 +10,7 @@ export interface AddonEnabledRepository { export const AddonEnabledRepository = Symbol("AddonEnabledRepository"); -@injectable() +@provide(AddonEnabledRepository) export class AddonEnabledRepositoryImpl implements AddonEnabledRepository { set(on: boolean): void { enabled = on; diff --git a/src/content/repositories/AddressRepository.ts b/src/content/repositories/AddressRepository.ts index 905eadfc..f2fc1680 100644 --- a/src/content/repositories/AddressRepository.ts +++ b/src/content/repositories/AddressRepository.ts @@ -1,4 +1,4 @@ -import { injectable } from "inversify"; +import { provide } from "inversify-binding-decorators"; export interface AddressRepository { getCurrentURL(): URL; @@ -6,7 +6,7 @@ export interface AddressRepository { export const AddressRepository = Symbol("AddressRepository"); -@injectable() +@provide(AddressRepository) export class AddressRepositoryImpl implements AddressRepository { getCurrentURL(): URL { return new URL(window.location.href); diff --git a/src/content/repositories/FrameIdRepository.ts b/src/content/repositories/FrameIdRepository.ts index f3f426de..6c53f033 100644 --- a/src/content/repositories/FrameIdRepository.ts +++ b/src/content/repositories/FrameIdRepository.ts @@ -1,4 +1,4 @@ -import { injectable } from "inversify"; +import { provide } from "inversify-binding-decorators"; export interface FrameIdRepository { saveFrameId(frameId: number, target: Window, element: Element): void; @@ -13,7 +13,7 @@ const elements: { [frameId: number]: Element } = {}; export const FrameIdRepository = Symbol("FrameIdRepository"); -@injectable() +@provide(FrameIdRepository) export class FrameIdRepositoryImpl implements FrameIdRepository { saveFrameId(frameId: number, target: Window, element: Element): void { targets[frameId] = target; diff --git a/src/content/repositories/KeymapRepository.ts b/src/content/repositories/KeymapRepository.ts index 5c95529a..6aae9a4b 100644 --- a/src/content/repositories/KeymapRepository.ts +++ b/src/content/repositories/KeymapRepository.ts @@ -1,4 +1,4 @@ -import { injectable } from "inversify"; +import { provide } from "inversify-binding-decorators"; import type { Key } from "../../shared/key"; import { KeySequence } from "../domains/KeySequence"; @@ -12,7 +12,7 @@ let current: KeySequence = new KeySequence([]); export const KeymapRepository = Symbol("KeymapRepository"); -@injectable() +@provide(KeymapRepository) export class KeymapRepositoryImpl implements KeymapRepository { enqueueKey(key: Key): KeySequence { current.push(key); diff --git a/src/content/repositories/ModeRepository.ts b/src/content/repositories/ModeRepository.ts index 872a45d8..88aaaf4e 100644 --- a/src/content/repositories/ModeRepository.ts +++ b/src/content/repositories/ModeRepository.ts @@ -1,4 +1,4 @@ -import { injectable } from "inversify"; +import { provide } from "inversify-binding-decorators"; import { Mode } from "../../shared/mode"; let mode: Mode = Mode.Normal; @@ -11,7 +11,7 @@ export interface ModeRepository { export const ModeRepository = Symbol("ModeRepository"); -@injectable() +@provide(ModeRepository) export class ModeRepositoryImpl implements ModeRepository { getMode(): Mode { return mode; diff --git a/src/content/repositories/SettingRepository.ts b/src/content/repositories/SettingRepository.ts index d9bdccf5..df8a9d3c 100644 --- a/src/content/repositories/SettingRepository.ts +++ b/src/content/repositories/SettingRepository.ts @@ -1,4 +1,5 @@ -import { injectable, inject } from "inversify"; +import { inject } from "inversify"; +import { provide } from "inversify-binding-decorators"; import { Blacklist } from "../../shared/blacklist"; import type { Keymaps } from "../../shared/keymaps"; import type { Properties } from "../../shared/properties"; @@ -26,7 +27,7 @@ export interface SettingRepository { export const SettingRepository = Symbol("SettingRepository"); -@injectable() +@provide(SettingRepository) export class SettingRepositoryImpl implements SettingRepository { constructor( @inject(SettingClient)