diff --git a/packages/backend-common/api-report.md b/packages/backend-common/api-report.md index d63eeaf11b..e22195c2b3 100644 --- a/packages/backend-common/api-report.md +++ b/packages/backend-common/api-report.md @@ -152,7 +152,7 @@ export interface Context { readonly abortSignal: AbortSignal_2; readonly deadline: Date | undefined; use(...decorators: ContextDecorator[]): Context; - value(key: string | symbol): T | undefined; + value(key: string): T | undefined; } // @public @@ -165,7 +165,7 @@ export class Contexts { static setTimeoutDuration(timeout: Duration): ContextDecorator; static setTimeoutMillis(timeout: number): ContextDecorator; static setValue( - key: string | symbol, + key: string, value: unknown | ((previous: unknown | undefined) => unknown), ): ContextDecorator; } diff --git a/packages/backend-common/src/context/AbortContext.ts b/packages/backend-common/src/context/AbortContext.ts index 78ad8646ed..86069d290b 100644 --- a/packages/backend-common/src/context/AbortContext.ts +++ b/packages/backend-common/src/context/AbortContext.ts @@ -127,7 +127,7 @@ export class AbortContext implements Context { readonly deadline: Date | undefined, ) {} - value(key: string | symbol): T | undefined { + value(key: string): T | undefined { return this.parent.value(key); } diff --git a/packages/backend-common/src/context/Contexts.ts b/packages/backend-common/src/context/Contexts.ts index 7bab26f05a..692248028a 100644 --- a/packages/backend-common/src/context/Contexts.ts +++ b/packages/backend-common/src/context/Contexts.ts @@ -99,7 +99,7 @@ export class Contexts { * @returns A decorator that can be passed to {@link Context.use} */ static setValue( - key: string | symbol, + key: string, value: unknown | ((previous: unknown | undefined) => unknown), ): ContextDecorator { return ctx => { diff --git a/packages/backend-common/src/context/RootContext.ts b/packages/backend-common/src/context/RootContext.ts index 48d24514fa..96f55dd76a 100644 --- a/packages/backend-common/src/context/RootContext.ts +++ b/packages/backend-common/src/context/RootContext.ts @@ -24,7 +24,7 @@ export class RootContext implements Context { readonly abortSignal = new AbortController().signal; readonly deadline = undefined; - value(_key: string | symbol): T | undefined { + value(_key: string): T | undefined { return undefined; } diff --git a/packages/backend-common/src/context/ValueContext.test.ts b/packages/backend-common/src/context/ValueContext.test.ts index 99d6b2b60a..792893833d 100644 --- a/packages/backend-common/src/context/ValueContext.test.ts +++ b/packages/backend-common/src/context/ValueContext.test.ts @@ -18,31 +18,29 @@ import { Contexts } from './Contexts'; import { RootContext } from './RootContext'; import { ValueContext } from './ValueContext'; -const s = Symbol(); - describe('ValueContext', () => { it('returns its own values, or delegates to the parent', async () => { const root = new RootContext(); const a = ValueContext.forConstantValue(root, 'a', 1); - const b = ValueContext.forConstantValue(a, s, 2); + const b = ValueContext.forConstantValue(a, 'x', 2); const c = ValueContext.forConstantValue(b, 'a', 3); const d = ValueContext.forConstantValue(c, 'b', 4); expect(a.value('a')).toBe(1); expect(a.value('b')).toBeUndefined(); - expect(a.value(s)).toBeUndefined(); + expect(a.value('x')).toBeUndefined(); expect(b.value('a')).toBe(1); expect(b.value('b')).toBeUndefined(); - expect(b.value(s)).toBe(2); + expect(b.value('x')).toBe(2); expect(c.value('a')).toBe(3); expect(c.value('b')).toBeUndefined(); - expect(c.value(s)).toBe(2); + expect(c.value('x')).toBe(2); expect(d.value('a')).toBe(3); expect(d.value('b')).toBe(4); - expect(d.value(s)).toBe(2); + expect(d.value('x')).toBe(2); }); it('can decorate', () => { diff --git a/packages/backend-common/src/context/ValueContext.ts b/packages/backend-common/src/context/ValueContext.ts index 368e73fa05..789741e45f 100644 --- a/packages/backend-common/src/context/ValueContext.ts +++ b/packages/backend-common/src/context/ValueContext.ts @@ -22,17 +22,13 @@ import { Context, ContextDecorator } from './types'; * parent. */ export class ValueContext implements Context { - static forConstantValue( - ctx: Context, - key: string | symbol, - value: unknown, - ): Context { + static forConstantValue(ctx: Context, key: string, value: unknown): Context { return new ValueContext(ctx, key, value); } constructor( private readonly _parent: Context, - private readonly _key: string | symbol, + private readonly _key: string, private readonly _value: unknown, ) {} @@ -44,7 +40,7 @@ export class ValueContext implements Context { return this._parent.deadline; } - value(key: string | symbol): T | undefined { + value(key: string): T | undefined { return key === this._key ? (this._value as T) : this._parent.value(key); } diff --git a/packages/backend-common/src/context/types.ts b/packages/backend-common/src/context/types.ts index 716a03308b..25a6d2354b 100644 --- a/packages/backend-common/src/context/types.ts +++ b/packages/backend-common/src/context/types.ts @@ -49,7 +49,7 @@ export interface Context { * @param key - The key of the value to get * @returns The associated value, or undefined if not set */ - value(key: string | symbol): T | undefined; + value(key: string): T | undefined; /** * Decorates this context with one or more behaviors.