Skip to content

Commit

Permalink
refa: move several events under internal namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 6, 2022
1 parent d535224 commit a77cd43
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 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.3.0",
"version": "1.3.1",
"main": "lib/index.cjs",
"module": "lib/index.ejs",
"typings": "lib/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export namespace Context {
if (oldValue === value) return
this.app[privateKey] = value
if (typeof name !== 'string') return
this.emit('service', name, oldValue)
this.emit('internal/service', name, oldValue)
const action = value ? oldValue ? 'changed' : 'enabled' : 'disabled'
this.emit('logger/debug', 'service', name, action)
},
Expand Down
10 changes: 5 additions & 5 deletions src/lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Lifecycle {
_hooks: Record<keyof any, [Context, (...args: any[]) => any][]> = {}

constructor(private ctx: Context, private config: Lifecycle.Config) {
(this as Lifecycle.Delegates).on('hook', (name, listener, prepend) => {
(this as Lifecycle.Delegates).on('internal/hook', (name, listener, prepend) => {
const method = prepend ? 'unshift' : 'push'
const { runtime, disposables } = this.caller.state
if (name === 'ready' && this.isActive) {
Expand Down Expand Up @@ -168,7 +168,7 @@ export class Lifecycle {

on(name: EventName, listener: Function, prepend = false) {
// handle special events
const result = this.bail('hook', name, listener, prepend)
const result = this.bail('internal/hook', name, listener, prepend)
if (result) return result

const hooks = this._hooks[name] ||= []
Expand Down Expand Up @@ -227,7 +227,7 @@ export interface Events {
'ready'(): Awaitable<void>
'fork': Plugin.Function
'dispose'(): Awaitable<void>
'service'(name: string, oldValue: any): void
'config'(state: Plugin.Fork, config: any): void
'hook'(name: string, listener: Function, prepend: boolean): () => boolean
'internal/service'(name: string, oldValue: any): void
'internal/update'(state: Plugin.Fork, config: any): void
'internal/hook'(name: string, listener: Function, prepend: boolean): () => boolean
}
16 changes: 10 additions & 6 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export namespace Plugin {

abstract dispose(): boolean
abstract restart(): void
abstract update(config: any): void
abstract update(config: any, manual: boolean): void

constructor(public parent: Context, public config: any) {
this.context = parent.fork({ state: this })
Expand Down Expand Up @@ -78,11 +78,13 @@ export namespace Plugin {
}
}

update(config: any) {
update(config: any, manual = false) {
const oldConfig = this.config
const resolved = Registry.validate(this.runtime.plugin, config)
this.config = resolved
this.context.emit('config', this, config)
if (!manual) {
this.context.emit('internal/update', this, config)
}
if (this.runtime.isForkable) {
this.restart()
} else if (this.runtime.config === oldConfig) {
Expand Down Expand Up @@ -147,7 +149,7 @@ export namespace Plugin {
}

if (this.using.length) {
const dispose = this.context.on('service', (name) => {
const dispose = this.context.on('internal/service', (name) => {
if (!this.using.includes(name)) return
this.restart()
})
Expand Down Expand Up @@ -187,7 +189,7 @@ export namespace Plugin {
}
}

update(config: any) {
update(config: any, manual = false) {
if (this.isForkable) {
this.context.emit('logger/warn', 'app', `attempting to update forkable plugin "${this.plugin.name}", which may lead unexpected behavior`)
}
Expand All @@ -197,7 +199,9 @@ export namespace Plugin {
for (const fork of this.children) {
if (fork.config !== oldConfig) continue
fork.config = resolved
this.context.emit('config', fork, config)
if (!manual) {
this.context.emit('internal/update', fork, config)
}
}
this.restart()
}
Expand Down

0 comments on commit a77cd43

Please sign in to comment.