본문으로 이동

컨텍스트

컨텍스트는 핵심 Cordis 객체입니다. 모든 서비스, 이벤트 및 수명 주기 API는 ctx를 통해 접근합니다. 이벤트 메서드는 이벤트에서, 효과와 현재 fiber는 Fiber에서, 플러그인 로딩은 레지스트리에서 문서화되어 있습니다.

Cordis 플러그인을 위한 루트 및 자식 의존성 컨테이너입니다.

컨텍스트는 프록시입니다. 일반 속성 읽기는 서비스 리졸버를 거치며, extend(), isolate(), intercept()는 부모를 변경하지 않고 범위가 지정된 자식 컨텍스트를 생성합니다.

소스

ctx.extend(meta?)

ts
/**
 * Create a child context with extra metadata on top of the current scope.
 *
 * The child prototypally inherits every property of this context; own
 * properties of `meta` shadow the inherited ones. The parent is not mutated.
 *
 * @param meta — own properties (including symbol keys) to define on the child.
 * @returns a child context inheriting from this one.
 */
extend(meta = {}): this

현재 범위에 추가 메타데이터를 더한 자식 컨텍스트를 생성합니다.

자식은 이 컨텍스트의 모든 속성을 프로토타입으로 상속합니다. meta의 자체 속성은 상속된 속성을 가립니다. 부모는 변경되지 않습니다.

  • meta — 자식에 정의할 자체 속성(심볼 키 포함)입니다.

반환값 이 컨텍스트를 상속하는 자식 컨텍스트입니다.

소스

ctx.isolate(name, label?)

ts
/**
 * Create a child context with an independent service scope for `name`.
 *
 * Below the returned context, reads and writes of the service `name`
 * resolve against the new label instead of the parent's, so a different
 * implementation can be provided without affecting the parent scope.
 * Passing the same `label` to two `isolate()` calls joins their scopes.
 *
 * @param name — the service name to isolate.
 * @param label — scope label to join; defaults to a fresh unique symbol.
 * @returns a child context whose `name` service resolves in the new scope.
 */
isolate(name: string, label?: symbol)

name에 대해 독립적인 서비스 범위를 가진 자식 컨텍스트를 생성합니다.

반환된 컨텍스트 아래에서는 서비스 name의 읽기와 쓰기가 부모가 아닌 새 레이블을 기준으로 해석되므로, 부모 범위에 영향을 주지 않고 다른 구현을 제공할 수 있습니다. 두 isolate() 호출에 동일한 label를 전달하면 해당 범위에 참여합니다.

  • name — 격리할 서비스 이름입니다.
  • label — 참여할 범위 레이블이며, 기본값은 새 고유 심볼입니다.

반환값 name 서비스가 새 범위에서 해석되는 자식 컨텍스트입니다.

소스

ctx.intercept(name, config)

ts
/**
 * Add service-specific intercept config for plugins started below this
 * context.
 *
 * Plugins loaded under the returned context see `config` merged into the
 * service's resolved config (ancestor entries first; see
 * `Service[symbols.resolveConfig]`). The parent context is not affected.
 *
 * @param name — the service name whose config to intercept.
 * @param config — the intercept config to merge for that service.
 * @returns a child context carrying the additional intercept entry.
 */
intercept<K extends InjectKey>(name: K, config: Context[K] extends { [symbols.config]: infer T } ? T : never): this
intercept(name: string, config: any): this

이 컨텍스트 아래에서 시작된 플러그인에 서비스별 intercept 설정을 추가합니다.

반환된 컨텍스트 아래에서 로드된 플러그인은 서비스의 해석된 설정에 병합된 config를 확인합니다(상위 항목 우선, Service[symbols.resolveConfig] 참조). 부모 컨텍스트에는 영향을 주지 않습니다.

  • name — 설정을 intercept할 서비스 이름입니다.
  • config — 해당 서비스에 병합할 intercept 설정입니다.

