{"id":18662591,"url":"https://github.com/zcong1993/context-async-hooks","last_synced_at":"2025-05-12T20:24:51.499Z","repository":{"id":38899357,"uuid":"389679383","full_name":"zcong1993/context-async-hooks","owner":"zcong1993","description":"OpenTelemetry AsyncHooks-based Context Manager for all","archived":false,"fork":false,"pushed_at":"2024-12-08T23:08:25.000Z","size":252,"stargazers_count":1,"open_issues_count":11,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-12-24T04:36:54.816Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zcong1993.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-26T15:22:55.000Z","updated_at":"2022-04-25T13:31:48.000Z","dependencies_parsed_at":"2024-02-12T22:58:03.730Z","dependency_job_id":null,"html_url":"https://github.com/zcong1993/context-async-hooks","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcong1993%2Fcontext-async-hooks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcong1993%2Fcontext-async-hooks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcong1993%2Fcontext-async-hooks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zcong1993%2Fcontext-async-hooks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zcong1993","download_url":"https://codeload.github.com/zcong1993/context-async-hooks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":231515010,"owners_count":18388404,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T08:13:09.431Z","updated_at":"2024-12-27T16:41:43.586Z","avatar_url":"https://github.com/zcong1993.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# context-async-hooks\n\n[![NPM version](https://img.shields.io/npm/v/@zcong/context-async-hooks.svg?style=flat)](https://npmjs.com/package/@zcong/context-async-hooks)\n[![NPM downloads](https://img.shields.io/npm/dm/@zcong/context-async-hooks.svg?style=flat)](https://npmjs.com/package/@zcong/context-async-hooks)\n[![codecov](https://codecov.io/gh/zcong1993/context-async-hooks/branch/master/graph/badge.svg)](https://codecov.io/gh/zcong1993/context-async-hooks)\n\n\u003e OpenTelemetry AsyncHooks-based Context Manager for all\n\n## Install\n\n```bash\n$ yarn add @zcong/context-async-hooks\n# or npm\n$ npm i @zcong/context-async-hooks --save\n```\n\n## Usage\n\n```ts\nconst ctx1 = { [key1]: 'ctx1' }\nconst ctx2 = { [key1]: 'ctx2' }\ncontextManager.with(ctx1, () =\u003e {\n  expect(contextManager.active()).toStrictEqual(ctx1)\n  contextManager.with(ctx2, () =\u003e {\n    expect(contextManager.active()).toStrictEqual(ctx2)\n  })\n  expect(contextManager.active()).toStrictEqual(ctx1)\n})\n```\n\n### bind function\n\n```ts\nconst context = { [key1]: 1 }\nconst fn = contextManager.bind(context, () =\u003e {\n  expect(contextManager.active()).toStrictEqual(context)\n})\nfn()\n```\n\n### bind EventEmitter\n\n```ts\nconst ee = new EventEmitter()\nconst context = { [key1]: 2 }\nconst patchedEE = contextManager.bind(context, ee)\nconst handler = () =\u003e {\n  expect(contextManager.active()).toStrictEqual(context)\n  patchedEE.removeListener('test', handler)\n}\npatchedEE.on('test', handler)\npatchedEE.emit('test')\n```\n\n### use ImmutableContext\n\n```ts\nconst ctx1 = new ImmutableContext\u003cTestCtx\u003e({ test: 'ctx1' })\ncontextManager.with(ctx1, () =\u003e {\n  expect(contextManager.active()).toStrictEqual(ctx1)\n  const ctx2 = ctx1.setValue('num', 18)\n  contextManager.with(ctx2, () =\u003e {\n    expect(contextManager.active()).toStrictEqual(ctx2)\n    expect(contextManager.active().getValue('test')).toEqual('ctx1')\n    expect(contextManager.active().getValue('num')).toEqual(18)\n\n    const ctx3 = ctx2.deleteValue('num')\n    contextManager.with(ctx3, () =\u003e {\n      expect(contextManager.active()).toStrictEqual(ctx3)\n      expect(contextManager.active().getValue('test')).toEqual('ctx1')\n      expect(contextManager.active().getValue('num')).toBeUndefined()\n      expect(contextManager.active().unwrap()).toStrictEqual(ctx1.unwrap())\n    })\n  })\n  // actually restore old context\n  expect(contextManager.active()).toStrictEqual(ctx1)\n  expect(contextManager.active().getValue('test')).toEqual('ctx1')\n  expect(contextManager.active().getValue('num')).toBeUndefined()\n})\n```\n\n## License\n\nMIT \u0026copy; zcong1993\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcong1993%2Fcontext-async-hooks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzcong1993%2Fcontext-async-hooks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzcong1993%2Fcontext-async-hooks/lists"}