import type { RawTelemetryUsage, RawTelemetryUsageFeature } from '../telemetry' import { addTelemetryUsage } from '../telemetry' import { monitor } from '../../tools/monitor' import type { BoundedBuffer } from '../../tools/boundedBuffer' import type { ContextManager } from './contextManager' import type { ContextManagerMethod, CustomerContextKey } from './contextConstants' export function defineContextMethod( getStrategy: () => Record, contextName: Key, methodName: MethodName, usage?: RawTelemetryUsageFeature ): ContextManager[MethodName] { return monitor((...args: any[]) => { if (usage) { addTelemetryUsage({ feature: usage } as RawTelemetryUsage) } return (getStrategy()[contextName][methodName] as (...args: unknown[]) => unknown)(...args) }) as ContextManager[MethodName] } export function bufferContextCalls>( preStartContextManager: ContextManager, name: Key, bufferApiCalls: BoundedBuffer ) { preStartContextManager.changeObservable.subscribe(() => { const context = preStartContextManager.getContext() bufferApiCalls.add((startResult) => startResult[name].setContext(context)) }) }