Skip to content

Commit

Permalink
fix(cordis): add missing ctx thisArg for internal logger events
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 17, 2024
1 parent acffbd3 commit a2ea5b1
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/cordis/src/worker/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function apply(ctx: Context, config: Config = {}) {
if (config.autoRestart) {
process.send!({ type: 'exit' })
}
ctx.emit('internal/info', `terminated by ${signal}`)
ctx.emit(ctx, 'internal/info', `terminated by ${signal}`)
ctx.parallel('exit', signal).finally(() => process.exit())
}

Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export interface Hook extends EventOptions {
callback: (...args: any[]) => any
}

export default class Lifecycle {
class Lifecycle {
isActive = false
_tasks = new Set<Promise<void>>()
_hooks: Record<keyof any, Hook[]> = {}
Expand Down Expand Up @@ -216,6 +216,8 @@ export default class Lifecycle {
}
}

export default Lifecycle

export interface Events<in C extends Context = Context> {
'fork'(ctx: C, config: C['config']): void
'ready'(): Awaitable<void>
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare module './context' {
}
}

export default class ReflectService {
class ReflectService {
static resolveInject(ctx: Context, name: string) {
let internal = ctx[symbols.internal][name]
while (internal?.type === 'alias') {
Expand Down Expand Up @@ -75,7 +75,7 @@ export default class ReflectService {
if (!internal.set) return false
return internal.set.call(ctx, value, ctx[symbols.receiver])
} else {
// ctx.emit('internal/warning', new Error(`assigning to service ${name} is not recommended, please use \`ctx.set()\` method instead`))
// ctx.emit(ctx, 'internal/warning', new Error(`assigning to service ${name} is not recommended, please use \`ctx.set()\` method instead`))
ctx.reflect.set(name, value)
return true
}
Expand Down Expand Up @@ -194,3 +194,5 @@ export default class ReflectService {
})
}
}

export default ReflectService
4 changes: 3 additions & 1 deletion packages/core/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ declare module './context.ts' {
}
}

export default class Registry<C extends Context = Context> {
class Registry<C extends Context = Context> {
private _counter = 0
private _internal = new Map<Function, MainScope<C>>()
protected context: Context
Expand Down Expand Up @@ -190,3 +190,5 @@ export default class Registry<C extends Context = Context> {
return runtime.fork(this.ctx, config, error)
}
}

export default Registry
7 changes: 4 additions & 3 deletions packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export abstract class EffectScope<C extends Context = Context> {
ensure(callback: () => Promise<void>) {
const task = callback()
.catch((reason) => {
this.context.emit('internal/error', reason)
this.context.emit(this.ctx, 'internal/error', reason)
this.cancel(reason)
})
.finally(() => {
Expand Down Expand Up @@ -177,7 +177,7 @@ export abstract class EffectScope<C extends Context = Context> {
this.disposables = this.disposables.splice(0).filter((dispose) => {
if (this.uid !== null && dispose[Context.static] === this) return true
;(async () => dispose())().catch((reason) => {
this.context.emit('internal/error', reason)
this.context.emit(this.ctx, 'internal/error', reason)
})
})
}
Expand Down Expand Up @@ -389,7 +389,8 @@ export class MainScope<C extends Context = Context> extends EffectScope<C> {

update(config: C['config'], forced?: boolean) {
if (this.isForkable) {
this.context.emit('internal/warning', new Error(`attempting to update forkable plugin "${this.plugin.name}", which may lead to unexpected behavior`))
const warning = new Error(`attempting to update forkable plugin "${this.plugin.name}", which may lead to unexpected behavior`)
this.context.emit(this.ctx, 'internal/warning', warning)
}
const oldConfig = this.config
const resolved = resolveConfig(this.runtime.plugin || this.context.constructor, config)
Expand Down
4 changes: 2 additions & 2 deletions packages/loader/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ export class Entry<C extends Context = Context> {

async start() {
const exports = await this.parent.tree.import(this.options.name).catch((error: any) => {
this.context.emit('internal/error', new Error(`Cannot find package "${this.options.name}"`))
this.context.emit('internal/error', error)
this.context.emit(this.ctx, 'internal/error', new Error(`Cannot find package "${this.options.name}"`))
this.context.emit(this.ctx, 'internal/error', error)
})
if (!exports) return
const plugin = this.loader.unwrapExports(exports)
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class EntryGroup {
for (const id of Reflect.ownKeys({ ...oldMap, ...newMap }) as string[]) {
if (newMap[id]) {
this.create(newMap[id]).catch((error) => {
this.ctx.emit('internal/error', error)
this.ctx.emit(this.ctx, 'internal/error', error)
})
} else {
this.remove(id)
Expand Down
4 changes: 2 additions & 2 deletions packages/loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class NodeLoader extends Loader {
exit(code = NodeLoader.exitCode) {
const body = JSON.stringify(this.envData)
process.send?.({ type: 'shared', body }, (err: any) => {
if (err) this.ctx.emit('internal/error', 'failed to send shared data')
this.ctx.emit('internal/info', 'trigger full reload')
if (err) this.ctx.emit(this.ctx, 'internal/error', 'failed to send shared data')
this.ctx.emit(this.ctx, 'internal/info', 'trigger full reload')
process.exit(code)
})
}
Expand Down
2 changes: 1 addition & 1 deletion packages/loader/src/isolate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function apply(ctx: Context) {
if (!(value instanceof Object)) continue
const source = Reflect.getOwnPropertyDescriptor(value, Context.source)?.value
if (!source) {
entry.ctx.emit('internal/warning', new Error(`expected service ${key} to be implemented`))
entry.ctx.emit(entry.ctx, 'internal/warning', new Error(`expected service ${key} to be implemented`))
continue
}
this.diff.push([key, oldMap[key], this.newMap[key], entry.ctx[delim], source[delim]])
Expand Down

0 comments on commit a2ea5b1

Please sign in to comment.