Event based JavaScript for the browser with comprehensive event management API
91
Build an event listener manager that provides a unified interface for batch adding and removing event handlers with support for both named events and pattern-based event groups.
Implement a ListenerManager class that wraps an EventEmitter instance and provides methods to:
The manager should handle the complexity of listener manipulation while providing a clean, simple API for users.
The manager supports adding multiple listeners to events in a single call.
batchAdd('data', [fn1, fn2, fn3]), then emitting 'data' with value 'test' causes all three functions to execute with 'test' @testbatchAdd(/user\..*/, handlerFn), then emitting both 'user.login' and 'user.logout' causes handlerFn to execute for both events @testThe manager supports removing multiple listeners from events in a single call.
batchRemove('data', [fn1, fn2]) removes those two listeners, and emitting 'data' only triggers fn3 @testbatchRemove(/user\..*/, handlerFn) removes the listener from all matching events, and emitting those events no longer triggers the handler @test@generates
/**
* ListenerManager wraps an EventEmitter to provide batch listener operations
*/
class ListenerManager {
/**
* Creates a new ListenerManager
* @param {EventEmitter} emitter - The EventEmitter instance to manage
*/
constructor(emitter) {}
/**
* Batch add listeners to an event or events
* @param {string|RegExp} event - Event name or pattern
* @param {Function|Function[]} listeners - Single listener or array of listeners
* @returns {ListenerManager} - Returns this for chaining
*/
batchAdd(event, listeners) {}
/**
* Batch remove listeners from an event or events
* @param {string|RegExp} event - Event name or pattern
* @param {Function|Function[]} listeners - Single listener or array of listeners
* @returns {ListenerManager} - Returns this for chaining
*/
batchRemove(event, listeners) {}
/**
* Define events for pattern matching support
* @param {string[]} eventNames - Array of event names to define
* @returns {ListenerManager} - Returns this for chaining
*/
defineEvents(eventNames) {}
/**
* Get the underlying EventEmitter instance
* @returns {EventEmitter}
*/
getEmitter() {}
}
module.exports = ListenerManager;Provides the underlying event emitter functionality with support for listener manipulation.
Install with Tessl CLI
npx tessl i tessl/npm-wolfy87-eventemitterdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10