Skip to content

Commit

Permalink
feat(cordis): optimize inject checker stack
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Aug 7, 2024
1 parent 821d7b4 commit b785eff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import ReflectService from './reflect.ts'
import Registry from './registry.ts'
import { getTraceable, resolveConfig, symbols } from './utils.ts'

export { Lifecycle, ReflectService, Registry }

export namespace Context {
export type Parameterized<C, T = any> = C & { config: T }

Expand Down
14 changes: 9 additions & 5 deletions packages/core/src/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ReflectService {
return [name, internal] as const
}

static checkInject(ctx: Context, name: string) {
static checkInject(ctx: Context, name: string, error: Error) {
ctx = ctx[symbols.shadow] ?? ctx
// Case 1: built-in services and special properties
// - prototype: prototype detection
Expand All @@ -39,8 +39,7 @@ class ReflectService {
if (!ctx.runtime.plugin) return
// Case 4: custom inject checks
if (ctx.bail(ctx, 'internal/inject', name)) return
const warning = new Error(`property ${name} is not registered, declare it as \`inject\` to suppress this warning`)
ctx.emit(ctx, 'internal/warning', warning)
ctx.emit(ctx, 'internal/warning', error)
}

static handler: ProxyHandler<Context> = {
Expand All @@ -52,13 +51,18 @@ class ReflectService {
}

const [name, internal] = ReflectService.resolveInject(ctx, prop)
// trace caller
const error = new Error(`property ${name} is not registered, declare it as \`inject\` to suppress this warning`)
const lines = error.stack!.split('\n')
lines.splice(1, 1)
error.stack = lines.join('\n')
if (!internal) {
ReflectService.checkInject(ctx, name)
ReflectService.checkInject(ctx, name, error)
return Reflect.get(target, name, ctx)
} else if (internal.type === 'accessor') {
return internal.get.call(ctx, ctx[symbols.receiver])
} else {
if (!internal.builtin) ReflectService.checkInject(ctx, name)
if (!internal.builtin) ReflectService.checkInject(ctx, name, error)
return ctx.reflect.get(name)
}
},
Expand Down

0 comments on commit b785eff

Please sign in to comment.