From 1ecaea566765e289e232c8827f49ed08db07b189 Mon Sep 17 00:00:00 2001 From: pompurin404 Date: Thu, 8 Aug 2024 14:34:51 +0800 Subject: [PATCH] auto update profile --- src/main/config/profile.ts | 3 +++ src/main/core/profileUpdater.ts | 37 +++++++++++++++++++++++++++++++++ src/main/index.ts | 7 ++++++- 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/main/core/profileUpdater.ts diff --git a/src/main/config/profile.ts b/src/main/config/profile.ts index 1df3271..dde07ac 100644 --- a/src/main/config/profile.ts +++ b/src/main/config/profile.ts @@ -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 // profiles/xxx.yaml @@ -41,6 +42,7 @@ export async function changeCurrentProfile(id: string): Promise { 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): Promise if (!getProfileConfig().current) { changeCurrentProfile(newItem.id) } + addProfileUpdater(newItem.id) fs.writeFileSync(profileConfigPath(), yaml.stringify(profileConfig)) window?.webContents.send('profileConfigUpdated') } diff --git a/src/main/core/profileUpdater.ts b/src/main/core/profileUpdater.ts new file mode 100644 index 0000000..616424c --- /dev/null +++ b/src/main/core/profileUpdater.ts @@ -0,0 +1,37 @@ +import { addProfileItem, getProfileConfig, getProfileItem } from '../config' + +const intervalPool: Record = {} + +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 + ) + } +} diff --git a/src/main/index.ts b/src/main/index.ts index 217eb94..bd77f60 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -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