Skip to content

Commit

Permalink
feat(cordis): enhance associate api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 24, 2024
1 parent 1ee7e8a commit d43f175
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/cordis/src/cli.ts
Expand Up @@ -47,6 +47,7 @@ function createWorker(options: worker.Options) {
if (message.type === 'start') {
started = true
timer = options.daemon?.heartbeatTimeout && setTimeout(() => {
// eslint-disable-next-line no-console
console.log(kleur.red('daemon: heartbeat timeout'))
child.kill('SIGKILL')
}, options.daemon?.heartbeatTimeout)
Expand Down
4 changes: 0 additions & 4 deletions packages/cordis/src/index.ts
Expand Up @@ -9,10 +9,6 @@ export { TimerService } from '@cordisjs/timer'

export interface Events<C extends Context = Context> extends core.Events<C> {}

export namespace Context {
export type Associate<P extends string, C extends Context = Context> = core.Context.Associate<P, C>
}

export interface Context {
[Context.events]: Events<this>
}
Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/context.ts
Expand Up @@ -33,10 +33,6 @@ export namespace Context {
name: string
}
}

export type Associate<P extends string, C extends Context = Context> = {
[K in keyof C as K extends `${P}.${infer R}` ? R : never]: C[K]
}
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -148,13 +144,13 @@ export class Context {
static associate<T extends {}>(object: T, name: string) {
return new Proxy(object, {
get(target, key, receiver) {
if (typeof key === 'symbol' || key in target) return Reflect.get(target, key, receiver)
if (typeof key === 'symbol') return Reflect.get(target, key, receiver)
const caller: Context = receiver[symbols.origin]
if (!caller?.[symbols.internal][`${name}.${key}`]) return Reflect.get(target, key, receiver)
return caller.get(`${name}.${key}`)
return caller[`${name}.${key}`]
},
set(target, key, value, receiver) {
if (typeof key === 'symbol' || key in target) return Reflect.set(target, key, value, receiver)
if (typeof key === 'symbol') return Reflect.set(target, key, value, receiver)
const caller: Context = receiver[symbols.origin]
if (!caller?.[symbols.internal][`${name}.${key}`]) return Reflect.set(target, key, value, receiver)
caller[`${name}.${key}`] = value
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/registry.ts
@@ -1,4 +1,4 @@
import { defineProperty } from 'cosmokit'
import { Dict, defineProperty } from 'cosmokit'
import { Context } from './context.ts'
import { ForkScope, MainScope } from './scope.ts'
import { resolveConfig } from './utils.ts'
Expand All @@ -24,6 +24,7 @@ export namespace Plugin {
reusable?: boolean
Config?: (config: any) => T
inject?: string[] | Inject
intercept?: Dict<boolean>
}

export interface Transform<S, T> {
Expand Down

0 comments on commit d43f175

Please sign in to comment.