{"id":22503642,"url":"https://github.com/jokero/ee-proxy","last_synced_at":"2025-08-03T11:31:35.870Z","repository":{"id":57219731,"uuid":"106044344","full_name":"Jokero/ee-proxy","owner":"Jokero","description":"Event emitter proxy for easy local listeners cleanup","archived":false,"fork":false,"pushed_at":"2023-04-19T06:35:55.000Z","size":206,"stargazers_count":7,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-08-08T15:45:57.026Z","etag":null,"topics":["cleanup","emitter","eventemitter","javascript","listeners","nodejs","proxy","ultron"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/Jokero.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":"2017-10-06T19:49:22.000Z","updated_at":"2020-09-01T09:28:28.000Z","dependencies_parsed_at":"2023-01-22T22:31:07.466Z","dependency_job_id":null,"html_url":"https://github.com/Jokero/ee-proxy","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/Jokero%2Fee-proxy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jokero%2Fee-proxy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jokero%2Fee-proxy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jokero%2Fee-proxy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jokero","download_url":"https://codeload.github.com/Jokero/ee-proxy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228540810,"owners_count":17934029,"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":["cleanup","emitter","eventemitter","javascript","listeners","nodejs","proxy","ultron"],"created_at":"2024-12-06T23:37:18.384Z","updated_at":"2024-12-06T23:37:19.384Z","avatar_url":"https://github.com/Jokero.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ee-proxy\n\nEvent emitter proxy for easy local listeners cleanup\n\n[![NPM version](https://img.shields.io/npm/v/ee-proxy.svg)](https://npmjs.org/package/ee-proxy)\n[![Build status](https://img.shields.io/travis/Jokero/ee-proxy.svg)](https://travis-ci.org/Jokero/ee-proxy)\n\n**Note:** This module works in browsers and Node.js \u003e= 6.0. Use `Proxy` and `Array` polyfills for Internet Explorer\n\n## Table of Contents\n\n- [Demo](#demo)\n- [Installation](#installation)\n  - [Node.js](#nodejs)\n  - [Browser](#browser)\n- [Overview](#overview)\n- [Usage](#usage)\n  - [emitterProxy(object, [options])](#emitterProxy-object-options)\n    - [Parameters](#parameters)\n    - [Return value](#return-value)\n  - [Example](#example)\n  - [Polyfill](#polyfill)\n- [Build](#build)\n- [Tests](#tests)\n- [License](#license)\n\n## Demo\n\nTry [demo](https://runkit.com/npm/ee-proxy) on RunKit.\n\n## Installation\n\n```sh\nnpm install ee-proxy\n```\n\n### Node.js\n```js\nconst emitterProxy = require('ee-proxy');\n```\n\n### Browser\n```\n\u003cscript src=\"node_modules/ee-proxy/dist/ee-proxy.js\"\u003e\n```\nor minified version\n```\n\u003cscript src=\"node_modules/ee-proxy/dist/ee-proxy.min.js\"\u003e\n```\n\nYou can use the module with AMD/CommonJS or just use `window.emitterProxy`.\n\n## Overview\n\n`ee-proxy` allows you to easily and safely remove listeners attached to event emitter without touching listeners added in other pieces of code.\nUnlike other similar modules (for example, [ultron](https://www.npmjs.com/package/ultron)) this one works seamlessly and allows to call your custom methods on event emitter:\n\n```js\nconst emitterProxy = require('ee-proxy');\nconst EventEmitter = require('events');\n\nclass Game extends EventEmitter {\n    start() {\n        console.log('Game started');\n    }\n}\n\nconst game = emitterProxy(new Game());\ngame.start(); // Game started\n\nconsole.log(game instanceof EventEmitter); // true\nconsole.log(game instanceof Game); // true\n```\n\n## Usage\n\n### emitterProxy(emitter, [options])\n\n#### Parameters\n\n- `emitter` (EventEmitter)\n- `[options]` (Object)\n    - `[stopListeningAfterFirstEvent]` (boolean) - If `true`, `ee-proxy` removes all listeners attached to the wrapped emitter when first event is triggered (might be useful in some cases)\n    - `[removeMethod]` (string) - Name of the method for listeners cleanup (default - `stopListening`)\n    - `[addListenerMethods]` (string[]) - Methods which are intercepted by `ee-proxy` for keeping attached to emitter listeners (default - `['on', 'once', 'addListener', 'prependListener', 'prependOnceListener', 'onceAny', 'onAny']`)\n    - `[fields]` (string[]) - Option specially for `Proxy` polyfill (see [below](#polyfill))\n\n#### Return value\n\n(EventEmitter) - Proxy object (which is `!==` original emitter)\n\n```js\nconst user = new EventEmitter();\nuser.once('disconnect', () =\u003e console.log('User disconnected'));\n\nconst wrappedUser = emitterProxy(user);\nwrappedUser.once('game:start', () =\u003e console.log('User is ready to start the game'));\nwrappedUser.once('game:cancel', () =\u003e console.log('User cancelled the game'));\nwrappedUser.once('disconnect', () =\u003e console.log('User disconnected'));\n\nwrappedUser.stopListening(); // removes all attached to the wrapped emitter listeners\nconsole.log(user.listenerCount('disconnect')); // 1\n\n// wrappedUser.stopListening('game:start'); // you can specify a particular event\n// wrappedUser.stopListening('game:start', 'game:cancel'); // or even list several events\n```\n\n### Examples\n\n#### Basic example\n\n```js\nconst EventEmitter = require('events');\nconst emitterProxy = require('ee-proxy');\n\nconst user = new EventEmitter();\nuser.once('disconnect', () =\u003e console.log('User disconnected'));\n\nclass Game extends EventEmitter {\n    constructor(user) {\n        super();\n        this._user = emitterProxy(user);\n        this._user.once('game:cancel', () =\u003e this._onUserLeft());\n        this._user.once('disconnect', () =\u003e this._onUserLeft());\n    }\n\n    start() {\n        this._user.on('game:message', message =\u003e console.log('game:message', message));\n        this._user.on('game:command', command =\u003e console.log('game:command', command));\n    }\n\n    _onUserLeft() {\n        console.log('User left the game');\n        this._user.stopListening(); // removes only game listeners (\"game:message\" and \"game:command\" events)\n        this.emit('canceled');\n    }\n}\n\nconst game = new Game(user);\ngame.start();\n\nconsole.log(user.listenerCount('disconnect')); // 2\nconsole.log(user.listenerCount('game:cancel')); // 1\nconsole.log(user.listenerCount('game:message')); // 1\nconsole.log(user.listenerCount('game:command')); // 1\n\ngame.once('canceled', () =\u003e {\n    console.log(user.listenerCount('disconnect')); // 1\n    console.log(user.listenerCount('game:cancel')); // 0\n    console.log(user.listenerCount('game:message')); // 0\n    console.log(user.listenerCount('game:command')); // 0\n});\n\nuser.emit('game:cancel');\n```\n\n#### Using of \"stopListeningAfterFirstEvent\" option\n\nSometimes you may need to listen to several events and you want to react only on first one.\nFor example, your user can have a choice: to start the game, to cancel it or the user can even disconnect.\nIn that case you can call `stopListening()` in every event listener but it's much easier just to set `stopListeningAfterFirstEvent=true`:\n\n```js\nconst user = new EventEmitter();\nuser.once('disconnect', () =\u003e console.log('User disconnected'));\n\nconst wrappedUser = emitterProxy(user, { stopListeningAfterFirstEvent: true });\nwrappedUser.once('game:start', () =\u003e console.log('User is ready to start the game'));\nwrappedUser.once('game:cancel', () =\u003e console.log('User cancelled the game'));\nwrappedUser.once('disconnect', () =\u003e console.log('User disconnected'));\n\nuser.emit('game:cancel');\n\nconsole.log(user.listenerCount('disconnect')); // 1\nconsole.log(user.listenerCount('game:start')); // 0\nconsole.log(user.listenerCount('game:cancel')); // 0\n```\n\n### Polyfill\n\nInternet Explorer and some other outdated browsers don't support `Proxy` (see [caniuse](https://caniuse.com/#search=proxy)). In this case you can use [polyfill](https://github.com/GoogleChrome/proxy-polyfill).\nBut keep in mind that all emitter properties you will use **must be known at proxy creation time** because polyfill seals an emitter object, preventing new properties from being added to it. But you can workaround it by using `fields` option:\n\n```js\nconst emitterProxy = require('ee-proxy');\nconst EventEmitter = require('events');\n\nclass Game extends EventEmitter {\n    start() {\n        console.log('Game started');\n    }\n}\n\nconst game = emitterProxy(new Game(), {\n    fields: ['something']\n});\ngame.something = '123456';\n```\n\n## Build\n\n```sh\nnpm install\nnpm run build\n```\n\n## Tests\n\n```sh\nnpm install\nnpm test\n```\n\n## License\n\n[MIT](LICENSE)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjokero%2Fee-proxy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjokero%2Fee-proxy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjokero%2Fee-proxy/lists"}