ee-proxy
Event emitter proxy for easy local listeners cleanup
Note: This module works in browsers and Node.js >= 6.0. Use Proxy
and Array
polyfills for Internet Explorer
Table of Contents
Demo
Try demo on RunKit.
Installation
npm install ee-proxy
Node.js
const emitterProxy = ;
Browser
<script src="node_modules/ee-proxy/dist/ee-proxy.js">
or minified version
<script src="node_modules/ee-proxy/dist/ee-proxy.min.js">
You can use the module with AMD/CommonJS or just use window.emitterProxy
.
Overview
ee-proxy
allows you to easily and safely remove listeners attached to event emitter without touching listeners added in other pieces of code.
Unlike other similar modules (for example, ultron) this one works seamlessly and allows to call your custom methods on event emitter:
const emitterProxy = ;const EventEmitter = ; { console; } const game = ;gamestart; // Game started console; // trueconsole; // true
Usage
emitterProxy(emitter, [options])
Parameters
emitter
(EventEmitter)[options]
(Object)[stopListeningAfterFirstEvent]
(boolean) - Iftrue
,ee-proxy
removes all listeners attached to the wrapped emitter when first event is triggered (might be useful in some cases)[removeMethod]
(string) - Name of the method for listeners cleanup (default -stopListening
)[addListenerMethods]
(string[]) - Methods which are intercepted byee-proxy
for keeping attached to emitter listeners (default -['on', 'once', 'addListener', 'prependListener', 'prependOnceListener', 'onceAny', 'onAny']
)[fields]
(string[]) - Option specially forProxy
polyfill (see below)
Return value
(EventEmitter) - Proxy object (which is !==
original emitter)
const user = ;user; const wrappedUser = ;wrappedUser;wrappedUser;wrappedUser; wrappedUser; // removes all attached to the wrapped emitter listenersconsole; // 1 // wrappedUser.stopListening('game:start'); // you can specify a particular event// wrappedUser.stopListening('game:start', 'game:cancel'); // or even list several events
Examples
Basic example
const EventEmitter = ;const emitterProxy = ; const user = ;user; { super; this_user = ; this_user; this_user; } { this_user; this_user; } { console; this_user; // removes only game listeners ("game:message" and "game:command" events) this; } const game = user;gamestart; console; // 2console; // 1console; // 1console; // 1 game; user;
Using of "stopListeningAfterFirstEvent" option
Sometimes you may need to listen to several events and you want to react only on first one.
For example, your user can have a choice: to start the game, to cancel it or the user can even disconnect.
In that case you can call stopListening()
in every event listener but it's much easier just to set stopListeningAfterFirstEvent=true
:
const user = ;user; const wrappedUser = ;wrappedUser;wrappedUser;wrappedUser; user; console; // 1console; // 0console; // 0
Polyfill
Internet Explorer and some other outdated browsers don't support Proxy
(see caniuse). In this case you can use polyfill.
But 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:
const emitterProxy = ;const EventEmitter = ; { console; } const game = ;gamesomething = '123456';
Build
npm installnpm run build
Tests
npm installnpm test