{"_id":"@audc/eventemitter-wrapper","_rev":"1-66fcc356e400b66acd76124c325b8014","name":"@audc/eventemitter-wrapper","dist-tags":{"latest":"2.0.2"},"versions":{"2.0.2":{"name":"@audc/eventemitter-wrapper","version":"2.0.2","keywords":["event","eventemitter","wrapper"],"author":{"name":"unchosen.aus@gmail.com"},"license":"MIT","_id":"@audc/eventemitter-wrapper@2.0.2","maintainers":[{"name":"bivas6","email":"yaakov.bivas@audiocodes.com"},{"name":"orgads","email":"orgads@gmail.com"}],"homepage":"https://github.com/Jashepp/eventemitter-wrapper#readme","bugs":{"url":"https://github.com/Jashepp/eventemitter-wrapper/issues"},"dist":{"shasum":"3981c019d0f8497e51de0d3dd0df2fad7e74b170","tarball":"https://registry.npmjs.org/@audc/eventemitter-wrapper/-/eventemitter-wrapper-2.0.2.tgz","fileCount":6,"integrity":"sha512-wYfvvG5Zb7gL/uZqDaFeHNzYJrzGCYaW8mPFGNKvwmQNtleD/F3HHP4Uz3fcuuazbAJwXOKYSBMh58euroYRAw==","signatures":[{"sig":"MEUCIQDBd3RowSVCTt5ogGMHuVWRkOLt5m5Q4NQTyfX7iqaUcAIgBwl9rv1KdadVpXpby9rrhrsUXAd2NDIHy7InuL6cQJo=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":18980},"main":"./index.js","type":"commonjs","types":"./index.d.ts","module":"./module.mjs","engines":{"node":">=16"},"exports":{".":{"types":"./index.d.ts","import":"./module.mjs","require":"./index.js"}},"gitHead":"4041c3bcad3de576879b8f012a8e37f90a4ec28f","scripts":{"test":"mocha -t 10000 --recursive tests/**/*.js --check-leaks --use_strict --exit","test-watch":"mocha -t 10000 --recursive tests/**/*.js --check-leaks --use_strict --watch --watch-files \"*.js,*.mjs,tests/**/*.js\" --bail --full-trace --trace-deprecation --exit","test-parallel":"mocha -t 10000 --recursive tests/**/*.js --check-leaks --use_strict --parallel --exit"},"_npmUser":{"name":"orgads","email":"orgads@gmail.com"},"repository":{"url":"git+https://github.com/Jashepp/eventemitter-wrapper.git","type":"git"},"_npmVersion":"10.8.2","description":"Control event groups","directories":{},"_nodeVersion":"22.5.1","_hasShrinkwrap":false,"devDependencies":{"chai":"^4.3.8","mocha":"^10.2.0","sinon":"^16.0.0","sinon-chai":"^3.7.0","underscore":"^1.13.6"},"_npmOperationalInternal":{"tmp":"tmp/eventemitter-wrapper_2.0.2_1728915582977_0.30099424669851693","host":"s3://npm-registry-packages"}}},"time":{"created":"2024-10-14T14:19:42.866Z","modified":"2025-01-19T20:18:58.484Z","2.0.2":"2024-10-14T14:19:43.140Z"},"bugs":{"url":"https://github.com/Jashepp/eventemitter-wrapper/issues"},"author":{"name":"unchosen.aus@gmail.com"},"license":"MIT","homepage":"https://github.com/Jashepp/eventemitter-wrapper#readme","keywords":["event","eventemitter","wrapper"],"repository":{"url":"git+https://github.com/Jashepp/eventemitter-wrapper.git","type":"git"},"description":"Control event groups","maintainers":[{"email":"orgads@gmail.com","name":"orgads"}],"readme":"# eventemitter-wrapper - Nodejs Module\r\n> Control event groups\r\n\r\n[![NPM Version][npm-image]][npm-url]\r\n[![Downloads Stats][npm-downloads]][npm-url]\r\n[![Tests][github-tests-badge]][github-tests-url]\r\n\r\n## What is this?\r\n\r\nThis module lets you group event listeners so you can seperate/isolate listeners from other listeners, such that you can call `removeAllListeners` and it will only remove the listeners on the current event wrapper. No need to keep track of specific groups of listeners when this can do it for you.\r\n\r\n## Why use this?\r\n\r\nIf you have an `EventEmitter` which has important event listeners on it, and you have a module that you want others to use without them removing those said event listeners by mistake, then this will let you protect them by exporting the wrapped `EventEmitter` instead of the main one.\r\n\r\nOr, in the case of why I needed this module, I have portions of my applications as reloadable, and each time some code unloads, I have the events on a wrapper that I simply remove the listeners from without effecting other parts of my application.\r\n\r\n## Installation\r\n\r\n**Install via NPM:** with the [NPM package][npm-url]\r\n```\r\nnpm install eventemitter-wrapper\r\n```\r\n\r\n**Install via NPM from Github:** with a [GitHub Tag][github-tags] to specify version (specifed as #hash)\r\n```\r\nnpm install jashepp/eventemitter-wrapper#v2.0.1\r\n```\r\n\r\nOr [download the latest release][github-releases], or [use github packages](https://github.com/Jashepp/eventemitter-wrapper/pkgs/npm/eventemitter-wrapper), or git clone the [repository on GitHub][github-url].\r\n\r\nThis module is written with ES6 features.\r\n\r\n## How To Use / API\r\n\r\nThis module is available as both CommonJS and ES Module. The ES Module calls the CommonJS file under the hood.\r\n\r\nRequire or import the module, wrap an existing `EventEmitter` instance, and use methods as you usually would.\r\n\r\nThis should behave like the [original EventEmitter](https://nodejs.org/api/events.html), with the same methods and functionality, since it wraps it and uses it under the hood.\r\n\r\nCommonJS Method:\r\n\r\n```javascript\r\nconst EventEmitter = require('node:events');\r\nconst EventEmitterWrapper = require('eventemitter-wrapper');\r\n\r\nconst events = new EventEmitter();\r\nconst eventsWrapped = new EventEmitterWrapper(events);\r\n// ...\r\n```\r\n\r\nES Module Method:\r\n\r\n```javascript\r\nimport { EventEmitter } from 'node:events';\r\nimport { EventEmitterWrapper } from 'eventemitter-wrapper';\r\n\r\nconst events = new EventEmitter();\r\nconst eventsWrapped = new EventEmitterWrapper(events);\r\n// ...\r\n```\r\n\r\nThe passed argument for `EventEmitterWrapper` can be any object that is an EventEmitter directly or prototyped.\r\n\r\nThe old `v1.0` method of creating the wrapped EventEmitter is still available:\r\n\r\n```javascript\r\nconst eventsWrapped = EventEmitterWrapper.createWrapper(events);\r\n```\r\n\r\n### Methods & Properties\r\n\r\nAPI for the wrapped instance created via `new EventEmitterWrapper(events);`\r\n\r\nOnly `eventEmitter` is new, along with unlisted internal methods & properties.\r\n\r\n| Method / Property | Type | Notes |\r\n|-|-|-|\r\n| `eventEmitter` | prop | Original `EventEmitter` |\r\n| [`addListener(eventName,listener)`](https://nodejs.org/api/events.html#emitteraddlistenereventname-listener) | method | Listens on `original` & `wrapped` |\r\n| [`on(eventName,listener)`](https://nodejs.org/api/events.html#emitteroneventname-listener) | method | Listens on `original` & `wrapped` |\r\n| [`once(eventName,listener)`](https://nodejs.org/api/events.html#emitteronceeventname-listener) | method | Listens on `original` & `wrapped` |\r\n| [`prependListener(eventName,listener)`](https://nodejs.org/api/events.html#emitterprependlistenereventname-listener) | method | Listens on `original` & `wrapped` |\r\n| [`prependOnceListener(eventName,listener)`](https://nodejs.org/api/events.html#emitterprependoncelistenereventname-listener) | method | Listens on `original` & `wrapped` |\r\n| [`rawListeners(eventName)`](https://nodejs.org/api/events.html#emitterrawlistenerseventname) | method | Lists only `wrapped` listeners |\r\n| [`listeners(eventName)`](https://nodejs.org/api/events.html#emitterlistenerseventname) | method | Lists only `wrapped` listeners |\r\n| [`listenerCount(eventName[,listener])`](https://nodejs.org/api/events.html#emitterlistenercounteventname-listener) | method | Counts only `wrapped` listeners |\r\n| [`eventNames()`](https://nodejs.org/api/events.html#emittereventnames) | method | Lists only `wrapped` listeners |\r\n| [`emit(eventName[,...args])`](https://nodejs.org/api/events.html#emitteremiteventname-args) | method | Directly calls `original` method |\r\n| [`removeAllListeners([eventName])`](https://nodejs.org/api/events.html#emitterremovealllistenerseventname) | method | Removes only `wrapped` listeners |\r\n| [`removeListener(eventName,listener)`](https://nodejs.org/api/events.html#emitterremovelistenereventname-listener) | method | Removes on both `original` & `wrapped` |\r\n| [`off(eventName,listener)`](https://nodejs.org/api/events.html#emitteroffeventname-listener) | method | Removes on both `original` & `wrapped` |\r\n| [`getMaxListeners()`](https://nodejs.org/api/events.html#emittergetmaxlisteners) | method | Directly calls `original` method |\r\n| [`setMaxListeners(n)`](https://nodejs.org/api/events.html#emittersetmaxlistenersn) | method | Directly calls `original` method |\r\n\r\nWhen the wrapper has events listening on the original EventEmitter, a `removeListener` event will be internally listened on for clean-up after an event is removed.\r\n\r\nOn this wrapper, there are internal methods & properties prefixed with '`_eew`'. These are available (see source code) to use, but they may change in future releases.\r\n\r\n### Examples\r\n\r\n```javascript\r\n// Require modules\r\nconst EventEmitter = require('node:events');\r\nconst EventEmitterWrapper = require('eventemitter-wrapper');\r\n\r\n// Create instances\r\nconst events = new EventEmitter();\r\nconst eventsWrapped = new EventEmitterWrapper(events);\r\n\r\n// Attach a listener to original EventEmitter\r\nevents.on('original',(...args)=>{\r\n\tconsole.log('original:',...args);\r\n});\r\n\r\n// Fire event on either original or wrapped\r\nevents.emit('original','foo');\r\n// Logs: original: foo\r\n\r\n// Attach a listener to wrapped EventEmitter\r\neventsWrapped.on('wrapped',(...args)=>{\r\n\tconsole.log('wrapped:',...args);\r\n});\r\n\r\n// Fire event on either original or wrapped\r\nevents.emit('wrapped','bar');\r\n// Logs: wrapped: bar\r\n\r\n// Fetch a list of events on the original\r\n// \"removeListener\" event is used by the wrapper for event clean-up\r\nconsole.log(events.eventNames());\r\n// Logs: [ 'original', 'wrapped', 'removeListener' ]\r\n\r\n// Fetch a list of events on the wrapper\r\nconsole.log(eventsWrapped.eventNames());\r\n// Logs: [ 'wrapped' ]\r\n\r\n// Remove all listeners on the wrapper\r\neventsWrapped.removeAllListeners();\r\n\r\n// Fetch a list of events on the original\r\nconsole.log(events.eventNames());\r\n// Logs: [ 'original' ]\r\n\r\n// Fetch a list of events on the wrapper\r\nconsole.log(eventsWrapped.eventNames());\r\n// Logs: []\r\n```\r\n\r\n## Tests\r\n\r\nTests are located within `./tests/` on the git [repository on GitHub][github-url] or locally if pulled. NPM version does **not** include tests.\r\n\r\nTo get started with tests, we need to install some dev dependencies. Enter local directory of this repository and run:\r\n```\r\nnpm install --only=dev\r\n```\r\n\r\nTo run the tests, run:\r\n```\r\nnpm run test\r\n```\r\n\r\nTo continuously run tests while editing, run:\r\n```\r\nnpm run test-watch\r\n```\r\n\r\n## Contributors\r\n\r\nTo submit a contribution, create issues or pull requests on the [GitHub repository][github-url].\r\n\r\nPlease be sure to run tests after any changes.\r\n\r\nAll help is appreciated. Even if it's just improvements to this readme or the tests.\r\n\r\n## License\r\n\r\nMIT License\r\n\r\nCopyright (c) 2023 Jason Sheppard @ https://github.com/Jashepp\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining a copy\r\nof this software and associated documentation files (the \"Software\"), to deal\r\nin the Software without restriction, including without limitation the rights\r\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\ncopies of the Software, and to permit persons to whom the Software is\r\nfurnished to do so, subject to the following conditions:\r\n\r\nThe above copyright notice and this permission notice shall be included in all\r\ncopies or substantial portions of the Software.\r\n\r\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r\nSOFTWARE.\r\n\r\n## Links\r\n\r\nGithub Repository: [https://github.com/Jashepp/eventemitter-wrapper](https://github.com/Jashepp/eventemitter-wrapper)\r\n\r\nNPM Package: [https://www.npmjs.com/package/eventemitter-wrapper](https://www.npmjs.com/package/eventemitter-wrapper)\r\n\r\n[npm-image]: https://img.shields.io/npm/v/eventemitter-wrapper.svg?style=flat-square\r\n[npm-url]: https://npmjs.org/package/eventemitter-wrapper\r\n[npm-downloads]: https://img.shields.io/npm/dm/eventemitter-wrapper.svg?style=flat-square\r\n[github-url]: https://github.com/Jashepp/eventemitter-wrapper\r\n[github-releases]: https://github.com/Jashepp/eventemitter-wrapper/releases\r\n[github-tags]: https://github.com/Jashepp/eventemitter-wrapper/tags\r\n[github-tests-badge]: https://github.com/Jashepp/eventemitter-wrapper/actions/workflows/run-tests.yml/badge.svg\r\n[github-tests-url]: https://github.com/Jashepp/eventemitter-wrapper/actions/workflows/run-tests.yml\r\n","readmeFilename":"README.md"}