UNPKG

backsplash-app

Version:
57 lines (44 loc) 2 kB
import { ipcMain } from "electron"; import { ServerChannels } from "@/ipc/channels/serverChannels"; import { getApiService } from "@/ipc/services/apiServiceInstance"; import { StoreService } from "@/ipc/services/storeService"; // Track if handlers have been initialized to prevent duplicates let serverHandlersInitialized = false; /** * Clean up existing server IPC handlers */ export const cleanupServerIpcHandlers = () => { try { // Remove all server-related handlers ipcMain.removeHandler(ServerChannels.SUBMIT_FEEDBACK); ipcMain.removeHandler(ServerChannels.GET_WALLPAPERS); ipcMain.removeHandler(ServerChannels.GET_CATEGORIES); ipcMain.removeHandler(ServerChannels.GET_CATEGORY); serverHandlersInitialized = false; console.log("[Server IPC] Cleaned up existing IPC handlers"); } catch (error) { // Ignore errors if handlers don't exist console.debug("[Server IPC] No existing handlers to clean up"); } }; export const registerServerIpc = (storeService: StoreService) => { // Prevent duplicate handler registration if (serverHandlersInitialized) { console.warn("[Server IPC] Handlers already initialized, skipping registration..."); return; } // Clean up any existing handlers first cleanupServerIpcHandlers(); const apiService = getApiService(storeService); // Feedback handlers ipcMain.handle(ServerChannels.SUBMIT_FEEDBACK, (event, content, userId, rating, category) => apiService.feedback.submit(content, userId, rating, category), ); // Wallpaper handlers ipcMain.handle(ServerChannels.GET_WALLPAPERS, (_event, nextToken) => apiService.wallpapers.getMany(nextToken)); // Category handlers ipcMain.handle(ServerChannels.GET_CATEGORIES, () => apiService.categories.getMany()); ipcMain.handle(ServerChannels.GET_CATEGORY, (_event, categoryId) => apiService.categories.get(categoryId)); serverHandlersInitialized = true; console.log("[Server IPC] All handlers registered successfully"); };