{"id":22793566,"url":"https://github.com/kylemocode/consumerx","last_synced_at":"2025-04-16T22:50:40.704Z","repository":{"id":57206324,"uuid":"296387902","full_name":"kylemocode/ConsumerX","owner":"kylemocode","description":"Priority consumer powered by RxJS 🔥","archived":false,"fork":false,"pushed_at":"2021-01-15T15:14:27.000Z","size":166,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T05:41:41.132Z","etag":null,"topics":["asynchronous-jobs","consumer","priority-queue","rxjs"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/consumer-x","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/kylemocode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-17T16:51:02.000Z","updated_at":"2024-06-30T07:20:35.000Z","dependencies_parsed_at":"2022-09-11T05:20:31.998Z","dependency_job_id":null,"html_url":"https://github.com/kylemocode/ConsumerX","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylemocode%2FConsumerX","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylemocode%2FConsumerX/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylemocode%2FConsumerX/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kylemocode%2FConsumerX/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kylemocode","download_url":"https://codeload.github.com/kylemocode/ConsumerX/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249284472,"owners_count":21243966,"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":["asynchronous-jobs","consumer","priority-queue","rxjs"],"created_at":"2024-12-12T03:27:29.364Z","updated_at":"2025-04-16T22:50:40.688Z","avatar_url":"https://github.com/kylemocode.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ConsumerX\nAn asynchronous jobs (Promise | Async Function) consumer which can deal with priority based on RxJS, having error-handling and retry mechanism and can specify callback on success and failure. Can be used in browser and node environment.\n\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fkylemocode%2FConsumerX.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fkylemocode%2FConsumerX?ref=badge_shield)\n![Download](https://img.shields.io/npm/dt/consumer-x)\n\n# Quick Start\n\n## Install\n\n[consumer-x](https://www.npmjs.com/package/consumer-x)\n```shell\n$ yarn add consumer-x\n```\n\n## Overview\n### Introduction\nConsumerX is a simple asynchronous priority jobs consumer with error-handling and retry mechanism built with TypeScript and RxJS, which can run on Node.JS environment and browser.\n\n## Basic Usage\n\n```javascript\nimport ConsumerX from 'consumer-x';\n\nconst consumer = new ConsumerX(2, 100); // Parameters have default value: retryCount = 0, intervalTime = 200\n\n// promise as job\nconst testPromiseResolve = () =\u003e {\n  return new Promise(resolve =\u003e {\n    resolve('resolve from promise');\n  })\n}\n\nconst testPromiseReject = () =\u003e {\n  console.log('called!');\n  return new Promise((_resolve, reject) =\u003e {\n    reject('reject from promise');\n  })\n}\n\n// You can also push async function as job\nconst testAsync = async() =\u003e {\n  try {\n    const res = await testPromiseResolve();\n    return res + '?No, is from async';\n  } catch(err) {\n    throw Error('error');\n  }\n}\n\nconsumer.push({\n  task: testPromiseResolve,\n  success: (res) =\u003e {\n    console.log('Success~~ ', res);\n  },\n  failure: (err) =\u003e {\n    console.log('Failure ', err);\n  }\n}, 3) // second argument is priority\n\nconsumer.push({\n  task: testPromiseReject,\n  success: (res) =\u003e {\n    console.log('Success ', res);\n  },\n  failure: (err) =\u003e {\n    console.log('Failure ', err);\n  }\n}, 2)\n\nconsumer.push({\n  task: testAsync,\n  success: (res) =\u003e {\n    console.log('Success!! ', res);\n  },\n  failure: (err) =\u003e {\n    console.log('Failure ', err);\n  }\n}) // If you don't specify the priority, the default value will be 5. \n\nconsumer.push({\n  task: testAsync,\n  success: (res) =\u003e {\n    console.log('Success$$ ', res);\n  },\n  failure: (err) =\u003e {\n    console.log('Failure ', err);\n  }\n})\n\n/*\nThe console will log:\n\ncalled!\ncalled!\ncalled!\nFailure  reject from promise\n(Because the testPromiseReject's priority is highest, so it run first, and because we set the retryCount to 2 when we created the instance, so it will retry two time (the initial call + 2 retry count, so totally 3 times.) If the last retry also fail, it will call the failure callback.)\n\nSuccess~~  resolve from promise\n\nSuccess!!  resolve from promise?No, is from async\n(Default priority is 5, which is the lowest in this case.)\n\nSuccess$$  resolve from promise?No, is from async\n(If tasks have same priority, it will work like normal queue (FIFO).)\n*/\n\n```\n\n## Running tests\n\n```shell\n$ yarn test\n```\n\n## Parameters\n\n| Param        | Type     | Description                                                        |\n|--------------|----------|--------------------------------------------------------------------|\n| retryCount   | `number` | The retry count when job encounter failure. (Default Value = 0)    |\n| intervalTime | `number` | The interval delay between each two tasks. (Default Value = 200ms) |\n|              |          |                                                                    |\n\n## Priority Queue\nConsumerX implement a linear data structure priority queue instead of heap based priority to make sure the order of tasks with same priority.(It's relatively difficult to achieve this in heap-based priority queue.)\n\n| Method         | Big O    |\n|----------------|----------|\n| push           | O(1)     |\n| size           | O(1)     |\n| isEmpty        | O(1)     |\n| pop            | O(n)     |\n\n## Priority\nYou can specify priority of each task while pushing the task\n\n```typescript\nconsumer.push({\n  task: testPromise,\n  success: (res) =\u003e {\n    console.log('Success ', res);\n  },\n  failure: (err) =\u003e {\n    console.log('Failure ', err);\n  }\n}, 2);\n```\nThe lower number has a higher priority than the bigger number, the default priority is 5.\n\nIf the tasks have same priorities, it will be implemented with pattern of First In First Out (FIFO).\n\n## Defer\nUse RxJS defer to achieve lazy execution.\n\n## QueueEntity\n```typescript\ninterface IQueueEntity\u003cResult\u003e {\n  task: () =\u003e Promise\u003cResult\u003e;\n  success?: (res: Result) =\u003e void;\n  failure?: (err: unknown) =\u003e void;\n}\n```\ncurrently accept promise and async function as job, will probably support other types in the future.\n\n## Roadmap\n  - [X] Implement priority queue\n  - [X] Setup lint-stage\n  - [X] Add Prettier\n  - [ ] Increase unit test and maintain high testing coverage\n  - [ ] CI/CD pipeline\n  - [ ] Take advantage of more RxJS features\n\n## License\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fkylemocode%2FConsumerX.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fkylemocode%2FConsumerX?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylemocode%2Fconsumerx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkylemocode%2Fconsumerx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkylemocode%2Fconsumerx/lists"}