Skip to content

Commit

Permalink
feat(core): ctx.with() mulitple plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 3, 2021
1 parent e0576e5 commit fa03ede
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 15 deletions.
1 change: 1 addition & 0 deletions build/dtsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ async function bundle(path: string) {
if (!identifier) line = line.slice(4)
return line
.replace(internalImport, '')
.replace(/import\("index"\)/g, 'import(".")')
.replace(/^((module|class|namespace) .+ \{)$/, (_) => `declare ${_}`)
}
}).filter(line => line).join(EOL)
Expand Down
39 changes: 26 additions & 13 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ export namespace Plugin {
disposables: Disposable[]
dependencies: Set<State>
}

export interface Packages {}

export type Teleporter<D extends readonly (keyof Packages)[]> = (...modules: From<D>) => void

type From<D extends readonly unknown[]> = D extends readonly [infer L, ...infer R]
? [L extends keyof Packages ? Packages[L] : unknown, ...From<R>]
: []
}

function isBailed(value: any) {
Expand Down Expand Up @@ -153,24 +161,29 @@ export class Context {
}
}

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)
private teleport(modules: any[], callback: Plugin.Teleporter<never[]>) {
const states: Plugin.State[] = []
for (const module of modules) {
const state = this.app.registry.get(module)
if (!state) return
states.push(state)
}
const plugin = () => callback(...modules as [])
const dispose = () => this.dispose(plugin)
this.plugin(plugin)
states.every(state => state.disposables.push(dispose))
this.before('disconnect', () => {
remove(state.disposables, dispose)
states.every(state => remove(state.disposables, dispose))
})
}

with(dependency: string, callback: Plugin) {
const parent = safeRequire(dependency)
if (!parent) return
this.teleport(parent, callback)
with<D extends readonly (keyof Plugin.Packages)[]>(deps: D, callback: Plugin.Teleporter<D>) {
const modules = deps.map(safeRequire)
if (!modules.every(val => val)) return
this.teleport(modules, callback)
this.on('plugin-added', (added) => {
const parent = safeRequire(dependency)
if (added === parent) this.teleport(parent, callback)
const modules = deps.map(safeRequire)
if (modules.includes(added)) this.teleport(modules, callback)
})
}

Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-adventure/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ declare module 'koishi-core' {
interface App {
adventure: Config
}

namespace Plugin {
interface Packages {
'koishi-plugin-adventure': typeof import('.')
}
}
}

export interface Config extends Item.Config {}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-chat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const name = 'chat'

export function apply(ctx: Context, options: Config = {}) {
ctx.plugin(debug, options)
ctx.with('koishi-plugin-webui', (ctx) => {
ctx.with(['koishi-plugin-webui'], () => {
const filename = ctx.webui.config.devMode ? '../client/index.ts' : '../dist/index.js'
ctx.webui.addEntry(resolve(__dirname, filename))
})
Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-eval/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ declare module 'koishi-core' {
_isEval: boolean
_sendCount: number
}

namespace Plugin {
interface Packages {
'koishi-plugin-eval': typeof import('.')
}
}
}

interface OptionManifest extends Domain.OptionConfig {
Expand Down
8 changes: 7 additions & 1 deletion packages/plugin-teach/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ declare module 'koishi-core' {
'dialogue/validate'(argv: Dialogue.Argv): void | string
'dialogue/execute'(argv: Dialogue.Argv): void | Promise<void | string>
}

namespace Plugin {
interface Packages {
'koishi-plugin-teach': typeof import('.')
}
}
}

declare module 'koishi-plugin-webui' {
Expand Down Expand Up @@ -205,7 +211,7 @@ export function apply(ctx: Context, config: Config = {}) {
ctx.plugin(time, config)
ctx.plugin(writer, config)

ctx.with('koishi-plugin-webui', (ctx) => {
ctx.with(['koishi-plugin-webui'], () => {
const { stats, meta } = ctx.webui.sources

ctx.on('dialogue/before-send', ({ session, dialogue }) => {
Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-webui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ declare module 'koishi-core' {
name: string
activity: Activity
}

namespace Plugin {
interface Packages {
'koishi-plugin-webui': typeof import('.')
}
}
}

Channel.extend(() => ({
Expand Down

0 comments on commit fa03ede

Please sign in to comment.