반환값 추가 intercept 항목을 포함하는 자식 컨텍스트입니다.

소스

ctx.root

ts
/** The root context of the application (every child context shares it). @experimental */
root: this

애플리케이션의 루트 컨텍스트입니다(모든 자식 컨텍스트가 이를 공유함). @experimental

소스

ctx.baseUrl

ts
/** Base URL used to resolve relative plugin/module specifiers, if the runtime sets one. */
baseUrl?: string

런타임에서 설정한 경우 상대 플러그인/모듈 지정자를 해석하는 데 사용하는 기본 URL입니다.

소스

ctx.events

ts
/** The event bus. Its methods are also mixed onto `ctx` (`ctx.on`, `ctx.emit`, ...). */
events: EventsService

이벤트 버스입니다. 해당 메서드는 ctx에도 혼합되어 있습니다(ctx.on, ctx.emit, ...).

소스

ctx.logger

ts
/** The logging service. Call `ctx.logger(name)` for a named logger. */
logger: LoggerService

로깅 서비스입니다. 이름이 지정된 로거에는 ctx.logger(name)를 호출합니다.

소스

ctx.reflect

ts
/** The reflection layer backing the context proxy (`ctx.get`, `ctx.provide`, ...). */
reflect: ReflectService

컨텍스트 프록시를 지원하는 리플렉션 계층입니다(ctx.get, ctx.provide, ...).

소스

ctx.레지스트리

ts
/** The plugin registry. Its methods are mixed onto `ctx` (`ctx.plugin`, `ctx.inject`). */
registry: RegistryService

플러그인 레지스트리입니다. 해당 메서드는 ctx에 혼합되어 있습니다(ctx.plugin, ctx.inject).

소스

정적 멤버

Context.effect

ts
/** Symbol key under which a disposer exposes its {@link EffectMeta} diagnostics tree. */
static readonly effect: unique symbol

disposer가 EffectMeta 진단 트리를 노출하는 심볼 키입니다.

소스

Context.filter

ts
/** Symbol key for a context's listener filter, consulted on every event dispatch. */
static readonly filter: unique symbol

컨텍스트의 리스너 필터를 위한 심볼 키이며, 모든 이벤트 디스패치에서 참조됩니다.

소스

Context.isolate

ts
/** Symbol key of the isolation map (see the `Context[symbols.isolate]` property). */
static readonly isolate: unique symbol

격리 맵의 심볼 키입니다(Context[symbols.isolate] 속성 참조).

소스

Context.intercept

ts
/** Symbol key of the intercept map (see the `Context[symbols.intercept]` property). */
static readonly intercept: unique symbol

intercept 맵의 심볼 키입니다(Context[symbols.intercept] 속성 참조).

소스

Context.is(value)

ts
/**
 * Returns true for Cordis context proxies and context prototypes.
 *
 * Works across realms and across multiple copies of cordis, because the
 * brand is keyed by a global symbol rather than by `instanceof`.
 *
 * @param value — the value to test.
 * @returns `true` if `value` is a Cordis context, narrowing its type.
 */
static is(value: any): value is Context

Cordis 컨텍스트 프록시 및 컨텍스트 프로토타입에 대해 true를 반환합니다.

브랜드가 instanceof가 아닌 전역 심볼을 키로 사용하므로, realm 간 및 cordis의 여러 복사본 간에도 작동합니다.

  • value — 테스트할 값입니다.

반환값 true가 Cordis 컨텍스트이면 value를 반환하며, 해당 타입을 좁힙니다.

소스

서비스 저장소 및 믹스인

ctx.get(name, strict?)

ts
/**
 * Read a service from the store without the inject requirement.
 *
 * @param name — the service name.
 * @param strict — when `true` (default), only return implementations
 * whose providing fiber is currently active.
 * @returns the service value, or `undefined` when not (yet) provided.
 */
