Skip to content

Commit

Permalink
fix: remove duplicate api
Browse files Browse the repository at this point in the history
  • Loading branch information
JellyBrick committed Dec 24, 2024
1 parent df0a9f7 commit 52f13e3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 61 deletions.
17 changes: 15 additions & 2 deletions src/plugins/api-server/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { registerAuth, registerControl } from './routes';
import { type APIServerConfig, AuthStrategy } from '../config';

import type { BackendType } from './types';
import type { RepeatMode } from '@/types/datahost-get-state';

export const backend = createBackend<BackendType, APIServerConfig>({
async start(ctx) {
Expand All @@ -23,7 +24,14 @@ export const backend = createBackend<BackendType, APIServerConfig>({
this.songInfo = songInfo;
});

ctx.ipc.on('ytmd:player-api-loaded', () => ctx.ipc.send('ytmd:setup-time-changed-listener'));
ctx.ipc.on('ytmd:player-api-loaded', () =>
ctx.ipc.send('ytmd:setup-time-changed-listener'),
);

ctx.ipc.on(
'ytmd:repeat-changed',
(mode: RepeatMode) => (this.currentRepeatMode = mode),
);

this.run(config.hostname, config.port);
},
Expand Down Expand Up @@ -75,7 +83,12 @@ export const backend = createBackend<BackendType, APIServerConfig>({
});

// routes
registerControl(this.app, ctx, () => this.songInfo);
registerControl(
this.app,
ctx,
() => this.songInfo,
() => this.currentRepeatMode,
);
registerAuth(this.app, ctx);

// swagger
Expand Down
41 changes: 7 additions & 34 deletions src/plugins/api-server/backend/routes/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
SetFullscreenSchema,
} from '../scheme';

import type { RepeatMode } from '@/types/datahost-get-state';
import type { SongInfo } from '@/providers/song-info';
import type { BackendContext } from '@/types/contexts';
import type { APIServerConfig } from '../../config';
Expand Down Expand Up @@ -171,7 +172,7 @@ const routes = {
content: {
'application/json': {
schema: z.object({
mode: z.string().nullable().openapi({example: 'ONE'}),
mode: z.enum(['ONE', 'NONE', 'ALL']).nullable(),
}),
},
},
Expand Down Expand Up @@ -304,8 +305,8 @@ const routes = {
content: {
'application/json': {
schema: z.object({
current: z.number().nullable().openapi({example: 3}),
duration: z.number().nullable().openapi({example: 233}),
current: z.number().nullable().openapi({ example: 3 }),
duration: z.number().nullable().openapi({ example: 233 }),
}),
},
},
Expand Down Expand Up @@ -337,6 +338,7 @@ export const register = (
app: HonoApp,
{ window }: BackendContext<APIServerConfig>,
songInfoGetter: () => SongInfo | undefined,
repeatModeGetter: () => RepeatMode | undefined,
) => {
const controller = getSongControls(window);

Expand Down Expand Up @@ -402,22 +404,10 @@ export const register = (
ctx.status(204);
return ctx.body(null);
});
app.openapi(routes.repeatMode, async (ctx) => {
const modeResponsePromise = new Promise<string|null>((resolve) => {
ipcMain.once(
'ytmd:repeat-mode-response',
(_, repeatmode: string | null) => {
return resolve(repeatmode);
},
);

controller.requestRepeatModeInformation();
});

const repeatmode = await modeResponsePromise;

app.openapi(routes.repeatMode, (ctx) => {
ctx.status(200);
return ctx.json({ mode: repeatmode });
return ctx.json({ mode: repeatModeGetter() ?? null });
});
app.openapi(routes.switchRepeat, (ctx) => {
const { iteration } = ctx.req.valid('json');
Expand Down Expand Up @@ -483,23 +473,6 @@ export const register = (
ctx.status(200);
return ctx.json(info);
});
app.openapi(routes.seekTime, async (ctx) => {
const timeResponsePromise = new Promise<object|null>((resolve) => {
ipcMain.once(
'ytmd:seek-time-response',
(_, time) => {
return resolve(time);
},
);

controller.requestSeekTimeInformation();
});

const time = await timeResponsePromise;

ctx.status(200);
return ctx.json(time);
});
app.openapi(routes.songInfo, (ctx) => {
const info = songInfoGetter();

Expand Down
2 changes: 2 additions & 0 deletions src/plugins/api-server/backend/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { serve } from '@hono/node-server';

import type { BackendContext } from '@/types/contexts';
import type { SongInfo } from '@/providers/song-info';
import type { RepeatMode } from '@/types/datahost-get-state';
import type { APIServerConfig } from '../config';

export type HonoApp = Hono;
Expand All @@ -11,6 +12,7 @@ export type BackendType = {
server?: ReturnType<typeof serve>;
oldConfig?: APIServerConfig;
songInfo?: SongInfo;
currentRepeatMode?: RepeatMode;

init: (ctx: BackendContext<APIServerConfig>) => Promise<void>;
run: (hostname: string, port: number) => void;
Expand Down
6 changes: 0 additions & 6 deletions src/providers/song-controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ export default (win: BrowserWindow) => {
}
},
shuffle: () => win.webContents.send('ytmd:shuffle'),
requestRepeatModeInformation: () => {
win.webContents.send('ytmd:get-repeat-mode');
},
switchRepeat: (n: ArgsType<number> = 1) => {
const repeat = parseNumberFromArgsType(n);
if (repeat !== null) {
Expand Down Expand Up @@ -83,8 +80,5 @@ export default (win: BrowserWindow) => {
keyCode: '/',
});
},
requestSeekTimeInformation: () => {
win.webContents.send('ytmd:get-seek-time');
},
};
};
19 changes: 0 additions & 19 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,6 @@ async function onApiLoaded() {
?.updateLikeStatus(status);
},
);
window.ipcRenderer.on('ytmd:get-repeat-mode', () => {
const repeatMode =
document
.querySelector<HTMLElement>('ytmusic-player-bar')
?.attributes.getNamedItem('repeat-mode') ?? null;
window.ipcRenderer.send('ytmd:repeat-mode-response', repeatMode?.value);
});
window.ipcRenderer.on('ytmd:switch-repeat', (_, repeat = 1) => {
for (let i = 0; i < repeat; i++) {
document
Expand Down Expand Up @@ -157,18 +150,6 @@ async function onApiLoaded() {
?.onVolumeTap();
});

window.ipcRenderer.on('ytmd:get-seek-time', () => {
const progressBar =
document
.querySelector<HTMLElement>('#progress-bar')
const currentTime = progressBar?.attributes.getNamedItem('value');
const duration = progressBar?.attributes.getNamedItem('aria-valuemax');
window.ipcRenderer.send('ytmd:seek-time-response', {
current: currentTime != null ? parseInt(currentTime.value): null,
duration: duration != null ? parseInt(duration.value): null
});
});

window.ipcRenderer.on('ytmd:get-queue', () => {
const queue = document.querySelector<QueueElement>('#queue');
window.ipcRenderer.send('ytmd:get-queue-response', {
Expand Down

0 comments on commit 52f13e3

Please sign in to comment.