{"id":25676361,"url":"https://github.com/smbeiragh/middlemitter","last_synced_at":"2025-07-15T04:35:09.896Z","repository":{"id":57151862,"uuid":"112169844","full_name":"smbeiragh/middlemitter","owner":"smbeiragh","description":"Async Event Emitter + Async Middleware for node.js","archived":false,"fork":false,"pushed_at":"2018-05-11T16:55:58.000Z","size":114,"stargazers_count":5,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-30T04:42:49.545Z","etag":null,"topics":["async","event-emitter","interceptor","javascript","middleware","priority"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/smbeiragh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-27T08:39:33.000Z","updated_at":"2024-04-13T08:52:17.000Z","dependencies_parsed_at":"2022-08-31T20:02:15.758Z","dependency_job_id":null,"html_url":"https://github.com/smbeiragh/middlemitter","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/smbeiragh/middlemitter","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smbeiragh%2Fmiddlemitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smbeiragh%2Fmiddlemitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smbeiragh%2Fmiddlemitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smbeiragh%2Fmiddlemitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smbeiragh","download_url":"https://codeload.github.com/smbeiragh/middlemitter/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smbeiragh%2Fmiddlemitter/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265402833,"owners_count":23759237,"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":["async","event-emitter","interceptor","javascript","middleware","priority"],"created_at":"2025-02-24T14:26:08.139Z","updated_at":"2025-07-15T04:35:09.851Z","avatar_url":"https://github.com/smbeiragh.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# middlemitter\n\nMiddleware + Event Emitter for node.js\n\n[![Build Status](https://travis-ci.org/smbeiragh/middlemitter.svg?branch=master)](https://travis-ci.org/smbeiragh/middlemitter)\n[![codecov](https://codecov.io/gh/smbeiragh/middlemitter/branch/master/graph/badge.svg)](https://codecov.io/gh/smbeiragh/middlemitter)\n\n[![NPM](https://nodei.co/npm/middlemitter.png)](https://nodei.co/npm/middlemitter/)\n\n1. async middlewares\n2. async/sync event listeners\n3. support priority for both middlewares and event listeners\n\n## Environment\nEvery ES5 compliant environment + Promise Api \u0026 Symbol.iterator.\nIn case of unsupported environments you can use appropriate polyfill(s).\n\n## Installation\n\nUsing yarn: \n\n```bash\nyarn add middlemitter\n``` \nUsing npm:\n\n```bash\nnpm install middlemitter\n```\n\n## Usage\n\nImporting\n```js\nimport { MiddlEmitter } from 'middlemitter';\n\nconst emitter = new MiddlEmitter();\n```\n\nAdd/Remove listener on test event\n```js\nconst listener = emitter.on('test', (a,b,c) =\u003e {\n  console.log('\u003e\u003e', a, b, c);\n});\n\nemitter.off('test', listener);\n```\n\nAdd listener on test event with priority\n```js\nemitter.on('test', (a,b,c) =\u003e {\n  console.log('second');\n}, 10);\n\nemitter.on('test', (a,b,c) =\u003e {\n  console.log('first');\n}, 11);\n```\n\nEmit test event\nReturns promise that resolves when all listeners executed.\n```js\nemitter.emit('test', 'a', 'b', 'c');\n```\n\nUse middleware on test event \n```js\nemitter.use('test', async (params) =\u003e {\n   const [a,b,c] = params;\n   // Mutate params array\n   params[0] = `(${a})`;\n   params[1] = `(${b})`;\n   params[2] = `(${c})`;\n   // Await for other middleware or just skip them all \n   await next();\n});\n\nemitter.on('test', (a,b,c) =\u003e {\n  // This will log (a)(b)(c)\n  console.log(a,b,c);\n});\n\nemitter.emit('test', 'a', 'b', 'c');\n```\n\nUse middleware on test event with specific priority\n```js\nemitter.use('test', async (params) =\u003e {\n   const [a,b,c] = params;\n   // Mutate params array\n   params[0] = `(${a})`;\n   params[1] = `(${b})`;\n   params[2] = `(${c})`;\n   // Await for other middleware or just skip them all \n   await next();\n}, 10);\n\nemitter.use('test', async (params) =\u003e {\n   const [a,b,c] = params;\n   // Mutate params array\n   params[0] = `{${a}}`;\n   params[1] = `{${b}}`;\n   params[2] = `{${c}}`;\n   // Await for other middleware or just skip them all \n   await next();\n}, 11);\n\nemitter.on('test', (a,b,c) =\u003e {\n  // This will log ({a})({b})({c})\n  console.log(a,b,c);\n});\n\nemitter.emit('test', 'a', 'b', 'c');\n```\n\nAdd middleware on test event at once\n```js\nemitter.use(\n  'test',\n  async (params, next) =\u003e {\n    const [a,b,c] = params;\n    // Mutate params array\n    params[0] = `{${a}}`;\n    params[1] = `{${b}}`;\n    params[2] = `{${c}}`;\n    await next();\n  },\n  async (params, next) =\u003e {\n    const [a,b,c] = params;\n    // Mutate params array\n    params[0] = `(${a})`;\n    params[1] = `(${b})`;\n    params[2] = `(${c})`;\n    await next();\n  },\n  10 // priority optional, default: 0\n);\n\nemitter.on('test', (a,b,c) =\u003e {\n  // This will log ({a})({b})({c})\n  console.log(a,b,c);\n});\n\nemitter.emit('test', 'a', 'b', 'c');\n```\n\nAsync listeners\n```js\nconst delay = (ms) =\u003e new Promise((resolve, reject) =\u003e {\n    setTimeout(() =\u003e { resolve(); }, ms);\n  });\n\nemitter.on('test', async (a,b,c) =\u003e {\n  await delay(1000);\n  console.log('first');\n});\n\nemitter.on('test', (a,b,c) =\u003e {\n  console.log('second, just after 1000 ms delay');\n});\n```\n\n## Contributing\n1. Fork repository\n2. Commit your code on feature/branch or bugfix/branch\n3. Write tests for new feature or bugs as needed\n3. All codes should pass eslint guide\n\n## TODO\n1. TODO(s) in listener_handler module\n3. Improve test coverage\n4. Improve README.md\n\n## License\nMIT License\n\nCopyright (c) 2017 Sajjad Mahdi Beiraghdar\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmbeiragh%2Fmiddlemitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmbeiragh%2Fmiddlemitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmbeiragh%2Fmiddlemitter/lists"}