Skip to content

Commit

Permalink
fix(loader): debounced write config
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 26, 2024
1 parent 50e4e0c commit 174ac2b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/koishi/src/cli/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function createWorker(options: Dict<any>) {
if (message.type === 'start') {
config = message.body
timer = config.heartbeatTimeout && setTimeout(() => {
// eslint-disable-next-line no-console
console.log(kleur.red('daemon: heartbeat timeout'))
child.kill('SIGKILL')
}, config.heartbeatTimeout)
Expand Down Expand Up @@ -128,6 +129,7 @@ export default function (cli: CAC) {
.action((file, options) => {
const { logLevel, debug, logTime, ...rest } = options
if (logLevel !== undefined && (!isInteger(logLevel) || logLevel < 0)) {
// eslint-disable-next-line no-console
console.warn(`${kleur.red('error')} log level should be a positive integer.`)
process.exit(1)
}
Expand Down
14 changes: 9 additions & 5 deletions packages/loader/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export abstract class Loader {

private store = new WeakMap<any, string>()

private _writeTimer: undefined | number | NodeJS.Timeout
private _writeTask?: Promise<void>
private _writeSlient = true

abstract import(name: string): Promise<any>
abstract fullReload(code?: number): void
Expand Down Expand Up @@ -260,10 +261,13 @@ export abstract class Loader {
if (!silent) this.app.emit('config')
}

async writeConfig(silent = false) {
clearTimeout(this._writeTimer)
return new Promise<void>((resolve, reject) => {
this._writeTimer = setTimeout(() => {
writeConfig(silent = false) {
this._writeSlient &&= silent
if (this._writeTask) return this._writeTask
return this._writeTask = new Promise((resolve, reject) => {
setTimeout(() => {
this._writeSlient = true
this._writeTask = undefined
this._writeConfig(silent).then(resolve, reject)
}, 0)
})
Expand Down

0 comments on commit 174ac2b

Please sign in to comment.