Skip to content

Commit

Permalink
refa: prevent hot reload when config file is being written
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 17, 2022
1 parent b8ab3c8 commit 13455a1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/cli/src/worker/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ declare module 'koishi' {
// Theoretically, these properties will only appear on `Fork`.
// We define them directly on `State` for typing convenience.
interface State {
[Loader.update]?: boolean
[Loader.kRecord]?: Dict<Fork>
alias?: string
}
Expand All @@ -45,6 +46,7 @@ const group: Plugin.Object = {
}

export default class Loader extends ConfigLoader<App.Config> {
static readonly update = Symbol.for('koishi.loader.update')
static readonly kRecord = Symbol.for('koishi.loader.record')
static readonly kWarning = Symbol.for('koishi.loader.warning')

Expand Down Expand Up @@ -128,7 +130,8 @@ export default class Loader extends ConfigLoader<App.Config> {
if (fork) {
logger.info(`reload plugin %c`, key)
patch(fork.parent, config)
fork.update(config, true)
fork[Loader.update] = true
fork.update(config)
} else {
logger.info(`apply plugin %c`, key)
const name = key.split(':', 1)[0]
Expand Down Expand Up @@ -164,6 +167,12 @@ export default class Loader extends ConfigLoader<App.Config> {
this.entry = this.reloadPlugin(app, 'group:entry', this.config.plugins).context

app.on('internal/update', (fork, value) => {
// prevent hot reload when config file is being written
if (fork[Loader.update]) {
fork[Loader.update] = false
return
}

const { runtime } = fork.parent.state
const record = runtime[Loader.kRecord]
if (!record) return
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@koishijs/utils": "^5.4.5",
"cordis": "^1.5.2",
"cordis": "^1.5.3",
"fastest-levenshtein": "^1.0.12",
"minato": "^1.2.0"
}
Expand Down

0 comments on commit 13455a1

Please sign in to comment.