Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,15 @@ function registerIpcHandlers() {
openPetdexWindow();
});

ipcMain.on('pet:state-changed', (event) => {
if (settingsWindow === undefined
|| settingsWindow.isDestroyed()
|| event.sender.id !== settingsWindow.webContents.id) return;
const currentPetWindow = getCurrentPetWindow();
if (currentPetWindow === undefined) return;
currentPetWindow.webContents.send('pet:state-changed');
});

ipcMain.handle('pet:manual-dock', () => {
if (!EDGE_DOCKING_ENABLED) return;
const currentPetWindow = getCurrentPetWindow();
Expand Down
23 changes: 23 additions & 0 deletions electron/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,26 @@ describe('Electron process lifecycle', () => {
expect(settingsWindows).toHaveLength(2);
});
});

describe('renderer state synchronization', () => {
it('forwards state changes from settings to the current pet window only', async () => {
const openSettings = electron.ipcMain.handle.mock.calls
.find(([channel]) => channel === 'pet:open-settings')[1];
await openSettings();
const settings = electron.windows
.filter(({ options }) => options.title === 'Codex Pet Pause 设置')
.at(-1);
const currentPet = electron.windows
.filter(({ options, destroyed }) => options.transparent === true && !destroyed)
.at(-1);
const forwardState = electron.ipcMain.on.mock.calls
.find(([channel]) => channel === 'pet:state-changed')[1];

forwardState({ sender: settings.webContents });
expect(currentPet.webContents.send).toHaveBeenCalledWith('pet:state-changed');

currentPet.webContents.send.mockClear();
forwardState({ sender: currentPet.webContents });
expect(currentPet.webContents.send).not.toHaveBeenCalled();
});
});
6 changes: 6 additions & 0 deletions electron/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ contextBridge.exposeInMainWorld('petShell', {
ipcRenderer.on('pet:petdex-import', listener);
return () => ipcRenderer.removeListener('pet:petdex-import', listener);
},
notifyStateChanged: () => ipcRenderer.send('pet:state-changed'),
onStateChanged: (callback) => {
const listener = () => callback();
ipcRenderer.on('pet:state-changed', listener);
return () => ipcRenderer.removeListener('pet:state-changed', listener);
},
dockNow: () => ipcRenderer.invoke('pet:manual-dock'),
dragWindowTo: (x, y) => ipcRenderer.send('pet:drag-window', { x, y }),
showContextMenu: (x, y) => ipcRenderer.send('pet:show-context-menu', { x, y }),
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "codex-pet-pause",
"private": true,
"version": "0.2.5",
"version": "0.2.6",
"description": "A playful, local-first break reminder PWA with interactive and Codex-compatible pets.",
"license": "MIT",
"repository": {
Expand Down
32 changes: 32 additions & 0 deletions src/infrastructure/rendererSynchronization.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { afterEach, describe, expect, test, vi } from 'vitest';
import { createRendererSynchronization } from './rendererSynchronization';

describe('renderer synchronization', () => {
afterEach(() => {
delete window.petShell;
});

test('uses native Electron IPC when browser channels are unreliable', () => {
const notifyStateChanged = vi.fn();
const unsubscribe = vi.fn();
let receiveNative: (() => void) | undefined;
window.petShell = {
notifyStateChanged,
onStateChanged(callback) {
receiveNative = callback;
return unsubscribe;
},
};
const synchronization = createRendererSynchronization();
const listener = vi.fn();
synchronization.subscribe(listener);

synchronization.notify();
expect(notifyStateChanged).toHaveBeenCalledOnce();
receiveNative?.();
expect(listener).toHaveBeenCalledOnce();

synchronization.close();
expect(unsubscribe).toHaveBeenCalledOnce();
});
});
13 changes: 13 additions & 0 deletions src/infrastructure/rendererSynchronization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function createRendererSynchronization(): RendererSynchronization {
let closed = false;
let lastToken: string | undefined;
let channel: BroadcastChannel | undefined;
let unsubscribeNative = (): void => undefined;

try {
if (typeof globalThis.BroadcastChannel === 'function') {
Expand All @@ -57,6 +58,12 @@ export function createRendererSynchronization(): RendererSynchronization {
if (isSyncMessage(event.data)) receive(event.data.token);
};
}
try {
unsubscribeNative = window.petShell?.onStateChanged?.(() => receive())
?? (() => undefined);
} catch {
unsubscribeNative = () => undefined;
}

return {
notify(): void {
Expand All @@ -73,6 +80,11 @@ export function createRendererSynchronization(): RendererSynchronization {
} catch {
// Storage events remain available when BroadcastChannel delivery fails.
}
try {
window.petShell?.notifyStateChanged?.();
} catch {
// Browser synchronization remains available when native IPC delivery fails.
}
},

subscribe(listener): () => void {
Expand All @@ -86,6 +98,7 @@ export function createRendererSynchronization(): RendererSynchronization {
closed = true;
listeners.clear();
window.removeEventListener('storage', onStorage);
unsubscribeNative();
if (channel !== undefined) {
channel.onmessage = null;
channel.close();
Expand Down
2 changes: 2 additions & 0 deletions src/types/electron-api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare global {
| { type: 'archive'; name: string; bytes: ArrayBuffer }
| { type: 'error' }
) => void) => () => void;
notifyStateChanged?: () => void;
onStateChanged?: (callback: () => void) => () => void;
dockNow?: () => void;
dragWindowTo?: (x: number, y: number) => void;
showContextMenu?: (x?: number, y?: number) => void;
Expand Down
Loading