Skip to content

Commit

Permalink
fix(console): do not directly access loader service
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jan 9, 2024
1 parent 765a7f9 commit ae60519
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/console/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export abstract class Console extends Service {
async get(client: Client) {
const result = valueMap(this.entries, ({ files, ctx, data }, key) => ({
files: this.resolveEntry(files, key),
paths: this.ctx.loader?.paths(ctx.scope),
paths: this.ctx.get('loader')?.paths(ctx.scope),
data: data?.(client),
}))
result['_id'] = this.id as any
Expand Down
2 changes: 1 addition & 1 deletion plugins/commands/src/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default class CommandProvider extends DataService<CommandData[]> {
create: this.manager.snapshots[command.name]?.create,
initial: this.manager.snapshots[command.name]?.initial || { aliases: command._aliases, config: command.config, options: command._options },
override: this.manager.snapshots[command.name]?.override || { aliases: command._aliases, config: null, options: {} },
paths: this.ctx.loader?.paths(command.ctx.scope) || [],
paths: this.ctx.get('loader')?.paths(command.ctx.scope) || [],
})).sort((a, b) => a.name.localeCompare(b.name))
}
}
11 changes: 6 additions & 5 deletions plugins/config/src/shared/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ export class ServiceProvider extends DataService<Dict<number>> {
attach(Object.getPrototypeOf(internal))
for (const [key, { type }] of Object.entries(internal)) {
if (type !== 'service') continue
const ctx: Context = this.ctx.get(key)?.[Context.source]
if (ctx) {
const name = key.replace(/^__/, '').replace(/__$/, '')
services[name] = ctx.scope.uid
}
const instance = this.ctx.get(key)
if (!(instance instanceof Object)) continue
const ctx: Context = Reflect.getOwnPropertyDescriptor(instance, Context.current)?.value
if (!ctx) continue
const name = key.replace(/^__/, '').replace(/__$/, '')
services[name] = ctx.scope.uid
}
}
attach(this.ctx.root[Context.internal])
Expand Down
7 changes: 4 additions & 3 deletions plugins/console/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ class NodeConsole extends Console {
})

ctx.on('console/connection', () => {
if (!ctx.loader) return
ctx.loader.envData.clientCount = this.layer.clients.size
const loader = ctx.get('loader')
if (!loader) return
loader.envData.clientCount = this.layer.clients.size
})

this.root = config.root || (config.devMode
Expand Down Expand Up @@ -84,7 +85,7 @@ class NodeConsole extends Console {

this.ctx.on('server/ready', () => {
const target = this.ctx.server.selfUrl + this.config.uiPath
if (this.config.open && !this.ctx.loader?.envData.clientCount && !process.env.KOISHI_AGENT) {
if (this.config.open && !this.ctx.get('loader')?.envData.clientCount && !process.env.KOISHI_AGENT) {
open(target)
}
this.logger.info('webui is available at %c', target)
Expand Down
11 changes: 8 additions & 3 deletions plugins/insight/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ class Insight extends DataService<Insight.Payload> {
const services = {} as Record<number, string[]>
for (const [key, { type }] of Object.entries(this.ctx.root[Context.internal])) {
if (type !== 'service') continue
const ctx: Context = this.ctx.get(key)?.[Context.source]
const instance = this.ctx.get(key)
if (!(instance instanceof Object)) continue
const ctx: Context = Reflect.getOwnPropertyDescriptor(instance, Context.current)?.value
if (ctx?.scope.uid) {
(services[ctx.scope.uid] ||= []).push(key)
}
Expand Down Expand Up @@ -110,9 +112,12 @@ class Insight extends DataService<Insight.Payload> {
edges.push({ type, source, target })
}

function addDeps(state: EffectScope) {
const addDeps = (state: EffectScope) => {
for (const name of runtime.using) {
const uid = state.ctx[name]?.[Context.source]?.state.uid
const instance = this.ctx.get(name)
if (!(instance instanceof Object)) continue
const ctx: Context = Reflect.getOwnPropertyDescriptor(instance, Context.current)?.value
const uid = ctx?.state.uid
if (!uid) continue
addEdge('dashed', uid, state.uid)
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ export async function apply(ctx: Context, config: Config) {
ctx.on('dispose', () => {
writer?.close()
remove(Logger.targets, target)
if (ctx.loader) {
ctx.loader.prolog = []
if (loader) {
loader.prolog = []
}
})

for (const record of ctx.loader?.prolog || []) {
for (const record of loader?.prolog || []) {
target.record(record)
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/notifier/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Notifier {
toJSON(): Notifier.Data {
return {
...this.config,
paths: this.ctx.loader?.paths(this.ctx.scope),
paths: this.ctx.get('loader')?.paths(this.ctx.scope),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/status/src/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class ProfileProvider extends DataService<ProfileProvider.Payload> {
if (bot.hidden) continue
bots[bot.sid] = {
...bot.toJSON(),
paths: this.ctx.loader?.paths(bot.ctx.scope),
paths: this.ctx.get('loader')?.paths(bot.ctx.scope),
error: bot.error?.message,
messageSent: bot._messageSent.get(),
messageReceived: bot._messageReceived.get(),
Expand Down

0 comments on commit ae60519

Please sign in to comment.