auto update profile

This commit is contained in:
pompurin404 2024-08-08 14:34:51 +08:00
parent 6b537085b4
commit 1ecaea5667
No known key found for this signature in database
3 changed files with 46 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import axios from 'axios'
import yaml from 'yaml'
import fs from 'fs'
import { dialog } from 'electron'
import { addProfileUpdater } from '../core/profileUpdater'
let profileConfig: IProfileConfig // profile.yaml
let currentProfile: Partial<IMihomoConfig> // profiles/xxx.yaml
@ -41,6 +42,7 @@ export async function changeCurrentProfile(id: string): Promise<void> {
export function updateProfileItem(item: IProfileItem): void {
const index = profileConfig.items.findIndex((i) => i.id === item.id)
profileConfig.items[index] = item
addProfileUpdater(item.id)
fs.writeFileSync(profileConfigPath(), yaml.stringify(profileConfig))
window?.webContents.send('profileConfigUpdated')
}
@ -56,6 +58,7 @@ export async function addProfileItem(item: Partial<IProfileItem>): Promise<void>
if (!getProfileConfig().current) {
changeCurrentProfile(newItem.id)
}
addProfileUpdater(newItem.id)
fs.writeFileSync(profileConfigPath(), yaml.stringify(profileConfig))
window?.webContents.send('profileConfigUpdated')
}

View File

@ -0,0 +1,37 @@
import { addProfileItem, getProfileConfig, getProfileItem } from '../config'
const intervalPool: Record<string, NodeJS.Timeout> = {}
export function initProfileUpdater(): void {
const { items } = getProfileConfig()
for (const item of items) {
console.log(item.type, item.interval)
if (item.type === 'remote' && item.interval) {
addProfileItem(getProfileItem(item.id))
intervalPool[item.id] = setInterval(
() => {
addProfileItem(getProfileItem(item.id))
},
item.interval * 60 * 1000
)
}
}
}
export function addProfileUpdater(id: string): void {
const { items } = getProfileConfig()
const item = items.find((i) => i.id === id)
if (item?.type === 'remote' && item.interval) {
if (intervalPool[id]) {
clearInterval(intervalPool[id])
}
intervalPool[id] = setInterval(
() => {
addProfileItem(getProfileItem(id))
},
item.interval * 60 * 1000
)
}
}

View File

@ -14,6 +14,7 @@ import {
stopMihomoMemory,
stopMihomoTraffic
} from './core/mihomoApi'
import { initProfileUpdater } from './core/profileUpdater'
export let window: BrowserWindow | null = null
@ -57,7 +58,11 @@ if (!gotTheLock) {
app.whenReady().then(() => {
// Set app user model id for windows
electronApp.setAppUserModelId('party.mihomo.app')
startCore()
startCore().then(() => {
setTimeout(() => {
initProfileUpdater()
}, 10000)
})
// Default open or close DevTools by F12 in development
// and ignore CommandOrControl + R in production.
// see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils