Skip to content

Commit

Permalink
fix(console): refactor to console.services, fix console.config
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 22, 2024
1 parent e834f6e commit c1a839a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/console/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export class Client {

refresh() {
Object.keys(this.ctx.root[Context.internal]).forEach(async (name) => {
if (!name.startsWith('console.')) return
const key = name.slice(8)
if (!name.startsWith('console.services.')) return
const key = name.slice(17)
const service = this.ctx.get(name) as DataService
if (!service) return
if (await this.ctx.serial('console/intercept', this, service.options)) {
Expand Down
20 changes: 13 additions & 7 deletions packages/console/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ export * from './client'
export * from './entry'
export * from './service'

type NestedServices = {
[K in keyof Console.Services as `console.${K}`]: Console.Services[K]
}

declare module 'koishi' {
interface Context extends NestedServices {
interface Context {
console: Console
}

Expand Down Expand Up @@ -58,6 +54,16 @@ export abstract class Console extends Service {
readonly listeners: Dict<Listener> = Object.create(null)
readonly clients: Dict<Client> = Object.create(null)

public services = new Proxy({} as Console.Services, {
get: (target, key, receiver) => {
if (typeof key === 'symbol') return Reflect.get(target, key, receiver)
return this.ctx.get(`console.services.${key}`)
},
set: (target, key, value, receiver) => {
return false
},
})

constructor(public ctx: Context) {
super(ctx, 'console', true)
ctx.plugin(EntryProvider)
Expand Down Expand Up @@ -108,11 +114,11 @@ export abstract class Console extends Service {
}

refresh<K extends keyof Console.Services>(type: K) {
return this.ctx.get(`console.${type}`)?.refresh()
return this.ctx.get(`console.services.${type}`)?.refresh()
}

patch<K extends keyof Console.Services>(type: K, value: Console.Services[K] extends DataService<infer T> ? T : never) {
return this.ctx.get(`console.${type}`)?.patch(value as any)
return this.ctx.get(`console.services.${type}`)?.patch(value as any)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/console/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export abstract class DataService<T = never> extends Service {
}

constructor(protected ctx: Context, protected key: keyof Console.Services, public options: DataService.Options = {}) {
super(ctx, `console.${key}`, options.immediate)
super(ctx, `console.services.${key}`, options.immediate)
}

start() {
Expand Down

0 comments on commit c1a839a

Please sign in to comment.