Skip to content
Open
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: 8 additions & 1 deletion src/plugins/api-server/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { registerCallback } from '@/providers/song-info';
import { createBackend } from '@/utils';

import { JWTPayloadSchema } from './scheme';
import { registerAuth, registerControl, registerWebsocket } from './routes';
import { registerAuth, registerControl, registerWebsocket, wsNotifyClose } from './routes';

import { APPLICATION_NAME } from '@/i18n';

Expand All @@ -28,8 +28,14 @@ import type {

export const backend = createBackend<BackendType, APIServerConfig>({
async start(ctx) {
const { app } = await import('electron');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you used await import?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe they were not aware that vite does tree-shaking

const config = await ctx.getConfig();

// Notify WebSocket clients that playback has stopped before quitting
app.on('before-quit', () => {
wsNotifyClose?.();
});

this.init(ctx);
registerCallback((songInfo) => {
this.songInfo = songInfo;
Expand Down Expand Up @@ -197,6 +203,7 @@ export const backend = createBackend<BackendType, APIServerConfig>({
}
},
end() {
wsNotifyClose?.();
this.server?.close();
this.server = undefined;
},
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/api-server/backend/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { register as registerControl } from './control';
export { register as registerAuth } from './auth';
export { register as registerWebsocket } from './websocket';
export { register as registerWebsocket, wsNotifyClose } from './websocket';
20 changes: 18 additions & 2 deletions src/plugins/api-server/backend/routes/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ export const register = (
);
};

// Notify all WebSocket clients that playback has stopped (e.g. app closing/hiding)
// We intentionally do NOT close or clear sockets here so that clients like
// Boring Notch keep their connection alive and can receive updates when
// Pear Desktop comes back from hiding.
wsNotifyClose = () => {
console.log(`[API Server WebSocket] NotifyClose called. Sending isPlaying:false to ${sockets.size} clients.`);
send(DataTypes.PlayerStateChanged, {
isPlaying: false,
position: lastSongInfo?.elapsedSeconds ?? 0,
});
};

const createPlayerState = ({
songInfo,
volumeState,
Expand Down Expand Up @@ -131,7 +143,7 @@ export const register = (
upgradeWebSocket(() => ({
onOpen(_, ws) {
// "Unsafe argument of type `WSContext<WebSocket>` assigned to a parameter of type `WSContext<WebSocket>`. (@typescript-eslint/no-unsafe-argument)" ????? what?
sockets.add(ws as WSContext<WebSocket>);
sockets.add(ws as unknown as WSContext<WebSocket>);

ws.send(
JSON.stringify({
Expand All @@ -147,8 +159,12 @@ export const register = (
},

onClose(_, ws) {
sockets.delete(ws as WSContext<WebSocket>);
sockets.delete(ws as unknown as WSContext<WebSocket>);
},
})) as (ctx: Context, next: Next) => Promise<Response>,
);
};

// Exposed so the backend can call it on app close/hide
export let wsNotifyClose: (() => void) | undefined;