@cordisjs/core
Advanced tools
Comparing version 3.12.0 to 3.13.0
@@ -12,3 +12,3 @@ import { Awaitable, Promisify, Dict } from 'cosmokit'; | ||
protected ctx: C; | ||
protected [symbols.trace]: C; | ||
protected [symbols.origin]: C; | ||
name: string; | ||
@@ -24,5 +24,4 @@ config: T; | ||
} | ||
import type { Context, Service } from "index"; | ||
export const symbols: { | ||
trace: typeof Context.trace; | ||
origin: typeof Context.origin; | ||
events: typeof Context.events; | ||
@@ -61,4 +60,2 @@ static: typeof Context.static; | ||
inject?: string[] | Inject; | ||
/** @deprecated use `inject` instead */ | ||
using?: string[] | Inject; | ||
} | ||
@@ -322,3 +319,3 @@ interface Transform<S, T> { | ||
export class Context { | ||
static readonly trace: unique symbol; | ||
static readonly origin: unique symbol; | ||
static readonly events: unique symbol; | ||
@@ -331,4 +328,4 @@ static readonly static: unique symbol; | ||
static readonly intercept: unique symbol; | ||
/** @deprecated use `Context.trace` instead */ | ||
static readonly current: typeof Context.trace; | ||
/** @deprecated use `Context.origin` instead */ | ||
static readonly current: typeof Context.origin; | ||
static is<C extends Context>(value: any): value is C; | ||
@@ -346,2 +343,3 @@ private static ensureInternal; | ||
get(name: string): any; | ||
set(name: string, value: any): void; | ||
provide(name: string, value?: any, builtin?: boolean): void; | ||
@@ -348,0 +346,0 @@ accessor(name: string, options: Omit<Context.Internal.Accessor, 'type'>): void; |
{ | ||
"name": "@cordisjs/core", | ||
"description": "Meta-Framework for Modern JavaScript Applications", | ||
"version": "3.12.0", | ||
"version": "3.13.0", | ||
"sideEffects": false, | ||
@@ -6,0 +6,0 @@ "type": "module", |
@@ -56,3 +56,3 @@ import { defineProperty, Dict, isNullable } from 'cosmokit' | ||
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 | ||
@@ -65,4 +65,4 @@ static readonly static: unique symbol = symbols.static as any | ||
static readonly intercept: unique symbol = symbols.intercept as any | ||
/** @deprecated use `Context.trace` instead */ | ||
static readonly current: typeof Context.trace = Context.trace | ||
/** @deprecated use `Context.origin` instead */ | ||
static readonly current: typeof Context.origin = Context.origin | ||
@@ -126,7 +126,5 @@ static is<C extends Context>(value: any): value is C { | ||
return Reflect.get(target, name, ctx) | ||
} | ||
if (internal.type === 'accessor') { | ||
} else if (internal.type === 'accessor') { | ||
return internal.get.call(ctx) | ||
} else if (internal.type === 'service') { | ||
} else { | ||
if (!internal.builtin) checkInject(name) | ||
@@ -145,34 +143,7 @@ return ctx.get(name) | ||
return internal.set.call(ctx, value) | ||
} else { | ||
ctx.emit('internal/warning', new Error(`Assigning to service ${name} is not recommended, please use \`ctx.set()\` method instead`)) | ||
ctx.set(name, value) | ||
return true | ||
} | ||
// service | ||
const key = ctx[symbols.isolate][name] | ||
const oldValue = ctx.root[key] | ||
if (oldValue === value) return true | ||
// check override | ||
if (value && oldValue) { | ||
throw new Error(`service ${name} has been registered`) | ||
} | ||
if (value) { | ||
ctx.on('dispose', () => ctx[name] = undefined) | ||
} | ||
if (isUnproxyable(value)) { | ||
ctx.emit('internal/warning', new Error(`service ${name} is an unproxyable object, which may lead to unexpected behavior`)) | ||
} | ||
// setup filter for events | ||
const self = Object.create(null) | ||
self[symbols.filter] = (ctx2: Context) => { | ||
// TypeScript is not smart enough to infer the type of `name` here | ||
return ctx[symbols.isolate][name as string] === ctx2[symbols.isolate][name as string] | ||
} | ||
ctx.root.emit(self, 'internal/before-service', name, value) | ||
ctx.root[key] = value | ||
if (value instanceof Object) { | ||
defineProperty(value, symbols.trace, ctx) | ||
} | ||
ctx.root.emit(self, 'internal/service', name, oldValue) | ||
return true | ||
}, | ||
@@ -185,3 +156,3 @@ } | ||
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) | ||
@@ -192,3 +163,3 @@ return caller.get(`${name}.${key}`) | ||
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) | ||
@@ -220,3 +191,3 @@ caller[`${name}.${key}`] = value | ||
self[internal[key]['key']] = new constructor(self, config) | ||
defineProperty(self[internal[key]['key']], symbols.trace, self) | ||
defineProperty(self[internal[key]['key']], symbols.origin, self) | ||
} | ||
@@ -257,3 +228,3 @@ } | ||
if (isUnproxyable(value)) { | ||
defineProperty(value, symbols.trace, this) | ||
defineProperty(value, symbols.origin, this) | ||
return value | ||
@@ -264,2 +235,35 @@ } | ||
set(name: string, value: any) { | ||
this.provide(name) | ||
const key = this[symbols.isolate][name] | ||
const oldValue = this.root[key] | ||
value ??= undefined | ||
if (oldValue === value) return | ||
// check override | ||
if (!isNullable(value) && !isNullable(oldValue)) { | ||
throw new Error(`service ${name} has been registered`) | ||
} | ||
const ctx: Context = this | ||
if (!isNullable(value)) { | ||
ctx.on('dispose', () => ctx.set(name, undefined)) | ||
} | ||
if (isUnproxyable(value)) { | ||
ctx.emit('internal/warning', new Error(`service ${name} is an unproxyable object, which may lead to unexpected behavior`)) | ||
} | ||
// setup filter for events | ||
const self = Object.create(null) | ||
self[symbols.filter] = (ctx2: Context) => { | ||
return ctx[symbols.isolate][name] === ctx2[symbols.isolate][name] | ||
} | ||
ctx.emit(self, 'internal/before-service', name, value) | ||
ctx.root[key] = value | ||
if (value instanceof Object) { | ||
defineProperty(value, symbols.origin, ctx) | ||
} | ||
ctx.emit(self, 'internal/service', name, oldValue) | ||
} | ||
provide(name: string, value?: any, builtin?: boolean) { | ||
@@ -266,0 +270,0 @@ const internal = Context.ensureInternal.call(this.root) |
@@ -41,3 +41,3 @@ import { Awaitable, defineProperty, Promisify, remove } from 'cosmokit' | ||
constructor(private root: Context) { | ||
defineProperty(this, Context.trace, root) | ||
defineProperty(this, Context.origin, root) | ||
@@ -88,3 +88,3 @@ defineProperty(this.on('internal/listener', function (this: Context, name, listener, prepend) { | ||
} | ||
return [this.getHooks(name, thisArg), thisArg ?? this[Context.trace]] as const | ||
return [this.getHooks(name, thisArg), thisArg ?? this[Context.origin]] as const | ||
} | ||
@@ -123,3 +123,3 @@ | ||
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' | ||
@@ -140,3 +140,3 @@ hooks[method]([caller, listener]) | ||
// handle special events | ||
const caller: Context = this[Context.trace] | ||
const caller: Context = this[Context.origin] | ||
caller.scope.assertActive() | ||
@@ -143,0 +143,0 @@ const result = this.bail(caller, 'internal/listener', name, listener, prepend) |
@@ -27,4 +27,2 @@ import { defineProperty } from 'cosmokit' | ||
inject?: string[] | Inject | ||
/** @deprecated use `inject` instead */ | ||
using?: string[] | Inject | ||
} | ||
@@ -71,3 +69,3 @@ | ||
constructor(private root: Context, config: any) { | ||
defineProperty(this, Context.trace, root) | ||
defineProperty(this, Context.origin, root) | ||
root.scope = new MainScope(this, null!, config) | ||
@@ -144,3 +142,3 @@ root.scope.runtime.isReactive = true | ||
const context: Context = this[Context.trace] | ||
const context: Context = this[Context.origin] | ||
context.scope.assertActive() | ||
@@ -147,0 +145,0 @@ |
@@ -332,3 +332,3 @@ import { deepEqual, defineProperty, isNullable, remove } from 'cosmokit' | ||
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) | ||
@@ -390,3 +390,3 @@ if (!plugin) { | ||
if (name) { | ||
context[name] = instance | ||
context.set(name, instance) | ||
} | ||
@@ -393,0 +393,0 @@ if (instance['fork']) { |
@@ -18,3 +18,3 @@ import { Awaitable, defineProperty } from 'cosmokit' | ||
protected ctx!: C | ||
protected [symbols.trace]!: C | ||
protected [symbols.origin]!: C | ||
@@ -54,3 +54,3 @@ public name!: string | ||
self.config = config | ||
defineProperty(self, symbols.trace, self.ctx) | ||
defineProperty(self, symbols.origin, self.ctx) | ||
@@ -61,3 +61,3 @@ self.ctx.provide(name) | ||
if (_ctx) self[symbols.expose] = name | ||
else self.ctx[name] = self | ||
else self.ctx.set(name, self) | ||
} | ||
@@ -69,3 +69,3 @@ | ||
await self.start() | ||
if (!immediate) self.ctx[name!] = self | ||
if (!immediate) self.ctx.set(name!, self) | ||
}) | ||
@@ -86,3 +86,3 @@ | ||
protected [symbols.extend](props?: any) { | ||
const caller = this[symbols.trace] | ||
const caller = this[symbols.origin] | ||
let self: any | ||
@@ -94,3 +94,3 @@ if (this[Service.invoke]) { | ||
} | ||
defineProperty(self, symbols.trace, caller) | ||
defineProperty(self, symbols.origin, caller) | ||
return Context.associate(Object.assign(self, props), this.name) | ||
@@ -97,0 +97,0 @@ } |
@@ -6,3 +6,3 @@ import { defineProperty } from 'cosmokit' | ||
// 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, | ||
@@ -54,3 +54,3 @@ static: Symbol.for('cordis.static') as typeof Context.static, | ||
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) | ||
@@ -72,3 +72,3 @@ }, | ||
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) | ||
@@ -75,0 +75,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
200419
3622