get<K extends string & keyof this>(name: K, strict?: boolean): undefined | this[K]
get(name: string, strict?: boolean): any

inject 요구 사항 없이 저장소에서 서비스를 읽습니다.

  • name — 서비스 이름입니다.
  • stricttrue(기본값)인 경우, 제공하는 fiber가 현재 활성 상태인 구현만 반환합니다.

반환값 은 서비스 값이며, 아직 제공되지 않은 경우 undefined입니다.

소스

ctx.set(name, value)

ts
/**
 * Overwrite a provided service's value.
 *
 * Only the fiber that provided the service may set it; setting an
 * unprovided name throws.
 *
 * @param name — the service name.
 * @param value — the new service value.
 */
set<K extends string & keyof this>(name: K, value: undefined | this[K]): void
set(name: string, value: any): void

제공된 서비스의 값을 덮어씁니다.

서비스를 제공한 fiber만 값을 설정할 수 있으며, 제공되지 않은 이름을 설정하면 예외가 발생합니다.

  • name — 서비스 이름입니다.
  • value — 새 서비스 값입니다.

소스

ctx.provide(name, value)

ts
/**
 * Register a service implementation owned by the current fiber.
 *
 * The service becomes visible to dependents in the same isolation scope
 * once the fiber is active; it is unregistered (waking dependents) when
 * the returned disposer runs or the fiber unloads. Throws if the name is
 * already provided in this scope or declared as an accessor.
 *
 * @param name — the service name.
 * @param value — the service value.
 * @returns a disposer that unregisters the service.
 */
provide<K extends string & keyof this>(name: K, value: undefined | this[K]): () => void
provide(name: string, value?: any): () => void

현재 fiber가 소유하는 서비스 구현을 등록합니다.

fiber가 활성화되면 서비스는 동일한 격리 범위의 종속 항목에 표시됩니다. 반환된 disposer가 실행되거나 fiber가 언로드되면 서비스 등록이 해제되어 종속 항목을 깨웁니다. 이 범위에서 이름이 이미 제공되었거나 accessor로 선언된 경우 예외가 발생합니다.

  • name — 서비스 이름입니다.
  • value — 서비스 값입니다.

반환값 은 서비스 등록을 해제하는 disposer입니다.

소스

ctx.accessor(name, options)

ts
/**
 * Define a computed context property backed by get/set hooks.
 *
 * The accessor is removed when the current fiber unloads. Throws if the
 * name is already declared.
 *
 * @param name — the context property name.
 * @param options — the `get` hook and optional `set` hook.
 */
accessor(name: string, options: Omit<Property.Accessor, 'type'>): void

get/set hook을 기반으로 계산된 컨텍스트 속성을 정의합니다.

현재 fiber가 언로드되면 accessor가 제거됩니다. 이름이 이미 선언된 경우 예외가 발생합니다.

  • name — 컨텍스트 속성 이름입니다.
  • optionsget hook과 선택적 set hook입니다.

소스

ctx.mixin(name, mixins)

ts
/**
 * Expose selected members of a service directly on `ctx`.
 *
 * Each mixed-in key becomes an accessor that forwards to the service
 * (binding methods to it), so e.g. `ctx.on` forwards to `ctx.events.on`.
 * Mixins are removed when the current fiber unloads.
 *
 * @param name — the context property holding the source service.
 * @param mixins — keys to forward, or a source-key → ctx-key map.
 */
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

서비스의 선택한 멤버를 ctx에 직접 노출합니다.

혼합된 각 키는 서비스로 전달하는 accessor가 되며(메서드는 서비스에 바인딩됨), 예를 들어 ctx.onctx.events.on로 전달됩니다. 현재 fiber가 언로드되면 mixin이 제거됩니다.

  • name — 소스 서비스를 보유하는 컨텍스트 속성입니다.
  • mixins — 전달할 키 또는 소스 키 → ctx 키 매핑입니다.

소스