|
| 1 | +import type { BrowserWindow } from 'electron' |
| 2 | + |
| 3 | +import type { createRequestWindowEventa, RequestWindowPayload } from '../../../shared/eventa' |
| 4 | + |
| 5 | +import { defineInvokeHandler } from '@moeru/eventa' |
| 6 | +import { createContext } from '@moeru/eventa/adapters/electron/main' |
| 7 | +import { ipcMain } from 'electron' |
| 8 | + |
| 9 | +export interface ReferencedWindowHandle { |
| 10 | + id: string |
| 11 | + window: BrowserWindow |
| 12 | + context: ReturnType<typeof createContext>['context'] |
| 13 | + eventa: ReturnType<typeof createRequestWindowEventa> |
| 14 | +} |
| 15 | + |
| 16 | +export interface ReferencedWindowManager<Payload extends RequestWindowPayload = RequestWindowPayload> { |
| 17 | + open: (payload: Payload & { id?: string }) => Promise<ReferencedWindowHandle> |
| 18 | + close: (id: string) => void |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Minimal per-id window manager used by notice/widgets-like windows. |
| 23 | + * It opens (or reuses) a window, loads the route with the id in query, and returns the window/context so |
| 24 | + * callers can register their own action handlers. |
| 25 | + */ |
| 26 | +export function createReferencedWindowManager<Payload extends RequestWindowPayload = RequestWindowPayload>(params: { |
| 27 | + eventa: ReturnType<typeof createRequestWindowEventa> |
| 28 | + createWindow: (id: string) => BrowserWindow |
| 29 | + loadRoute: (window: BrowserWindow, payload: Payload & { id: string }) => Promise<void> |
| 30 | +}): ReferencedWindowManager<Payload> { |
| 31 | + const windows = new Map<string, { window: BrowserWindow, context: ReturnType<typeof createContext>['context'] }>() |
| 32 | + |
| 33 | + function bindContext(id: string, payload: Payload, win: BrowserWindow) { |
| 34 | + // TODO: once we refactored eventa to support window-namespaced contexts, |
| 35 | + // we can remove the setMaxListeners call below since eventa will be able to dispatch and |
| 36 | + // manage events within eventa's context system. |
| 37 | + ipcMain.setMaxListeners(0) |
| 38 | + const { context } = createContext(ipcMain, win) |
| 39 | + |
| 40 | + defineInvokeHandler(context, params.eventa.pageMounted, (req) => { |
| 41 | + if (req?.id && req.id !== id) |
| 42 | + return undefined |
| 43 | + return { id, type: payload.type, payload: payload.payload } |
| 44 | + }) |
| 45 | + |
| 46 | + defineInvokeHandler(context, params.eventa.pageUnmounted, (req) => { |
| 47 | + if (req?.id && req.id !== id) |
| 48 | + return |
| 49 | + windows.delete(id) |
| 50 | + }) |
| 51 | + |
| 52 | + win.on('closed', () => windows.delete(id)) |
| 53 | + |
| 54 | + return { window: win, context } |
| 55 | + } |
| 56 | + |
| 57 | + async function open(payload: Payload & { id?: string }): Promise<ReferencedWindowHandle> { |
| 58 | + const id = payload.id ?? Math.random().toString(36).slice(2, 10) |
| 59 | + let ctx = windows.get(id) |
| 60 | + |
| 61 | + if (!ctx || ctx.window.isDestroyed()) { |
| 62 | + const win = params.createWindow(id) |
| 63 | + ctx = bindContext(id, payload, win) |
| 64 | + windows.set(id, ctx) |
| 65 | + } |
| 66 | + |
| 67 | + try { |
| 68 | + await params.loadRoute(ctx.window, { ...payload, id }) |
| 69 | + ctx.window.show() |
| 70 | + ctx.window.focus() |
| 71 | + } |
| 72 | + catch (error) { |
| 73 | + const wrapped = error ?? new Error('Failed to open referenced window') |
| 74 | + console.error('[referenced-window] open failed', wrapped) |
| 75 | + throw wrapped |
| 76 | + } |
| 77 | + |
| 78 | + return { id, window: ctx.window, context: ctx.context, eventa: params.eventa } |
| 79 | + } |
| 80 | + |
| 81 | + function close(id: string) { |
| 82 | + const ctx = windows.get(id) |
| 83 | + if (!ctx) |
| 84 | + return |
| 85 | + if (!ctx.window.isDestroyed()) |
| 86 | + ctx.window.close() |
| 87 | + windows.delete(id) |
| 88 | + } |
| 89 | + |
| 90 | + return { open, close } |
| 91 | +} |
0 commit comments