Skip to content

Commit

Permalink
fix(core): use Context.Services to suggest services
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 21, 2022
1 parent aeacedb commit 3a63ca0
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 11 deletions.
1 change: 0 additions & 1 deletion packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export class App extends Context {
_shortcuts: Command.Shortcut[] = []
_hooks: Record<keyof any, [Context, (...args: any[]) => any][]> = {}
_sessions: Dict<Session> = Object.create(null)
_services: Dict<string> = Object.create(null)
_userCache = new SharedCache<User.Observed<any>>()
_channelCache = new SharedCache<Channel.Observed<any>>()

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,8 +586,11 @@ export namespace Context {
model: Model
}

export const Services: (keyof Services)[] = []

export function service(key: keyof Services) {
if (Object.prototype.hasOwnProperty.call(Context.prototype, key)) return
Services.push(key)
const privateKey = Symbol(key)
Object.defineProperty(Context.prototype, key, {
get(this: Context) {
Expand All @@ -604,16 +607,13 @@ export namespace Context {
const action = value ? oldValue ? 'changed' : 'enabled' : 'disabled'
this.logger('service').debug(key, action)
if (value) {
this.app._services[key] = this.state.id
const dispose = () => {
if (this.app[privateKey] !== value) return
this[key] = null
delete this.app._services[key]
}
this.state.disposables.push(dispose)
this.on('service', (name) => {
if (name !== key) return
dispose()
remove(this.state.disposables, dispose)
})
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/frontend/console/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ class Console extends Service {
private onConnection = (socket: WebSocket) => {
const channel = new SocketHandle(this, socket)

for (const name in this.ctx.app._services) {
for (const name of Context.Services) {
if (!name.startsWith('console.')) continue
this.ctx[name].get().then((value) => {
this.ctx[name]?.['get']().then((value) => {
const key = name.slice(8)
socket.send(JSON.stringify({ type: 'data', body: { key, value } }))
})
Expand Down
2 changes: 0 additions & 2 deletions plugins/frontend/manager/client/graph/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { store } from '~/client'
import { Dict } from 'koishi'
import { computed } from 'vue'

type Bound = [number, number][]

export interface Node extends PluginData {
id: string
col?: number
Expand Down
2 changes: 1 addition & 1 deletion plugins/frontend/manager/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ registerPage({
name: '依赖图',
icon: 'project-diagram',
order: 600,
fields: ['registry', 'services'],
fields: ['registry'],
component: Graph,
})
2 changes: 1 addition & 1 deletion plugins/frontend/manager/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class RegistryProvider extends DataSource<Dict<PluginData>> {
: capitalize(camelize(plugin.name)),
parent: state.parent?.id,
disposables: state.disposables.length,
dependencies: state.using.map(name => this.ctx.app._services[name]).filter(x => x),
dependencies: state.using.map(name => this.ctx[name]?.['ctx']?.state.id).filter(x => x),
}
for (const child of state.children) {
this.traverse(child)
Expand Down
7 changes: 6 additions & 1 deletion plugins/frontend/manager/src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export default class ServiceProvider extends DataSource<Dict<string>> {
}

async get() {
return this.ctx.app._services
const result: Dict<string> = {}
for (const name of Context.Services) {
const value = this.ctx[name]?.['ctx']?.state.id
if (value) result[name] = value
}
return result
}
}

0 comments on commit 3a63ca0

Please sign in to comment.