Skip to content

Commit

Permalink
feat(core): typings support for delegate events
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 6, 2021
1 parent 5b56651 commit 3dd372d
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
22 changes: 15 additions & 7 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,14 @@ interface Selector<T> extends PartialSeletor<T> {
except?: PartialSeletor<T>
}

export interface Context extends Context.Delegates {}

export class Context {
static readonly middleware = Symbol('middleware')
static readonly current = Symbol('source')

protected _bots: Bot[] & Record<string, Bot>

public database: Database
public assets: Assets
public router: Router

protected constructor(public filter: Filter, public app?: App, private _plugin: Plugin = null) {}

[inspect.custom]() {
Expand Down Expand Up @@ -526,6 +524,14 @@ Context.delegate('database')
Context.delegate('assets')
Context.delegate('router')

export namespace Context {
export interface Delegates {
database: Database
assets: Assets
router: Router
}
}

type FlattenEvents<T> = {
[K in keyof T & string]: K | `${K}/${FlattenEvents<T[K]>}`
}[keyof T & string]
Expand All @@ -538,17 +544,19 @@ type SessionEventMap = {
: (session: Session.Payload<K>) => void
}

type DelegateEventMap = {
[K in keyof Context.Delegates as `delegate/${K}`]: () => void
}

type EventName = keyof EventMap
type OmitSubstring<S extends string, T extends string> = S extends `${infer L}${T}${infer R}` ? `${L}${R}` : never
type BeforeEventName = OmitSubstring<EventName & string, 'before-'>
type BeforeEventMap = { [E in EventName & string as OmitSubstring<E, 'before-'>]: EventMap[E] }

export interface EventMap extends SessionEventMap {
export interface EventMap extends SessionEventMap, DelegateEventMap {
[Context.middleware]: Middleware

// Koishi events
'delegate/assets'(): void
'delegate/database'(): void
'appellation'(name: string, session: Session): string
'before-parse'(content: string, session: Session): Argv
'parse'(argv: Argv, session: Session): string
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin-eval/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import { WorkerResponse } from './worker'
export * from './main'

declare module 'koishi-core' {
interface Context {
worker: EvalWorker
namespace Context {
interface Delegates {
worker: EvalWorker
}
}

namespace Command {
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin-puppeteer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ declare module 'puppeteer-core/lib/types' {
}

declare module 'koishi-core' {
interface Context {
puppeteer: Puppeteer
namespace Context {
interface Delegates {
puppeteer: Puppeteer
}
}

namespace Plugin {
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-webui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export * from './server'
export type Activity = Record<number, number>

declare module 'koishi-core' {
interface Context {
webui: WebServer
namespace Context {
interface Delegates {
webui: WebServer
}
}

interface Database {
Expand Down Expand Up @@ -55,6 +57,8 @@ declare module 'koishi-core' {
}
}

Context.delegate('webui')

Channel.extend(() => ({
activity: {},
}))
Expand Down
2 changes: 0 additions & 2 deletions packages/plugin-webui/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import WebSocket from 'ws'
import type * as Vite from 'vite'
import type PluginVue from '@vitejs/plugin-vue'

Context.delegate('webui')

interface BaseConfig {
title?: string
devMode?: boolean
Expand Down

0 comments on commit 3dd372d

Please sign in to comment.