From ec498765735e6dd55ee86ebdf9f2b05201a29f50 Mon Sep 17 00:00:00 2001 From: Shigma Date: Sat, 18 Jun 2022 04:50:11 +0800 Subject: [PATCH] feat: support Context.source symbol --- package.json | 2 +- src/context.ts | 9 +++++++-- src/lifecycle.ts | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 0102e20..19e7678 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "cordis", "description": "AOP Framework for Modern JavaScript Applications", - "version": "1.5.0", + "version": "1.5.3", "main": "lib/index.cjs", "module": "lib/index.mjs", "typings": "lib/index.d.ts", diff --git a/src/context.ts b/src/context.ts index 61c41af..4cc8da6 100644 --- a/src/context.ts +++ b/src/context.ts @@ -8,13 +8,15 @@ export interface Context extends Context.Services, Context.Meta, Lifecycle.Deleg declare global { interface Object { + [Context.source]?: Context [Context.filter]?(context: Context): boolean } } export class Context { static readonly filter = Symbol('filter') - static readonly current = Symbol('source') + static readonly source = Symbol('source') + static readonly current = Symbol('caller') static readonly immediate = Symbol('immediate') ;[Symbol.for('nodejs.util.inspect.custom')]() { @@ -75,6 +77,9 @@ export namespace Context { const oldValue = this.app[key] if (oldValue === value) return this.app[key] = value + if (value && typeof value === 'object') { + defineProperty(value, Context.source, this) + } if (typeof name !== 'string') return // trigger event @@ -82,7 +87,7 @@ export namespace Context { self[Context.filter] = (ctx) => { return this.mapping[name] === ctx.mapping[name] } - this.emit(self, 'internal/service', name, oldValue) + this.emit(self, 'internal/service', name) }, }) diff --git a/src/lifecycle.ts b/src/lifecycle.ts index f361ee5..7ae10b2 100644 --- a/src/lifecycle.ts +++ b/src/lifecycle.ts @@ -210,7 +210,7 @@ export interface Events { 'internal/fork'(fork: Fork): void 'internal/runtime'(runtime: Runtime): void 'internal/warning'(format: any, ...param: any[]): void - 'internal/service'(this: Context, name: string, oldValue: any): void + 'internal/service'(this: Context, name: string): void 'internal/update'(fork: Fork, config: any): void 'internal/hook'(name: string, listener: Function, prepend: boolean): () => boolean }