Skip to content

Commit

Permalink
feat(cordis): support mixin object
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 10, 2024
1 parent 9909463 commit f904dc4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
8 changes: 4 additions & 4 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export namespace Context {

export interface Accessor {
type: 'accessor'
get: () => any
set?: (value: any) => boolean
get: (this: Context) => any
set?: (this: Context, value: any) => boolean
}

export interface Alias {
Expand Down Expand Up @@ -88,8 +88,8 @@ export class Context {
self.registry = new Registry(self, config)
self.lifecycle = new Lifecycle(self)
self.mixin('scope', ['config', 'runtime', 'effect', 'collect', 'accept', 'decline'])
self.mixin('registry', ['using', 'inject', 'plugin', 'dispose'])
self.mixin('lifecycle', ['on', 'once', 'off', 'after', 'parallel', 'emit', 'serial', 'bail', 'start', 'stop'])
self.mixin('registry', ['using', 'inject', 'plugin'])
self.mixin('lifecycle', ['on', 'once', 'parallel', 'emit', 'serial', 'bail', 'start', 'stop'])

const attach = (internal: Context[typeof symbols.internal]) => {
if (!internal) return
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface EventOptions {
global?: boolean
}

interface Hook extends EventOptions {
export interface Hook extends EventOptions {
ctx: Context
callback: (...args: any[]) => any
}
Expand Down Expand Up @@ -123,8 +123,7 @@ export default class Lifecycle {
}
}

getHooks(name: keyof any, thisArg?: object) {
const hooks = this._hooks[name] || []
filterHooks(hooks: Hook[], thisArg?: object) {
return hooks.slice().filter((hook) => {
const filter = thisArg?.[Context.filter]
return hook.global || !filter || filter.call(thisArg, hook.ctx)
Expand All @@ -137,7 +136,7 @@ export default class Lifecycle {
if (name !== 'internal/event') {
this.emit('internal/event', type, name, args, thisArg)
}
for (const hook of this.getHooks(name, thisArg)) {
for (const hook of this.filterHooks(this._hooks[name] || [], thisArg)) {
yield hook.callback.apply(thisArg, args)
}
}
Expand Down
12 changes: 7 additions & 5 deletions packages/core/src/reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ declare module './context' {
provide(name: string, value?: any, builtin?: boolean): void
accessor(name: string, options: Omit<Context.Internal.Accessor, 'type'>): void
alias(name: string, aliases: string[]): void
mixin(name: string, mixins: string[] | Dict<string>): void
mixin<K extends string & keyof this>(name: K, mixins: (keyof this & keyof this[K])[] | Dict<string>): void
mixin<T extends {}>(source: T, mixins: (keyof this & keyof T)[] | Dict<string>): void
}
}

Expand Down Expand Up @@ -160,19 +161,20 @@ export default class ReflectService {
}
}

mixin(name: string, mixins: string[] | Dict<string>) {
mixin(source: any, mixins: string[] | Dict<string>) {
const entries = Array.isArray(mixins) ? mixins.map(key => [key, key]) : Object.entries(mixins)
const getTarget = typeof source === 'string' ? (ctx: Context) => ctx[source] : () => source
for (const [key, value] of entries) {
this.accessor(value, {
get() {
const service = this[name]
const service = getTarget(this)
if (isNullable(service)) return service
const value = Reflect.get(service, key)
if (typeof value !== 'function') return value
if (typeof value !== 'function' || typeof source !== 'string') return value
return value.bind(service)
},
set(value) {
return Reflect.set(this[name], key, value)
return Reflect.set(getTarget(this), key, value)
},
})
}
Expand Down

0 comments on commit f904dc4

Please sign in to comment.