Skip to content

Commit

Permalink
use provider module of inversify in content scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ueokande committed Jul 21, 2024
1 parent 3e119c3 commit c4d44e8
Show file tree
Hide file tree
Showing 18 changed files with 61 additions and 104 deletions.
5 changes: 3 additions & 2 deletions src/content/client/BackgroundKeyClient.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -8,7 +9,7 @@ export interface BackgroundKeyClient {

export const BackgroundKeyClient = Symbol("BackgroundKeyClient");

@injectable()
@provide(BackgroundKeyClient)
export class BackgroundKeyClientImpl implements BackgroundKeyClient {
constructor(
@inject(BackgroundMessageSender)
Expand Down
5 changes: 3 additions & 2 deletions src/content/client/OperationClient.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -8,7 +9,7 @@ export interface OperationClient {

export const OperationClient = Symbol("OperationClient");

@injectable()
@provide(OperationClient)
export class OperationClientImpl implements OperationClient {
constructor(
@inject(BackgroundMessageSender)
Expand Down
5 changes: 3 additions & 2 deletions src/content/client/SettingClient.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -9,7 +10,7 @@ export interface SettingClient {

export const SettingClient = Symbol("SettingClient");

@injectable()
@provide(SettingClient)
export class SettingClientImpl {
constructor(
@inject(BackgroundMessageSender)
Expand Down
5 changes: 3 additions & 2 deletions src/content/client/TopFrameClient.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -7,7 +8,7 @@ export interface TopFrameClient {

export const TopFrameClient = Symbol("TopFrameClient");

@injectable()
@provide(TopFrameClient)
export class TopFrameClientImpl implements TopFrameClient {
constructor(
@inject(WindowMessageSender)
Expand Down
91 changes: 21 additions & 70 deletions src/content/di.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/content/presenters/ConsoleFramePresenter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectable } from "inversify";
import { provide } from "inversify-binding-decorators";

export interface ConsoleFramePresenter {
attach(): void;
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/content/presenters/FindPresenter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectable } from "inversify";
import { provide } from "inversify-binding-decorators";
import type { FindQuery } from "../../shared/findQuery";

export interface FindPresenter {
Expand All @@ -23,7 +23,7 @@ let textGroups: Array<Array<Text>> | 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 });
Expand Down
4 changes: 2 additions & 2 deletions src/content/presenters/FocusPresenter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { injectable } from "inversify";
import { provide } from "inversify-binding-decorators";

export interface FocusPresenter {
focusFirstElement(): boolean;
}

export const FocusPresenter = Symbol("FocusPresenter");

@injectable()
@provide(FocusPresenter)
export class FocusPresenterImpl implements FocusPresenter {
focusFirstElement(): boolean {
const inputTypes = ["email", "number", "search", "tel", "text", "url"];
Expand Down
5 changes: 3 additions & 2 deletions src/content/presenters/HintPresenter.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -82,7 +83,7 @@ export interface HintPresenter {

export const HintPresenter = Symbol("HintPresenter");

@injectable()
@provide(HintPresenter)
export class HintPresenterImpl implements HintPresenter {
constructor(
@inject(SettingRepository)
Expand Down
4 changes: 2 additions & 2 deletions src/content/presenters/NavigationPresenter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectable } from "inversify";
import { provide } from "inversify-binding-decorators";

export interface NavigationPresenter {
openHistoryPrev(): void;
Expand Down Expand Up @@ -34,7 +34,7 @@ function selectLast<E extends Element>(

export const NavigationPresenter = Symbol("NavigationPresenter");

@injectable()
@provide(NavigationPresenter)
export class NavigationPresenterImpl implements NavigationPresenter {
openHistoryPrev(): void {
window.history.back();
Expand Down
4 changes: 2 additions & 2 deletions src/content/presenters/ReadyStatusPresenter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectable } from "inversify";
import { provide } from "inversify-binding-decorators";

export interface ReadyStatusPresenter {
setContentReady(): void;
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/content/presenters/ScrollPresenter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectable } from "inversify";
import { provide } from "inversify-binding-decorators";

const SCROLL_DELTA_X = 64;
const SCROLL_DELTA_Y = 64;
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/content/repositories/AddonEnabledRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectable } from "inversify";
import { provide } from "inversify-binding-decorators";

let enabled = false;

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/content/repositories/AddressRepository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { injectable } from "inversify";
import { provide } from "inversify-binding-decorators";

export interface AddressRepository {
getCurrentURL(): URL;
}

export const AddressRepository = Symbol("AddressRepository");

@injectable()
@provide(AddressRepository)
export class AddressRepositoryImpl implements AddressRepository {
getCurrentURL(): URL {
return new URL(window.location.href);
Expand Down
4 changes: 2 additions & 2 deletions src/content/repositories/FrameIdRepository.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/content/repositories/KeymapRepository.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/content/repositories/ModeRepository.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectable } from "inversify";
import { provide } from "inversify-binding-decorators";
import { Mode } from "../../shared/mode";

let mode: Mode = Mode.Normal;
Expand All @@ -11,7 +11,7 @@ export interface ModeRepository {

export const ModeRepository = Symbol("ModeRepository");

@injectable()
@provide(ModeRepository)
export class ModeRepositoryImpl implements ModeRepository {
getMode(): Mode {
return mode;
Expand Down
5 changes: 3 additions & 2 deletions src/content/repositories/SettingRepository.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -26,7 +27,7 @@ export interface SettingRepository {

export const SettingRepository = Symbol("SettingRepository");

@injectable()
@provide(SettingRepository)
export class SettingRepositoryImpl implements SettingRepository {
constructor(
@inject(SettingClient)
Expand Down

0 comments on commit c4d44e8

Please sign in to comment.