Skip to content

Commit

Permalink
fix(cli): await for plugin dispose before reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 19, 2021
1 parent a4498ea commit 2370aca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 0 additions & 2 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ export class Context {
]).finally(() => {
this.app.registry.delete(plugin)
remove(state.parent.children, plugin)
const index = state.parent.children.indexOf(plugin)
if (index >= 0) state.parent.children.splice(index, 1)
this.emit('registry', this.app.registry)
})
}
Expand Down
12 changes: 9 additions & 3 deletions packages/koishi/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function createWatcher() {

const logger = new Logger('app:watcher')

watcher.on('change', (path) => {
watcher.on('change', async (path) => {
if (!require.cache[path] || externals.has(path)) return
logger.debug('change detected:', path)

Expand All @@ -214,7 +214,8 @@ function createWatcher() {
declined.delete(path)

const plugins: string[] = []
for (const [filename] of pluginMap) {
const tasks: Promise<void>[] = []
for (const [filename, [name]] of pluginMap) {
const dependencies = loadDependencies(filename, declined)
if (dependencies.has(path)) {
dependencies.forEach(dep => accepted.add(dep))
Expand All @@ -224,10 +225,15 @@ function createWatcher() {

// dispose installed plugin
plugins.push(filename)
app.dispose(plugin)
const displayName = plugin.name || name
tasks.push(app.dispose(plugin).catch((err) => {
logger.warn('failed to dispose plugin %c\n' + coerce(err), displayName)
}))
}
}

await Promise.all(tasks)

accepted.forEach(dep => delete require.cache[dep])
for (const filename of plugins) {
const plugin = require(filename)
Expand Down

0 comments on commit 2370aca

Please sign in to comment.