Skip to content

Commit

Permalink
refa: rename trace to origin
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Mar 3, 2024
1 parent 6327f6f commit 37d62a8
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 30 deletions.
14 changes: 7 additions & 7 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface Context {
}

export class Context {
static readonly trace: unique symbol = symbols.trace as any
static readonly origin: unique symbol = symbols.origin as any
static readonly events: unique symbol = symbols.events as any
static readonly static: unique symbol = symbols.static as any
static readonly filter: unique symbol = symbols.filter as any
Expand All @@ -62,7 +62,7 @@ export class Context {
static readonly internal: unique symbol = symbols.internal as any
static readonly intercept: unique symbol = symbols.intercept as any
/** @deprecated use `Context.trace` instead */
static readonly current: typeof Context.trace = Context.trace
static readonly current: typeof Context.origin = Context.origin

static is<C extends Context>(value: any): value is C {
return !!value?.[Context.is as any]
Expand Down Expand Up @@ -167,7 +167,7 @@ export class Context {
ctx.root.emit(self, 'internal/before-service', name, value)
ctx.root[key] = value
if (value instanceof Object) {
defineProperty(value, symbols.trace, ctx)
defineProperty(value, symbols.origin, ctx)
}
ctx.root.emit(self, 'internal/service', name, oldValue)
return true
Expand All @@ -178,13 +178,13 @@ export class Context {
return new Proxy(object, {
get(target, key, receiver) {
if (typeof key === 'symbol' || key in target) return Reflect.get(target, key, receiver)
const caller: Context = receiver[symbols.trace]
const caller: Context = receiver[symbols.origin]
if (!caller?.[symbols.internal][`${name}.${key}`]) return Reflect.get(target, key, receiver)
return caller.get(`${name}.${key}`)
},
set(target, key, value, receiver) {
if (typeof key === 'symbol' || key in target) return Reflect.set(target, key, value, receiver)
const caller: Context = receiver[symbols.trace]
const caller: Context = receiver[symbols.origin]
if (!caller?.[symbols.internal][`${name}.${key}`]) return Reflect.set(target, key, value, receiver)
caller[`${name}.${key}`] = value
return true
Expand All @@ -211,7 +211,7 @@ export class Context {
const constructor = internal[key]['prototype']?.constructor
if (!constructor) continue
self[internal[key]['key']] = new constructor(self, config)
defineProperty(self[internal[key]['key']], symbols.trace, self)
defineProperty(self[internal[key]['key']], symbols.origin, self)
}
}
attach(this[symbols.internal])
Expand Down Expand Up @@ -247,7 +247,7 @@ export class Context {
const value = this.root[this[symbols.isolate][name]]
if (!value || typeof value !== 'object' && typeof value !== 'function') return value
if (isUnproxyable(value)) {
defineProperty(value, symbols.trace, this)
defineProperty(value, symbols.origin, this)
return value
}
return createTraceable(this, value)
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Lifecycle {
_hooks: Record<keyof any, [Context, (...args: any[]) => any][]> = {}

constructor(private root: Context) {
defineProperty(this, Context.trace, root)
defineProperty(this, Context.origin, root)

defineProperty(this.on('internal/listener', function (this: Context, name, listener, prepend) {
const method = prepend ? 'unshift' : 'push'
Expand Down Expand Up @@ -84,7 +84,7 @@ export class Lifecycle {
if (name !== 'internal/event') {
this.emit('internal/event', type, name, args, thisArg)
}
return [this.getHooks(name, thisArg), thisArg ?? this[Context.trace]] as const
return [this.getHooks(name, thisArg), thisArg ?? this[Context.origin]] as const
}

async parallel(...args: any[]) {
Expand Down Expand Up @@ -118,7 +118,7 @@ export class Lifecycle {
}

register(label: string, hooks: [Context, any][], listener: any, prepend?: boolean) {
const caller = this[Context.trace]
const caller = this[Context.origin]
const method = prepend ? 'unshift' : 'push'
hooks[method]([caller, listener])
return caller.state.collect(label, () => this.unregister(hooks, listener))
Expand All @@ -134,7 +134,7 @@ export class Lifecycle {

on(name: string, listener: (...args: any) => any, prepend = false) {
// handle special events
const caller: Context = this[Context.trace]
const caller: Context = this[Context.origin]
caller.scope.assertActive()
const result = this.bail(caller, 'internal/listener', name, listener, prepend)
if (result) return result
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ export namespace Plugin {
reusable?: boolean
Config?: (config: any) => T
inject?: string[] | Inject
/** @deprecated use `inject` instead */
using?: string[] | Inject
}

export interface Transform<S, T> {
Expand Down Expand Up @@ -67,7 +65,7 @@ export class Registry<C extends Context = Context> {
private _internal = new Map<Plugin, MainScope<C>>()

constructor(private root: Context, config: any) {
defineProperty(this, Context.trace, root)
defineProperty(this, Context.origin, root)
root.scope = new MainScope(this, null!, config)
root.scope.runtime.isReactive = true
}
Expand Down Expand Up @@ -139,7 +137,7 @@ export class Registry<C extends Context = Context> {
// check if it's a valid plugin
this.resolve(plugin)

const context: Context = this[Context.trace]
const context: Context = this[Context.origin]
context.scope.assertActive()

// resolve plugin config
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ export class MainScope<C extends Context = Context> extends EffectScope<C> {
isReactive?: boolean = false

constructor(registry: Registry<C>, public plugin: Plugin, config: any, error?: any) {
super(registry[Context.trace] as C, config)
super(registry[Context.origin] as C, config)
registry.set(plugin, this)
if (!plugin) {
this.name = 'root'
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export abstract class Service<T = unknown, C extends Context = Context> {
protected fork?(ctx: C, config: any): void

protected ctx!: C
protected [symbols.trace]!: C
protected [symbols.origin]!: C

public name!: string
public config!: T
Expand Down Expand Up @@ -50,7 +50,7 @@ export abstract class Service<T = unknown, C extends Context = Context> {
}
self.name = name
self.config = config
defineProperty(self, symbols.trace, self.ctx)
defineProperty(self, symbols.origin, self.ctx)

self.ctx.provide(name)
self.ctx.runtime.name = name
Expand Down Expand Up @@ -79,14 +79,14 @@ export abstract class Service<T = unknown, C extends Context = Context> {
}

protected [symbols.extend](props?: any) {
const caller = this[symbols.trace]
const caller = this[symbols.origin]
let self: any
if (this[Service.invoke]) {
self = createCallable(this.name, this)
} else {
self = Object.create(this)
}
defineProperty(self, symbols.trace, caller)
defineProperty(self, symbols.origin, caller)
return Context.associate(Object.assign(self, props), this.name)
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Context, Service } from './index.ts'

export const symbols = {
// context symbols
trace: Symbol.for('cordis.trace') as typeof Context.trace,
origin: Symbol.for('cordis.origin') as typeof Context.origin,
events: Symbol.for('cordis.events') as typeof Context.events,
static: Symbol.for('cordis.static') as typeof Context.static,
filter: Symbol.for('cordis.filter') as typeof Context.filter,
Expand Down Expand Up @@ -50,7 +50,7 @@ export function joinPrototype(proto1: {}, proto2: {}) {
export function createTraceable(ctx: any, value: any) {
const proxy = new Proxy(value, {
get: (target, name, receiver) => {
if (name === symbols.trace || name === 'caller') return ctx
if (name === symbols.origin || name === 'caller') return ctx
return Reflect.get(target, name, receiver)
},
apply: (target, thisArg, args) => {
Expand All @@ -67,7 +67,7 @@ export function applyTraceable(proxy: any, value: any, thisArg: any, args: any[]

export function createCallable(name: string, proto: {}) {
const self = function (...args: any[]) {
const proxy = createTraceable(self[symbols.trace], self)
const proxy = createTraceable(self[symbols.origin], self)
return applyTraceable(proxy, self, this, args)
}
defineProperty(self, 'name', name)
Expand Down
6 changes: 3 additions & 3 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface LoggerService extends Pick<Logger, Logger.Type | 'extend'> {
}

export class LoggerService extends Service {
static name = 'logger'
static [Service.provide] = 'logger'

constructor(ctx: Context) {
super(ctx, 'logger', true)
Expand All @@ -33,13 +33,13 @@ export class LoggerService extends Service {
}

[Service.invoke](name: string) {
return new Logger(name, { [Context.trace]: this })
return new Logger(name, { [Context.origin]: this })
}

static {
for (const type of ['success', 'error', 'info', 'warn', 'debug', 'extend'] as const) {
LoggerService.prototype[type] = function (this: any, ...args: any[]) {
const caller: Context = this[Context.trace]
const caller: Context = this[Context.origin]
return this(caller.name)[type](...args)
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/timer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class TimerService extends Service {
}

setTimeout(callback: () => void, delay: number) {
const dispose = this[Context.trace].effect(() => {
const dispose = this[Context.origin].effect(() => {
const timer = setTimeout(() => {
dispose()
callback()
Expand All @@ -32,14 +32,14 @@ export class TimerService extends Service {
}

setInterval(callback: () => void, delay: number) {
return this[Context.trace].effect(() => {
return this[Context.origin].effect(() => {
const timer = setInterval(callback, delay)
return () => clearInterval(timer)
})
}

sleep(delay: number) {
const caller = this[Context.trace]
const caller = this[Context.origin]
return new Promise<void>((resolve, reject) => {
const dispose1 = this.setTimeout(() => {
dispose1()
Expand All @@ -55,7 +55,7 @@ export class TimerService extends Service {
}

private createWrapper(callback: (args: any[], check: () => boolean) => any, isDisposed = false) {
const caller = this[Context.trace]
const caller = this[Context.origin]
caller.scope.assertActive()

let timer: number | NodeJS.Timeout | undefined
Expand Down

0 comments on commit 37d62a8

Please sign in to comment.