/** * A prototype chain that matches the resource hierarchy. This is used to * pass state down the tree without plumbing through every function. */ export type InheritedContext = Record; /** An opaque handle used to get/set values in a prototype chain. */ export class ContextHandle { static getId(handle: ContextHandle): symbol { return handle.#id; } static getDefaultValue(handle: ContextHandle): Value { return handle.#getDefaultContext(); } #id = Symbol('Context ID'); #getDefaultContext: () => Value; constructor(getDefaultContext: () => Value) { this.#getDefaultContext = getDefaultContext; } } /** * Create a context object that is used to get and set context values in the * resource hierarchy. */ export const createContext = ( /** Returns a default value if the context could not be found. */ getDefaultContext: () => Value, ): ContextHandle => new ContextHandle(getDefaultContext);