Skip to content

Commit

Permalink
perf: 监听文件
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Jan 5, 2025
1 parent a4ed463 commit 9ca613d
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions components/Config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import YAML from 'yaml'
import fs from 'node:fs'
import { logger } from '#lib'
import { task } from '#models'
import chokidar from 'chokidar'
import Version from './Version.js'
import YamlReader from './YamlReader.js'
import { task } from '#models'
import { basename, dirname } from 'path'

const watcher = new chokidar.FSWatcher({
persistent: true,
ignoreInitial: true,
usePolling: true
})

class Config {
constructor () {
this.config = {}
/** 监听文件 */
this.watcher = { config: {}, defSet: {} }

this.initCfg()
}
Expand Down Expand Up @@ -75,6 +80,16 @@ class Config {
}
this.watch(`${path}${file}`, file.replace('.yaml', ''), 'config')
}
watcher.on('change', async path => {
const name = basename(path).replace('.yaml', '')
const type = basename(dirname(path))
const key = `${type}.${name}`
delete this.config[key]
logger.mark(`[${Version.pluginName}][修改配置文件][${type}][${name}]`)
if (type === 'config' && name === 'push') {
task.startTimer()
}
})
}

/**
Expand Down Expand Up @@ -192,8 +207,8 @@ class Config {

/**
* 获取配置yaml
* @param type 默认跑配置-defSet,用户配置-config
* @param name 名称
* @param {'config'|'default_config'} type
* @param {String} name 文件名
*/
getYaml (type, name) {
const file = `${Version.pluginPath}/config/${type}/${name}.yaml`
Expand All @@ -205,8 +220,6 @@ class Config {
fs.readFileSync(file, 'utf8')
)

this.watch(file, name, type)

return this.config[key]
}

Expand All @@ -220,20 +233,8 @@ class Config {
}

/** 监听配置文件 */
watch (file, name, type = 'default_config') {
const key = `${type}.${name}`
if (this.watcher[key]) return

const watcher = chokidar.watch(file)
watcher.on('change', async path => {
delete this.config[key]
logger.mark(`[${Version.pluginName}][修改配置文件][${type}][${name}]`)
if (type === 'config' && name === 'push') {
task.startTimer()
}
})

this.watcher[key] = watcher
watch (file) {
watcher.add(file)
}

/**
Expand Down

0 comments on commit 9ca613d

Please sign in to comment.