0.2.1 • Published 3 years ago

@zcong/context-async-hooks v0.2.1

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

context-async-hooks

NPM version NPM downloads codecov

OpenTelemetry AsyncHooks-based Context Manager for all

Install

$ yarn add @zcong/context-async-hooks
# or npm
$ npm i @zcong/context-async-hooks --save

Usage

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)
})

bind function

const context = { [key1]: 1 }
const fn = contextManager.bind(context, () => {
  expect(contextManager.active()).toStrictEqual(context)
})
fn()

bind EventEmitter

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')

use ImmutableContext

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()
})

License

MIT © zcong1993