Skip to content

Commit

Permalink
feat: support Context.source symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 17, 2022
1 parent f1b039a commit ec49876
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 7 additions & 2 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')]() {
Expand Down Expand Up @@ -75,14 +77,17 @@ 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
const self: Context = Object.create(this)
self[Context.filter] = (ctx) => {
return this.mapping[name] === ctx.mapping[name]
}
this.emit(self, 'internal/service', name, oldValue)
this.emit(self, 'internal/service', name)
},
})

Expand Down
2 changes: 1 addition & 1 deletion src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ec49876

Please sign in to comment.