上下文
Context.effect
Section titled “Context.effect”/** Symbol key under which a disposer exposes its {@link EffectMeta} diagnostics tree. */static readonly effect: unique symbol资源释放函数用于公开其 EffectMeta 诊断树的 symbol 键。
Context.filter
Section titled “Context.filter”/** Symbol key for a context's listener filter, consulted on every event dispatch. */static readonly filter: unique symbol上下文监听器过滤器的 symbol 键,每次分派事件时都会查询该过滤器。
Context.isolate
Section titled “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
Section titled “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)
Section titled “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 Context对于 Cordis 上下文代理和上下文原型,返回 true。
此方法可跨 realm 和多个 cordis 副本工作,因为其品牌标识以全局 symbol 为键,而不是通过 instanceof 判断。
value:要测试的值。
返回 true 时,value 是 Cordis 上下文,并会收窄其类型。
服务存储与混入
Section titled “服务存储与混入”ctx.get(name, strict?)
Section titled “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): any从存储中读取服务,无需满足注入要求。
name:服务名称。strict:设为true(默认值)时,仅返回其提供方 fiber 当前处于活动状态的实现。
返回服务值;如果尚未提供,则返回 undefined。
ctx.set(name, value)
Section titled “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]): voidset(name: string, value: any): void覆盖已提供服务的值。
只有提供该服务的 fiber 才能设置它;设置尚未提供的名称会抛出异常。
name:服务名称。value:新的服务值。
ctx.provide(name, value)
Section titled “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]): () => voidprovide(name: string, value?: any): () => void注册一个归当前 fiber 所有的服务实现。
fiber 激活后,该服务对同一隔离作用域内的依赖方可见;当返回的资源释放函数运行或 fiber 卸载时,该服务会被取消注册,并唤醒依赖方。如果该名称已在此作用域中被提供,或已声明为访问器,则抛出异常。
name:服务名称。value:服务值。
返回一个用于取消注册该服务的资源释放函数。
ctx.accessor(name, options)
Section titled “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'>): void定义一个由 get/set 钩子支持的计算型上下文属性。
当前 fiber 卸载时会移除该访问器。如果该名称已被声明,则抛出异常。
name:上下文属性名称。options:get钩子和可选的set钩子。
ctx.mixin(name, mixins)
Section titled “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>): voidmixin<T extends {}>(source: T, mixins: (keyof this & keyof T)[] | Dict<string>): void直接在 ctx 上公开服务的指定成员。
每个混入的键都会成为一个转发到该服务的访问器,并将方法绑定到该服务。例如,ctx.on 会转发到 ctx.events.on。当前 fiber 卸载时会移除这些混入。
name:存放源服务的上下文属性。mixins:要转发的键,或从源键到 ctx 键的映射。