コンテキスト
コンテキストは Cordis の中核オブジェクトです。すべてのサービス、イベント、ライフサイクル API には ctx を通じてアクセスします。イベントメソッドについては イベント、エフェクトと現在のファイバーについては ファイバー、プラグインの読み込みについては レジストリを参照してください。
Cordis プラグイン用のルートおよび子依存関係コンテナです。
コンテキストはプロキシです。通常のプロパティ読み取りはサービスリゾルバーを経由します。一方、extend()、isolate()、intercept()は、親を変更せずにスコープ付きの子コンテキストを作成します。
ctx.extend(meta?)
/**
* 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— 子に定義する自身のプロパティ(symbol キーを含む)。
戻り値 このコンテキストを継承する子コンテキスト。
ctx.isolate(name, label?)
/**
* 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 の読み取りと書き込みは親ではなく新しいラベルに対して解決されます。そのため、親スコープに影響を与えずに別の実装を提供できます。同じ label を 2 回の isolate() 呼び出しに渡すと、それらのスコープは結合されます。
name— 分離するサービス名。label— 結合するスコープラベル。既定値は新しい一意の symbol です。
戻り値 name サービスが新しいスコープで解決される子コンテキスト。
ctx.intercept(name, config)
/**
* 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このコンテキストの下で開始されるプラグインに、サービス固有のインターセプト設定を追加します。
返されるコンテキストの下で読み込まれたプラグインでは、config がサービスの解決済み設定にマージされます(祖先のエントリが先。Service[symbols.resolveConfig]を参照)。親コンテキストには影響しません。
name— 設定をインターセプトするサービス名。config— そのサービス用にマージするインターセプト設定。
戻り値 追加のインターセプトエントリを保持する子コンテキスト。
ctx.root
/** The root context of the application (every child context shares it). @experimental */
root: thisアプリケーションのルートコンテキストです(すべての子コンテキストがこれを共有します)。@experimental
ctx.baseUrl
/** Base URL used to resolve relative plugin/module specifiers, if the runtime sets one. */
baseUrl?: stringランタイムで設定されている場合に、相対プラグイン/モジュール指定子の解決に使用されるベース URL です。
ctx.events
/** The event bus. Its methods are also mixed onto `ctx` (`ctx.on`, `ctx.emit`, ...). */
events: EventsServiceイベントバスです。そのメソッドは ctx(ctx.on、ctx.emit、…)にもミックスインされます。
ctx.logger
/** The logging service. Call `ctx.logger(name)` for a named logger. */
logger: LoggerServiceロギングサービスです。名前付きロガーを取得するには ctx.logger(name) を呼び出します。
ctx.reflect
/** The reflection layer backing the context proxy (`ctx.get`, `ctx.provide`, ...). */
reflect: ReflectServiceコンテキストプロキシを支えるリフレクション層です(ctx.get、ctx.provide、…)。
ctx.レジストリ
/** The plugin registry. Its methods are mixed onto `ctx` (`ctx.plugin`, `ctx.inject`). */
registry: RegistryServiceプラグインレジストリです。そのメソッドは ctx(ctx.plugin、ctx.inject)にもミックスインされます。
静的メンバー
Context.effect
/** Symbol key under which a disposer exposes its {@link EffectMeta} diagnostics tree. */
static readonly effect: unique symboldisposer が EffectMeta 診断ツリーを公開する際に使用する symbol キーです。
Context.filter
/** Symbol key for a context's listener filter, consulted on every event dispatch. */
static readonly filter: unique symbolイベントのディスパッチごとに参照される、コンテキストのリスナーフィルター用 symbol キーです。
Context.isolate
/** Symbol key of the isolation map (see the `Context[symbols.isolate]` property). */
static readonly isolate: unique symbol分離マップの symbol キーです(Context[symbols.isolate]プロパティを参照)。
Context.intercept
/** Symbol key of the intercept map (see the `Context[symbols.intercept]` property). */
static readonly intercept: unique symbolインターセプトマップの symbol キーです(Context[symbols.intercept]プロパティを参照)。
Context.is(value)
/**
* 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 ContextCordis のコンテキストプロキシおよびコンテキストプロトタイプに対して true を返します。
ブランドは instanceof ではなくグローバル symbol をキーとしているため、realm をまたぐ場合や複数の cordis コピー間でも動作します。
value— テストする値。
戻り値 true が Cordis コンテキストの場合に value を返し、その型を絞り込みます。
サービスストアとミックスイン
ctx.get(name, strict?)
/**
* 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): anyinject 要件なしでストアからサービスを読み取ります。
name— サービス名です。strict—true(デフォルト)の場合、提供元のファイバーが現在アクティブな実装のみを返します。
戻り値 まだ提供されていない場合はサービス値、または undefined を返します。
ctx.set(name, value)
/**
* 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提供済みサービスの値を上書きします。
サービスを提供したファイバーだけが値を設定できます。未提供の名前を設定すると例外がスローされます。
name— サービス名です。value— 新しいサービス値です。
ctx.provide(name, value)
/**
* 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現在のファイバーが所有するサービス実装を登録します。
ファイバーがアクティブになると、同じ分離スコープ内の依存先からサービスが見えるようになります。返された disposer が実行されるかファイバーがアンロードされると、サービスは登録解除され(依存先が起こされます)。このスコープですでに名前が提供されている場合、またはアクセサーとして宣言されている場合は例外がスローされます。
name— サービス名です。value— サービス値です。
戻り値 サービスを登録解除する disposer を返します。
ctx.accessor(name, options)
/**
* 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'>): voidget/set フックに基づく計算済みコンテキストプロパティを定義します。
現在のファイバーがアンロードされるとアクセサーは削除されます。名前がすでに宣言されている場合は例外がスローされます。
name— コンテキストプロパティ名です。options—getフックと、任意のsetフックです。
ctx.mixin(name, mixins)
/**
* 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 上に直接公開します。
ミックスインした各キーは、サービスへ転送するアクセサーになります(メソッドはサービスにバインドされます)。たとえば、ctx.on は ctx.events.on に転送されます。現在のファイバーがアンロードされるとミックスインは削除されます。
name— ソースサービスを保持するコンテキストプロパティです。mixins— 転送するキー、またはソースキー → ctx キーのマップです。