Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zcong1993/context-async-hooks
OpenTelemetry AsyncHooks-based Context Manager for all
https://github.com/zcong1993/context-async-hooks
Last synced: about 23 hours ago
JSON representation
OpenTelemetry AsyncHooks-based Context Manager for all
- Host: GitHub
- URL: https://github.com/zcong1993/context-async-hooks
- Owner: zcong1993
- License: mit
- Created: 2021-07-26T15:22:55.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-07-31T18:50:49.000Z (3 months ago)
- Last Synced: 2024-07-31T23:01:23.473Z (3 months ago)
- Language: TypeScript
- Homepage:
- Size: 243 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# context-async-hooks
[![NPM version](https://img.shields.io/npm/v/@zcong/context-async-hooks.svg?style=flat)](https://npmjs.com/package/@zcong/context-async-hooks)
[![NPM downloads](https://img.shields.io/npm/dm/@zcong/context-async-hooks.svg?style=flat)](https://npmjs.com/package/@zcong/context-async-hooks)
[![codecov](https://codecov.io/gh/zcong1993/context-async-hooks/branch/master/graph/badge.svg)](https://codecov.io/gh/zcong1993/context-async-hooks)> OpenTelemetry AsyncHooks-based Context Manager for all
## Install
```bash
$ yarn add @zcong/context-async-hooks
# or npm
$ npm i @zcong/context-async-hooks --save
```## Usage
```ts
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
```ts
const context = { [key1]: 1 }
const fn = contextManager.bind(context, () => {
expect(contextManager.active()).toStrictEqual(context)
})
fn()
```### bind EventEmitter
```ts
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
```ts
const ctx1 = new ImmutableContext({ 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