From 08bff81bf342de6508fc2e2af36bd6b32e49248d Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 23 Mar 2021 11:04:08 +0200 Subject: [PATCH] extract ipc renderer navigation events to const enum to avoid names duplication Signed-off-by: Roman --- src/main/window-manager.ts | 15 +++++++++------ src/renderer/navigation/events.ts | 15 +++++++++++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/main/window-manager.ts b/src/main/window-manager.ts index 0b7272331e..afd6b03670 100644 --- a/src/main/window-manager.ts +++ b/src/main/window-manager.ts @@ -8,6 +8,7 @@ import { initMenu } from "./menu"; import { initTray } from "./tray"; import { Singleton } from "../common/utils"; import { ClusterFrameInfo, clusterFrameMap } from "../common/cluster-frames"; +import { IpcRendererNavigationEvents } from "../renderer/navigation/events"; import logger from "./logger"; export class WindowManager extends Singleton { @@ -65,13 +66,13 @@ export class WindowManager extends Singleton { shell.openExternal(url); }); this.mainWindow.webContents.on("dom-ready", () => { - appEventBus.emit({name: "app", action: "dom-ready"}); + appEventBus.emit({ name: "app", action: "dom-ready" }); }); this.mainWindow.on("focus", () => { - appEventBus.emit({name: "app", action: "focus"}); + appEventBus.emit({ name: "app", action: "focus" }); }); this.mainWindow.on("blur", () => { - appEventBus.emit({name: "app", action: "blur"}); + appEventBus.emit({ name: "app", action: "blur" }); }); // clean up @@ -115,7 +116,7 @@ export class WindowManager extends Singleton { protected bindEvents() { // track visible cluster from ui - subscribeToBroadcast("cluster-view:current-id", (event, clusterId: ClusterId) => { + subscribeToBroadcast(IpcRendererNavigationEvents.CLUSTER_VIEW_CURRENT_ID, (event, clusterId: ClusterId) => { this.activeClusterId = clusterId; }); } @@ -139,7 +140,9 @@ export class WindowManager extends Singleton { await this.ensureMainWindow(); const frameInfo = Array.from(clusterFrameMap.values()).find((frameInfo) => frameInfo.frameId === frameId); - const channel = frameInfo ? "renderer:navigate-in-cluster" : "renderer:navigate"; + const channel = frameInfo + ? IpcRendererNavigationEvents.NAVIGATE_IN_CLUSTER + : IpcRendererNavigationEvents.NAVIGATE_IN_APP; this.sendToView({ channel, @@ -152,7 +155,7 @@ export class WindowManager extends Singleton { const frameInfo = clusterFrameMap.get(this.activeClusterId); if (frameInfo) { - this.sendToView({ channel: "renderer:reload", frameInfo }); + this.sendToView({ channel: IpcRendererNavigationEvents.RELOAD_PAGE, frameInfo }); } else { webContents.getFocusedWebContents()?.reload(); } diff --git a/src/renderer/navigation/events.ts b/src/renderer/navigation/events.ts index 12805123f9..6ae0107374 100644 --- a/src/renderer/navigation/events.ts +++ b/src/renderer/navigation/events.ts @@ -4,6 +4,13 @@ import { getMatchedClusterId, navigate } from "./helpers"; import { broadcastMessage, subscribeToBroadcast } from "../../common/ipc"; import logger from "../../main/logger"; +export const enum IpcRendererNavigationEvents { + RELOAD_PAGE = "renderer:page-reload", + CLUSTER_VIEW_CURRENT_ID = "renderer:cluster-id-of-active-view", + NAVIGATE_IN_APP = "renderer:navigate", + NAVIGATE_IN_CLUSTER = "renderer:navigate-in-cluster", +} + export function bindEvents() { if (!ipcRenderer) { return; @@ -16,7 +23,7 @@ export function bindEvents() { } // Reload dashboard window - subscribeToBroadcast("renderer:reload", () => { + subscribeToBroadcast(IpcRendererNavigationEvents.RELOAD_PAGE, () => { location.reload(); }); } @@ -25,13 +32,13 @@ export function bindEvents() { function bindClusterManagerRouteEvents() { // Keep track of active cluster-id for handling IPC/menus/etc. reaction(() => getMatchedClusterId(), clusterId => { - broadcastMessage("cluster-view:current-id", clusterId); + broadcastMessage(IpcRendererNavigationEvents.CLUSTER_VIEW_CURRENT_ID, clusterId); }, { fireImmediately: true }); // Handle navigation via IPC - subscribeToBroadcast("renderer:navigate", (event, url: string) => { + subscribeToBroadcast(IpcRendererNavigationEvents.NAVIGATE_IN_APP, (event, url: string) => { logger.info(`[IPC]: ${event.type}: ${url}`, { currentLocation: location.href }); navigate(url); }); @@ -39,7 +46,7 @@ function bindClusterManagerRouteEvents() { // Handle cluster-view renderer process events within iframes function bindClusterFrameRouteEvents() { - subscribeToBroadcast("renderer:navigate-in-cluster", (event, url: string) => { + subscribeToBroadcast(IpcRendererNavigationEvents.NAVIGATE_IN_CLUSTER, (event, url: string) => { logger.info(`[IPC]: ${event.type}: ${url}`, { currentLocation: location.href }); navigate(url); });