YesPlayMusic/src/main/ipcMain.ts

93 lines
2.1 KiB
TypeScript
Raw Normal View History

import { BrowserWindow, ipcMain, app } from 'electron'
import { db, Tables } from './db'
2022-04-16 21:14:03 +08:00
import { IpcChannels, IpcChannelsParams } from '../shared/IpcChannels'
2022-04-12 01:48:14 +08:00
import cache from './cache'
2022-04-16 13:30:25 +08:00
import log from './log'
2022-04-09 00:28:37 +08:00
import fs from 'fs'
2022-04-16 21:14:03 +08:00
import { APIs } from '../shared/CacheAPIs'
const on = <T extends keyof IpcChannelsParams>(
channel: T,
listener: (event: Electron.IpcMainEvent, params: IpcChannelsParams[T]) => void
) => {
ipcMain.on(channel, listener)
}
2022-04-09 00:28:37 +08:00
/**
* win对象的事件
* @param {BrowserWindow} win
*/
export function initIpcMain(win: BrowserWindow | null) {
2022-04-16 21:14:03 +08:00
on(IpcChannels.Minimize, () => {
2022-04-09 00:28:37 +08:00
win?.minimize()
})
2022-04-16 21:14:03 +08:00
on(IpcChannels.MaximizeOrUnmaximize, () => {
2022-04-09 00:28:37 +08:00
if (!win) return
win.isMaximized() ? win.unmaximize() : win.maximize()
})
2022-04-16 21:14:03 +08:00
on(IpcChannels.Close, () => {
2022-04-09 00:28:37 +08:00
app.exit()
})
}
2022-04-09 00:28:37 +08:00
/**
* API缓存
*/
2022-04-16 21:14:03 +08:00
on(IpcChannels.ClearAPICache, () => {
2022-04-12 01:48:14 +08:00
db.truncate(Tables.Track)
db.truncate(Tables.Album)
db.truncate(Tables.Artist)
db.truncate(Tables.Playlist)
db.truncate(Tables.ArtistAlbum)
db.truncate(Tables.AccountData)
db.truncate(Tables.Audio)
db.vacuum()
})
2022-04-09 00:28:37 +08:00
/**
* Get API cache
*/
2022-04-16 21:14:03 +08:00
on(IpcChannels.GetApiCacheSync, (event, args) => {
2022-04-09 00:28:37 +08:00
const { api, query } = args
2022-04-12 01:48:14 +08:00
const data = cache.get(api, query)
2022-04-09 00:28:37 +08:00
event.returnValue = data
})
2022-04-12 01:48:14 +08:00
/**
*
*/
2022-04-16 21:14:03 +08:00
on(IpcChannels.CacheCoverColor, (event, args) => {
const { id, color } = args
2022-04-12 01:48:14 +08:00
cache.set(APIs.CoverColor, { id, color })
})
2022-04-09 00:28:37 +08:00
/**
* tables到json文件便table大小dev环境
*/
if (process.env.NODE_ENV === 'development') {
2022-04-16 21:14:03 +08:00
on(IpcChannels.DevDbExportJson, () => {
2022-04-09 00:28:37 +08:00
const tables = [
2022-04-12 01:48:14 +08:00
Tables.ArtistAlbum,
Tables.Playlist,
Tables.Album,
Tables.Track,
Tables.Artist,
Tables.Audio,
Tables.AccountData,
Tables.Lyric,
2022-04-09 00:28:37 +08:00
]
tables.forEach(table => {
const data = db.findAll(table)
2022-04-09 00:28:37 +08:00
fs.writeFile(`./tmp/${table}.json`, JSON.stringify(data), function (err) {
if (err) {
return console.log(err)
}
console.log('The file was saved!')
})
})
})
}