OpenTelemetry AsyncHooks-based Context Manager for all
$ yarn add @zcong/context-async-hooks
# or npm
$ npm i @zcong/context-async-hooks --save
const ctx1 = { [key1]: 'ctx1' }
const ctx2 = { [key1]: 'ctx2' }
contextManager.with(ctx1, () => {
expect(contextManager.active()).toStrictEqual(ctx1)
contextManager.with(ctx2, () => {
expect(contextManager.active()).toStrictEqual(ctx2)
})
expect(contextManager.active()).toStrictEqual(ctx1)
})
const context = { [key1]: 1 }
const fn = contextManager.bind(context, () => {
expect(contextManager.active()).toStrictEqual(context)
})
fn()
const ee = new EventEmitter()
const context = { [key1]: 2 }
const patchedEE = contextManager.bind(context, ee)
const handler = () => {
expect(contextManager.active()).toStrictEqual(context)
patchedEE.removeListener('test', handler)
}
patchedEE.on('test', handler)
patchedEE.emit('test')
const ctx1 = new ImmutableContext<TestCtx>({ test: 'ctx1' })
contextManager.with(ctx1, () => {
expect(contextManager.active()).toStrictEqual(ctx1)
const ctx2 = ctx1.setValue('num', 18)
contextManager.with(ctx2, () => {
expect(contextManager.active()).toStrictEqual(ctx2)
expect(contextManager.active().getValue('test')).toEqual('ctx1')
expect(contextManager.active().getValue('num')).toEqual(18)
const ctx3 = ctx2.deleteValue('num')
contextManager.with(ctx3, () => {
expect(contextManager.active()).toStrictEqual(ctx3)
expect(contextManager.active().getValue('test')).toEqual('ctx1')
expect(contextManager.active().getValue('num')).toBeUndefined()
expect(contextManager.active().unwrap()).toStrictEqual(ctx1.unwrap())
})
})
// actually restore old context
expect(contextManager.active()).toStrictEqual(ctx1)
expect(contextManager.active().getValue('test')).toEqual('ctx1')
expect(contextManager.active().getValue('num')).toBeUndefined()
})
MIT © zcong1993