Skip to content

Commit

Permalink
Migrated favoritesUpdated event to BrowserWindowNotifier
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Feb 11, 2024
1 parent 3e9309a commit bf34db7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 26 deletions.
2 changes: 0 additions & 2 deletions src/main/Core/BrowserWindow/BrowserWindowModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ export class BrowserWindowModule {
sendToBrowserWindow(browserWindow, "excludedSearchResultItemsUpdated"),
);

eventSubscriber.subscribe("favoritesUpdated", () => sendToBrowserWindow(browserWindow, "favoritesUpdated"));

BrowserWindowModule.registerUeliCommandEvents(browserWindow, eventSubscriber);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface BrowserWindowNotifier {
notify<T>(channel: string, data?: T);
notify<T>(channel: string, data?: T): void;
}
18 changes: 9 additions & 9 deletions src/main/Core/FavoriteManager/FavoriteManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventEmitter } from "@Core/EventEmitter";
import type { BrowserWindowNotifier } from "@Core/BrowserWindowNotifier";
import type { SettingsManager } from "@Core/SettingsManager/SettingsManager";
import { describe, expect, it, vi } from "vitest";
import { FavoriteManager } from "./FavoriteManager";
Expand All @@ -10,7 +10,7 @@ describe(FavoriteManager, () => {
const getValueMock = vi.fn().mockReturnValue(favorites);
const settingsManager = <SettingsManager>{ getValue: (k, d) => getValueMock(k, d) };

const favoriteManager = new FavoriteManager(settingsManager, <EventEmitter>{});
const favoriteManager = new FavoriteManager(settingsManager, <BrowserWindowNotifier>{});

expect(favoriteManager.favorites).toBe(favorites);
expect(favoriteManager.getAll()).toEqual(Object.values(favorites));
Expand All @@ -26,17 +26,17 @@ describe(FavoriteManager, () => {
updateValue: (k, v) => updateValueMock(k, v),
};

const emitEventMock = vi.fn();
const eventEmitter = <EventEmitter>{ emitEvent: (e) => emitEventMock(e) };
const notifyMock = vi.fn();
const browserWindowNotifier = <BrowserWindowNotifier>{ notify: (e) => notifyMock(e) };

const favoriteManager = new FavoriteManager(settingsManager, eventEmitter);
const favoriteManager = new FavoriteManager(settingsManager, browserWindowNotifier);

await favoriteManager.add("id_1");

expect(favoriteManager.favorites).toEqual(["id_1"]);
expect(getValueMock).toHaveBeenCalledWith("favorites", []);
expect(updateValueMock).toHaveBeenCalledWith("favorites", ["id_1"]);
expect(emitEventMock).toHaveBeenCalledWith("favoritesUpdated");
expect(notifyMock).toHaveBeenCalledWith("favoritesUpdated");
});

it("should remove a favorite", async () => {
Expand All @@ -48,8 +48,8 @@ describe(FavoriteManager, () => {
updateValue: (k, v) => updateValueMock(k, v),
};

const emitEventMock = vi.fn();
const eventEmitter = <EventEmitter>{ emitEvent: (e) => emitEventMock(e) };
const notifyMock = vi.fn();
const eventEmitter = <BrowserWindowNotifier>{ notify: (c) => notifyMock(c) };

const favoriteManager = new FavoriteManager(settingsManager, eventEmitter);

Expand All @@ -58,6 +58,6 @@ describe(FavoriteManager, () => {
expect(favoriteManager.favorites).toEqual([]);
expect(getValueMock).toHaveBeenCalledWith("favorites", []);
expect(updateValueMock).toHaveBeenCalledWith("favorites", []);
expect(emitEventMock).toHaveBeenCalledWith("favoritesUpdated");
expect(notifyMock).toHaveBeenCalledWith("favoritesUpdated");
});
});
8 changes: 4 additions & 4 deletions src/main/Core/FavoriteManager/FavoriteManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventEmitter } from "@Core/EventEmitter";
import type { BrowserWindowNotifier } from "@Core/BrowserWindowNotifier";
import type { SettingsManager } from "@Core/SettingsManager";
import type { FavoriteManager as FavoriteManagerInterface } from "./Contract";

Expand All @@ -9,7 +9,7 @@ export class FavoriteManager implements FavoriteManagerInterface {

public constructor(
private readonly settingsManager: SettingsManager,
private readonly eventEmitter: EventEmitter,
private readonly browserWindowNotifier: BrowserWindowNotifier,
) {
this.favorites = settingsManager.getValue<string[]>(this.settingKey, []);
}
Expand All @@ -21,14 +21,14 @@ export class FavoriteManager implements FavoriteManagerInterface {

this.favorites.push(id);
await this.saveChanges();
this.eventEmitter.emitEvent("favoritesUpdated");
this.browserWindowNotifier.notify("favoritesUpdated");
}

public async remove(id: string): Promise<void> {
const indexToDelete = this.favorites.findIndex((f) => f === id);
this.favorites.splice(indexToDelete, 1);
await this.saveChanges();
this.eventEmitter.emitEvent("favoritesUpdated");
this.browserWindowNotifier.notify("favoritesUpdated");
}

public getAll(): string[] {
Expand Down
17 changes: 7 additions & 10 deletions src/main/Core/FavoriteManager/FavoriteManagerModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import { FavoriteManager } from "./FavoriteManager";

export class FavoriteManagerModule {
public static bootstrap(dependencyRegistry: DependencyRegistry<Dependencies>) {
const settingsManager = dependencyRegistry.get("SettingsManager");
const ipcMain = dependencyRegistry.get("IpcMain");
const eventEmitter = dependencyRegistry.get("EventEmitter");

const f = new FavoriteManager(settingsManager, eventEmitter);
const favoriteManager = new FavoriteManager(
dependencyRegistry.get("SettingsManager"),
dependencyRegistry.get("BrowserWindowNotifier"),
);

dependencyRegistry.register("FavoriteManager", f);
dependencyRegistry.register("FavoriteManager", favoriteManager);

ipcMain.on("getFavorites", (event) => {
event.returnValue = f.getAll();
});
ipcMain.on("getFavorites", (event) => (event.returnValue = favoriteManager.getAll()));

ipcMain.handle("removeFavorite", async (_, { id }: { id: string }) => {
await f.remove(id);
});
ipcMain.handle("removeFavorite", async (_, { id }: { id: string }) => await favoriteManager.remove(id));
}
}

0 comments on commit bf34db7

Please sign in to comment.