{"id":15700603,"url":"https://github.com/satantime/ngrx-correlation-id","last_synced_at":"2025-04-11T07:39:46.222Z","repository":{"id":37047934,"uuid":"265334863","full_name":"satanTime/ngrx-correlation-id","owner":"satanTime","description":"A ngrx store library to track an asynchronous activity.","archived":false,"fork":false,"pushed_at":"2025-04-07T19:47:33.000Z","size":55325,"stargazers_count":6,"open_issues_count":155,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-07T20:40:37.218Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/ngrx-correlation-id","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/satanTime.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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},"funding":{"github":"satanTime"}},"created_at":"2020-05-19T18:48:16.000Z","updated_at":"2022-11-04T19:42:18.000Z","dependencies_parsed_at":"2023-02-15T07:46:43.470Z","dependency_job_id":"e44bb887-c1e1-4421-9b7e-6ca346c11052","html_url":"https://github.com/satanTime/ngrx-correlation-id","commit_stats":{"total_commits":1196,"total_committers":4,"mean_commits":299.0,"dds":"0.35367892976588633","last_synced_commit":"8277a82c9dc2a3d77b596485b60ac7b73c2248b3"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/satanTime%2Fngrx-correlation-id","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/satanTime%2Fngrx-correlation-id/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/satanTime%2Fngrx-correlation-id/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/satanTime%2Fngrx-correlation-id/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/satanTime","download_url":"https://codeload.github.com/satanTime/ngrx-correlation-id/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248359165,"owners_count":21090487,"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-10-03T19:50:54.227Z","updated_at":"2025-04-11T07:39:46.199Z","avatar_url":"https://github.com/satanTime.png","language":"TypeScript","funding_links":["https://github.com/sponsors/satanTime"],"categories":[],"sub_categories":[],"readme":"# ngrx-correlation-id\n\n[![npm version](https://badge.fury.io/js/ngrx-correlation-id.svg)](https://badge.fury.io/js/ngrx-correlation-id)\n[![CircleCI](https://circleci.com/gh/satanTime/ngrx-correlation-id.svg?style=shield)](https://app.circleci.com/pipelines/github/satanTime/ngrx-correlation-id)\n[![Coverage Status](https://coveralls.io/repos/github/satanTime/ngrx-correlation-id/badge.svg?branch=master)](https://coveralls.io/github/satanTime/ngrx-correlation-id?branch=master)\n\nA ngrx store library to track an asynchronous activity such as a http request, a ngrx effect or anything else.\n\n### Supports\n- Angular 13 and `ngrx/store@13`\n- Angular 12 and `ngrx/store@12`\n- Angular 11 and `ngrx/store@11`\n- Angular 10 and `ngrx/store@10`\n- Angular 9 and `ngrx/store@9`\n- Angular 8 and `ngrx/store@8`\n- Angular 7 and `ngrx/store@7`\n- Angular 6 and `ngrx/store@6`\n\n### API short list\n\nPlease check source code\n\n- NgrxCorrelationIdModule\n* CidTask\n- cidTask(correlationId, Observable\u003cany\u003e)\n- cidWait()\n* store.select(selectCid, correlationId)\n- cidStart(correlationId: string)\n- cidPayload(correlationId: string, payload: any)\n- cidEnd(correlationId: string)\n- cidRemove(correlationId: string)\n\n## How to use\n\nImport `NgrxCorrelationIdModule` in the same module where you import `StoreModule.forRoot`.\n\n```typescript\nimport {NgrxCorrelationIdModule} from 'ngrx-correlation-id';\n\n@NgModule({\n    imports: [\n        StoreModule.forRoot(/* ... */),\n        NgrxCorrelationIdModule, // \u003c- import it here\n    ],\n})\nexport class AppModule {}\n```\n\nAdd `cid: string` to props of an action you want to track.\n\n```typescript\nexport const loadUsers = createAction(\n  '[User] Load Users',\n  props\u003c{cid: string}\u003e(), // \u003c- correlation id\n);\n```\n\nWrap an effect pipe with `cidTask`.\nThe first argument is a `cid` of the current task.\nThe second argument is a stream that should be tracked.\n\nOptionally you can dispatch `cidPayload` action with a custom payload.\nIn this case we want our payload to be an array of ids of users.\n\n```typescript\nimport {cidPayload, cidTask} from 'ngrx-correlation-id';\n\n@Injectable()\nexport class UsersEffects {\n    @Effect()\n    public readonly loadUsers$ = this.actions$.pipe(\n        ofType(loadUsers),\n        switchMap(({cid}) =\u003e cidTask(cid, this.http.get\u003cArray\u003cUser\u003e\u003e('v2/api/users').pipe(\n            switchMap(users =\u003e of(\n                upsertUsers({items: users}),\n                cidPayload({cid, payload: users.map(user =\u003e user.id)}),\n            )),\n        ))),\n    );\n\n    constructor(\n        protected readonly actions$: Actions,\n        private readonly http: HttpClient,\n    ) {}\n}\n```\n\nUpdate a component to use `cid` and all the features of the lib.\n\n```typescript\nimport {cidRemove, CidTask, cidWait, selectCid} from 'ngrx-correlation-id';\n\nexport class EntityComponent implements OnInit, OnDestroy {\n    public readonly users$: Observable\u003cArray\u003cUser\u003e\u003e;\n\n    // set here an unique value to distinguish this task from others.\n    private readonly cid: string = `randomString`;\n\n    constructor(private readonly store: Store) {\n        // selecting the related task that belongs to cid.\n        this.users$ = this.store.select\u003cCidTask\u003cArray\u003cstring\u003e\u003e\u003e(selectCid, this.cid).pipe(\n\n            // doesn't emit until the task isn't completed\n            cidWait(), // waits until the task starts, then waits until the task ends and the task.\n\n            map(task =\u003e task.payload),\n            withLatestFrom(this.store.select(selectUsers)),\n            map(([ids, users]) =\u003e users \u0026\u0026 users.filter(user =\u003e ids.indexOf(user.id) !== -1) || []),\n        );\n    }\n\n    public ngOnInit(): void {\n        // dispatching an action to load users.\n        this.store.dispatch(loadUsers({cid: this.cid}));\n    }\n\n    public ngOnDestroy(): void {\n        // clearing payload and selector\n        this.store.dispatch(cidRemove({cid: this.cid}));\n        selectCid.release();\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsatantime%2Fngrx-correlation-id","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsatantime%2Fngrx-correlation-id","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsatantime%2Fngrx-correlation-id/lists"}