Skip to content

Commit

Permalink
feat(core): teleport plugin under another plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 30, 2021
1 parent 904e589 commit cfd9151
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
36 changes: 25 additions & 11 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,27 @@ export class Context {
}
}

addDependency(plugin: string | Plugin) {
if (typeof plugin === 'string') {
plugin = require(plugin) as Plugin
}
const parent = this.app.registry.get(plugin)
if (!parent) throw new Error('dependency has not been installed')
this.state.dependencies.add(parent)
if (this.state.sideEffect) this.addSideEffect(parent)
private teleport(parent: Plugin, callback: Plugin) {
const state = this.app.registry.get(parent)
if (!state) return
this.plugin(callback)
const dispose = () => this.dispose(callback)
state.disposables.push(dispose)
this.before('disconnect', () => {
remove(state.disposables, dispose)
})
}

with(dependency: string, callback: Plugin) {
let parent: Plugin
try {
parent = require(dependency)
} catch {}
if (!parent) return
this.teleport(parent, callback)
this.on('plugin-added', (added) => {
if (added === parent) this.teleport(parent, callback)
})
}

plugin<T extends Plugin>(plugin: T, options?: Plugin.Config<T>): this
Expand Down Expand Up @@ -194,7 +207,7 @@ export class Context {
}

this.state.children.push(plugin)
this.emit('registry', this.app.registry)
this.emit('plugin-added', plugin, this.app.registry)
return this
}

Expand All @@ -208,7 +221,7 @@ export class Context {
]).finally(() => {
this.app.registry.delete(plugin)
remove(state.parent.children, plugin)
this.emit('registry', this.app.registry)
this.emit('plugin-removed', plugin, this.app.registry)
})
}

Expand Down Expand Up @@ -524,7 +537,8 @@ export interface EventMap extends SessionEventMap {
'before-command'(argv: Argv): Awaitable<void | string>
'command'(argv: Argv): Awaitable<void>
'middleware'(session: Session): void
'registry'(registry: Map<Plugin, Plugin.State>): void
'plugin-added'(plugin: Plugin, registry: Map<Plugin, Plugin.State>): void
'plugin-removed'(plugin: Plugin, registry: Map<Plugin, Plugin.State>): void
'before-connect'(): Awaitable<void>
'connect'(): void
'before-disconnect'(): Awaitable<void>
Expand Down
7 changes: 3 additions & 4 deletions packages/plugin-teach/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,9 @@ export function apply(ctx: Context, config: Config = {}) {
ctx.plugin(time, config)
ctx.plugin(writer, config)

const webui = ctx.app.webui
if (webui) {
ctx.with('koishi-plugin-webui', (ctx) => {
const { webui } = ctx.app
const { stats, meta } = webui.sources
ctx.addDependency('koishi-plugin-webui')

ctx.on('dialogue/before-send', ({ session, dialogue }) => {
session._sendType = 'dialogue'
Expand Down Expand Up @@ -243,5 +242,5 @@ export function apply(ctx: Context, config: Config = {}) {
ctx.before('disconnect', () => {
delete webui.entries['teach.js']
})
}
})
}
9 changes: 6 additions & 3 deletions packages/plugin-webui/server/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,12 @@ export class Registry implements DataSource<Registry.Payload> {
payload: Registry.Payload

constructor(private ctx: Context, public config: Registry.Config) {
ctx.on('registry', async () => {
this.ctx.app.webui.adapter?.broadcast('registry', await this.get(true))
})
ctx.on('plugin-added', this.update)
ctx.on('plugin-removed', this.update)
}

update = async () => {
this.ctx.app.webui.adapter?.broadcast('registry', await this.get(true))
}

async get(forced = false) {
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-webui/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export class WebServer {
}
const stats = await fs.stat(filename).catch<Stats>(noop)
if (stats?.isFile()) return sendFile(filename)
console.log(ctx.path, stats)
let template = await fs.readFile(resolve(this.root, 'index.html'), 'utf8')
if (vite) template = await vite.transformIndexHtml(uiPath, template)
ctx.type = 'html'
Expand Down

0 comments on commit cfd9151

Please sign in to comment.