Skip to content

Commit

Permalink
use provider module of inversify in background script
Browse files Browse the repository at this point in the history
  • Loading branch information
ueokande committed Jul 21, 2024
1 parent 5a32ab4 commit 1408526
Show file tree
Hide file tree
Showing 28 changed files with 91 additions and 164 deletions.
4 changes: 2 additions & 2 deletions src/background/clients/AddonEnabledClient.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 { newSender } from "./ContentMessageSender";

export interface AddonEnabledClient {
Expand All @@ -9,7 +9,7 @@ export interface AddonEnabledClient {

export const AddonEnabledClient = Symbol("AddonEnabledClient");

@injectable()
@provide(AddonEnabledClient)
export class AddonEnabledClientImpl implements AddonEnabledClient {
async enable(tabId: number): Promise<void> {
const sender = newSender(tabId);
Expand Down
4 changes: 2 additions & 2 deletions src/background/clients/ConsoleClient.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 { newSender } from "./ConsoleMessageSender";

export interface ConsoleClient {
Expand All @@ -15,7 +15,7 @@ export interface ConsoleClient {

export const ConsoleClient = Symbol("ConsoleClient");

@injectable()
@provide(ConsoleClient)
export class ConsoleClientImpl implements ConsoleClient {
async showCommand(tabId: number, command: string): Promise<void> {
const sender = newSender(tabId);
Expand Down
4 changes: 2 additions & 2 deletions src/background/clients/ConsoleFrameClient.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 { newSender } from "./ContentMessageSender";

export interface ConsoleFrameClient {
Expand All @@ -7,7 +7,7 @@ export interface ConsoleFrameClient {

export const ConsoleFrameClient = Symbol("ConsoleFrameClient");

@injectable()
@provide(ConsoleFrameClient)
export class ConsoleFrameClientImpl implements ConsoleFrameClient {
async resize(tabId: number, width: number, height: number): Promise<void> {
const sender = newSender(tabId);
Expand Down
4 changes: 2 additions & 2 deletions src/background/clients/ContentMessageClient.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 { newSender } from "./ContentMessageSender";

export interface ContentMessageClient {
Expand Down Expand Up @@ -42,7 +42,7 @@ export interface ContentMessageClient {

export const ContentMessageClient = Symbol("ContentMessageClient");

@injectable()
@provide(ContentMessageClient)
export class ContentMessageClientImpl implements ContentMessageClient {
async scrollTo(
tabId: number,
Expand Down
4 changes: 2 additions & 2 deletions src/background/clients/FindClient.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 { newSender } from "./ContentMessageSender";
import type { FindQuery } from "../../shared/findQuery";

Expand All @@ -12,7 +12,7 @@ export interface FindClient {

export const FindClient = Symbol("FindClient");

@injectable()
@provide(FindClient)
export class FindClientImpl implements FindClient {
async findNext(
tabId: number,
Expand Down
4 changes: 2 additions & 2 deletions src/background/clients/FrameClient.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 { newSender } from "./ContentMessageSender";

export interface FrameClient {
Expand All @@ -7,7 +7,7 @@ export interface FrameClient {

export const FrameClient = Symbol("FrameClient");

@injectable()
@provide(FrameClient)
export class FrameClientImpl implements FrameClient {
async notifyFrameId(tabId: number, frameId: number): Promise<void> {
const sender = newSender(tabId, frameId);
Expand Down
4 changes: 2 additions & 2 deletions src/background/clients/HintClient.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 { newSender } from "./ContentMessageSender";
import type { HTMLElementType } from "../../shared/HTMLElementType";

Expand Down Expand Up @@ -44,7 +44,7 @@ export interface HintClient {

export const HintClient = Symbol("HintClient");

@injectable()
@provide(HintClient)
export class HintClientImpl implements HintClient {
async lookupTargets(
tabId: number,
Expand Down
4 changes: 2 additions & 2 deletions src/background/clients/ModeClient.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 { newSender } from "./ContentMessageSender";
import type { Mode } from "../../shared/mode";

Expand All @@ -8,7 +8,7 @@ export interface ModeClient {

export const ModeClient = Symbol("ModeClient");

@injectable()
@provide(ModeClient)
export class ModeClientImpl implements ModeClient {
async setMode(tabId: number, mode: Mode): Promise<void> {
const sender = newSender(tabId);
Expand Down
4 changes: 2 additions & 2 deletions src/background/clients/NavigateClient.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 { newSender } from "./ContentMessageSender";

export interface NavigateClient {
Expand All @@ -13,7 +13,7 @@ export interface NavigateClient {

export const NavigateClient = Symbol("NavigateClient");

@injectable()
@provide(NavigateClient)
export class NavigateClientImpl implements NavigateClient {
async historyNext(tabId: number): Promise<void> {
const sender = newSender(tabId);
Expand Down
4 changes: 2 additions & 2 deletions src/background/clients/TopFrameClient.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 { newSender } from "./ContentMessageSender";

export type Rect = {
Expand All @@ -19,7 +19,7 @@ export interface TopFrameClient {

export const TopFrameClient = Symbol("TopFrameClient");

@injectable()
@provide(TopFrameClient)
export class TopFrameClientImpl implements TopFrameClient {
getWindowViewport(tabId: number): Promise<Rect> {
const sender = newSender(tabId, 0);
Expand Down
140 changes: 31 additions & 109 deletions src/background/di.ts
Original file line number Diff line number Diff line change
@@ -1,116 +1,55 @@
/* eslint-disable max-len */

import { Container } from "inversify";
import {
LastSelectedTabRepository,
LastSelectedTabRepositoryImpl,
} from "./repositories/LastSelectedTabRepository";
import { Notifier, NotifierImpl } from "./presenters/Notifier";
import {
ContentMessageClient,
ContentMessageClientImpl,
} from "./clients/ContentMessageClient";
import { NavigateClient, NavigateClientImpl } from "./clients/NavigateClient";
import { ConsoleClient, ConsoleClientImpl } from "./clients/ConsoleClient";
import { buildProviderModule } from "inversify-binding-decorators";
import {
BrowserSettingRepository,
FirefoxBrowserSettingRepositoryImpl,
ChromeBrowserSettingRepositoryImpl,
} from "./repositories/BrowserSettingRepository";
import {
RepeatRepository,
RepeatRepositoryImpl,
} from "./repositories/RepeatRepository";
import { FindClient, FindClientImpl } from "./clients/FindClient";
import {
ConsoleFrameClient,
ConsoleFrameClientImpl,
} from "./clients/ConsoleFrameClient";
import {
FindRepository,
FindRepositoryImpl,
} from "./repositories/FindRepository";
import {
FindHistoryRepository,
FindHistoryRepositoryImpl,
} from "./repositories/FindHistoryRepository";
import {
ReadyFrameRepository,
ReadyFrameRepositoryImpl,
} from "./repositories/ReadyFrameRepository";
import {
ClipboardRepository,
FirefoxClipboardRepositoryImpl,
ChromeClipboardRepositoryImpl,
} from "./repositories/ClipboardRepository";
import {
PropertySettings,
PropertySettingsImpl,
} from "./settings/PropertySettings";
import { StyleSettings, StyleSettingsImpl } from "./settings/StyleSettings";
import {
SearchEngineSettings,
SearchEngineSettingsImpl,
} from "./settings/SearchEngineSettings";
import {
ModeRepository,
ModeRepositoryImpl,
} from "./repositories/ModeRepository";
import {
SettingsRepository,
PermanentSettingsRepository,
TransientSettingsRepositoryImpl,
PermanentSettingsRepositoryImpl,
} from "./settings/SettingsRepository";
import { ModeClient, ModeClientImpl } from "./clients/ModeClient";
import {
MarkRepository,
MarkRepositoryImpl,
} from "./repositories/MarkRepository";
import { HintClient, HintClientImpl } from "./clients/HintClient";
import {
HintRepository,
HintRepositoryImpl,
} from "./repositories/HintRepository";
import { FrameClient, FrameClientImpl } from "./clients/FrameClient";
import { TopFrameClient, TopFrameClientImpl } from "./clients/TopFrameClient";
import {
AddonEnabledRepository,
AddonEnabledRepositoryImpl,
} from "./repositories/AddonEnabledRepository";
import {
AddonEnabledClient,
AddonEnabledClientImpl,
} from "./clients/AddonEnabledClient";
import {
ToolbarPresenter,
ToolbarPresenterImpl,
} from "./presenters/ToolbarPresenter";
import { TabPresenter, TabPresenterImpl } from "./presenters/TabPresenter";
import { createPropertyRegistry } from "./property";
import { PropertyRegistry } from "./property/PropertyRegistry";
import { CommandRegistryFactory } from "./command";
import { CommandRegistry } from "./command/CommandRegistry";
import { OperatorRegistryFactory } from "./operators";
import { OperatorRegistry } from "./operators/OperatorRegistry";
import {
HintActionFactory,
HintActionFactoryImpl,
} from "./hint/HintActionFactory";
import { PropertyRegistry } from "./property/PropertyRegistry";
import "./clients/AddonEnabledClient";
import "./clients/ConsoleClient";
import "./clients/ConsoleFrameClient";
import "./clients/ContentMessageClient";
import "./clients/FindClient";
import "./clients/FrameClient";
import "./clients/HintClient";
import "./clients/ModeClient";
import "./clients/NavigateClient";
import "./clients/TopFrameClient";
import "./hint/HintActionFactory";
import "./presenters/Notifier";
import "./presenters/TabPresenter";
import "./presenters/ToolbarPresenter";
import "./repositories/AddonEnabledRepository";
import "./repositories/FindHistoryRepository";
import "./repositories/FindRepository";
import "./repositories/HintRepository";
import "./repositories/LastSelectedTabRepository";
import "./repositories/MarkRepository";
import "./repositories/ModeRepository";
import "./repositories/ReadyFrameRepository";
import "./repositories/RepeatRepository";
import "./settings/PropertySettings";
import "./settings/SearchEngineSettings";
import "./settings/SettingsRepository";
import "./settings/StyleSettings";

const container = new Container({ autoBindInjectable: true });

container.bind(LastSelectedTabRepository).to(LastSelectedTabRepositoryImpl);
container.bind(Notifier).to(NotifierImpl);
container.bind(RepeatRepository).to(RepeatRepositoryImpl);
container.bind(FindRepository).to(FindRepositoryImpl);
container.bind(FindHistoryRepository).to(FindHistoryRepositoryImpl);
container.bind(FindClient).to(FindClientImpl);
container.bind(ContentMessageClient).to(ContentMessageClientImpl);
container.bind(NavigateClient).to(NavigateClientImpl);
container.bind(ConsoleClient).to(ConsoleClientImpl);
container.bind(ConsoleFrameClient).to(ConsoleFrameClientImpl);
container.bind(ReadyFrameRepository).to(ReadyFrameRepositoryImpl);
container.load(buildProviderModule());

if (process.env.BROWSER === "firefox") {
container.bind(ClipboardRepository).to(FirefoxClipboardRepositoryImpl);
container
Expand All @@ -122,23 +61,6 @@ if (process.env.BROWSER === "firefox") {
.bind(BrowserSettingRepository)
.to(ChromeBrowserSettingRepositoryImpl);
}
container.bind(PropertySettings).to(PropertySettingsImpl);
container.bind(StyleSettings).to(StyleSettingsImpl);
container.bind(SearchEngineSettings).to(SearchEngineSettingsImpl);
container.bind(ModeRepository).to(ModeRepositoryImpl);
container.bind(MarkRepository).to(MarkRepositoryImpl);
container.bind(ModeClient).to(ModeClientImpl);
container.bind(HintClient).to(HintClientImpl);
container.bind(HintRepository).to(HintRepositoryImpl);
container.bind(FrameClient).to(FrameClientImpl);
container.bind(TopFrameClient).to(TopFrameClientImpl);
container.bind(AddonEnabledRepository).to(AddonEnabledRepositoryImpl);
container.bind(AddonEnabledClient).to(AddonEnabledClientImpl);
container.bind(ToolbarPresenter).to(ToolbarPresenterImpl);
container.bind(TabPresenter).to(TabPresenterImpl);
container.bind(PermanentSettingsRepository).to(PermanentSettingsRepositoryImpl);
container.bind(SettingsRepository).to(TransientSettingsRepositoryImpl);
container.bind(HintActionFactory).to(HintActionFactoryImpl);
container.bind(PropertyRegistry).toConstantValue(createPropertyRegistry());
container
.bind(CommandRegistry)
Expand Down
5 changes: 3 additions & 2 deletions src/background/hint/HintActionFactory.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 { QuickHintAction } from "./QuickHintAction";
import { OpenImageHintAction } from "./OpenImageHintAction";
import { YankURLHintAction } from "./YankURLHintAction";
Expand All @@ -18,7 +19,7 @@ export interface HintActionFactory {

export const HintActionFactory = Symbol("HintActionFactory");

@injectable()
@provide(HintActionFactory)
export class HintActionFactoryImpl implements HintActionFactory {
constructor(
@inject(QuickHintAction)
Expand Down
4 changes: 2 additions & 2 deletions src/background/presenters/Notifier.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 NOTIFICATION_ID_UPDATE = "vimmatic-update";

Expand All @@ -8,7 +8,7 @@ export interface Notifier {

export const Notifier = Symbol("Notifier");

@injectable()
@provide(Notifier)
export class NotifierImpl implements NotifierImpl {
async notifyUpdated(version: string, onclick: () => void): Promise<void> {
const title = `Vimmatic ${version} has been installed`;
Expand Down
4 changes: 2 additions & 2 deletions src/background/presenters/TabPresenter.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 type Tab = chrome.tabs.Tab;

Expand All @@ -14,7 +14,7 @@ export interface TabPresenter {

export const TabPresenter = Symbol("TabPresenter");

@injectable()
@provide(TabPresenter)
export class TabPresenterImpl implements TabPresenter {
async openToTab(url: string, tabId: number): Promise<void> {
await chrome.tabs.update(tabId, { url });
Expand Down
4 changes: 2 additions & 2 deletions src/background/presenters/ToolbarPresenter.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 ToolbarPresenter {
setEnabled(enabled: boolean): Promise<void>;
Expand All @@ -8,7 +8,7 @@ export interface ToolbarPresenter {

export const ToolbarPresenter = Symbol("ToolbarPresenter");

@injectable()
@provide(ToolbarPresenter)
export class ToolbarPresenterImpl {
async setEnabled(enabled: boolean): Promise<void> {
const path = enabled
Expand Down
4 changes: 2 additions & 2 deletions src/background/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";
import { type LocalCache, LocalCacheImpl } from "../db/LocalStorage";

export interface AddonEnabledRepository {
Expand All @@ -21,7 +21,7 @@ const listeners: OnChangeListener[] = [];

export const AddonEnabledRepository = Symbol("AddonEnabledRepository");

@injectable()
@provide(AddonEnabledRepository)
export class AddonEnabledRepositoryImpl implements AddonEnabledRepository {
constructor(
private readonly cache: LocalCache<boolean> = new LocalCacheImpl<boolean>(
Expand Down
Loading

0 comments on commit 1408526

Please sign in to comment.