YesPlayMusic/src/main/ipcMain.ts

77 lines
1.7 KiB
TypeScript
Raw Normal View History

import { BrowserWindow, ipcMain, app } from 'electron'
import { db, Tables } from './db'
2022-04-09 00:28:37 +08:00
import { IpcChannels } from './IpcChannelsName'
import { getCache } from './cache'
import logger from './logger'
import fs from 'fs'
2022-04-09 00:28:37 +08:00
/**
* win对象的事件
* @param {BrowserWindow} win
*/
export function initIpcMain(win: BrowserWindow | null) {
ipcMain.on(IpcChannels.Minimize, () => {
win?.minimize()
})
ipcMain.on(IpcChannels.MaximizeOrUnmaximize, () => {
if (!win) return
win.isMaximized() ? win.unmaximize() : win.maximize()
})
ipcMain.on(IpcChannels.Close, () => {
app.exit()
})
}
2022-04-09 00:28:37 +08:00
/**
* API缓存
*/
ipcMain.on(IpcChannels.ClearAPICache, () => {
db.truncate(Tables.TRACK)
db.truncate(Tables.ALBUM)
db.truncate(Tables.ARTIST)
db.truncate(Tables.PLAYLIST)
db.truncate(Tables.ARTIST_ALBUMS)
db.truncate(Tables.ACCOUNT_DATA)
db.truncate(Tables.AUDIO)
db.vacuum()
})
2022-04-09 00:28:37 +08:00
/**
* Get API cache
*/
ipcMain.on(IpcChannels.GetApiCacheSync, (event, args) => {
const { api, query } = args
const data = getCache(api, query)
event.returnValue = data
})
2022-04-09 00:28:37 +08:00
/**
* tables到json文件便table大小dev环境
*/
if (process.env.NODE_ENV === 'development') {
ipcMain.on(IpcChannels.DevDbExportJson, () => {
const tables = [
Tables.ARTIST_ALBUMS,
Tables.PLAYLIST,
Tables.ALBUM,
Tables.TRACK,
Tables.ARTIST,
Tables.AUDIO,
Tables.ACCOUNT_DATA,
Tables.LYRIC,
]
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!')
})
})
})
}