only allow string keys for context values

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2022-01-24 14:26:08 +01:00
parent 95e702e50d
commit c78bf4acd4
7 changed files with 14 additions and 20 deletions
+2 -2
View File
@@ -152,7 +152,7 @@ export interface Context {
readonly abortSignal: AbortSignal_2;
readonly deadline: Date | undefined;
use(...decorators: ContextDecorator[]): Context;
value<T = unknown>(key: string | symbol): T | undefined;
value<T = unknown>(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;
}
@@ -127,7 +127,7 @@ export class AbortContext implements Context {
readonly deadline: Date | undefined,
) {}
value<T = unknown>(key: string | symbol): T | undefined {
value<T = unknown>(key: string): T | undefined {
return this.parent.value(key);
}
@@ -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 => {
@@ -24,7 +24,7 @@ export class RootContext implements Context {
readonly abortSignal = new AbortController().signal;
readonly deadline = undefined;
value<T = unknown>(_key: string | symbol): T | undefined {
value<T = unknown>(_key: string): T | undefined {
return undefined;
}
@@ -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', () => {
@@ -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<T = unknown>(key: string | symbol): T | undefined {
value<T = unknown>(key: string): T | undefined {
return key === this._key ? (this._value as T) : this._parent.value(key);
}
+1 -1
View File
@@ -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<T = unknown>(key: string | symbol): T | undefined;
value<T = unknown>(key: string): T | undefined;
/**
* Decorates this context with one or more behaviors.