Collection of utility functions used in web3.js for Ethereum dApp development
—
Node.js-compatible EventEmitter implementation for browser environments with support for max listeners configuration. Provides a consistent event handling interface across different JavaScript environments.
Browser-compatible EventEmitter that copies the behavior of Node.js EventEmitter class.
/**
* EventEmitter class compatible with Node.js EventEmitter interface
* Extends EventEmitter3 for browser environment compatibility
*/
class EventEmitter extends EventEmitter3 {
/**
* Sets the maximum number of listeners for this EventEmitter
* @param maxListeners - Maximum number of listeners
* @returns this EventEmitter instance for chaining
*/
setMaxListeners(maxListeners: number): this;
/**
* Gets the current maximum number of listeners
* @returns Current maximum listeners value
*/
getMaxListeners(): number;
}import { EventEmitter } from "web3-utils";
// Create new EventEmitter instance
const emitter = new EventEmitter();
// Set maximum listeners (default is Number.MAX_SAFE_INTEGER)
emitter.setMaxListeners(10);
console.log(emitter.getMaxListeners()); // 10
// Standard event handling (inherited from EventEmitter3)
emitter.on('data', (data) => {
console.log('Received data:', data);
});
emitter.once('connect', () => {
console.log('Connected once');
});
// Emit events
emitter.emit('data', { message: 'Hello World' });
emitter.emit('connect');
// Remove listeners
emitter.removeListener('data', handler);
emitter.removeAllListeners('connect');
// Check listener count
const listenerCount = emitter.listenerCount('data');The EventEmitter class inherits all standard EventEmitter methods:
// Event registration
on(event: string | symbol, listener: Function): this;
once(event: string | symbol, listener: Function): this;
addListener(event: string | symbol, listener: Function): this;
// Event emission
emit(event: string | symbol, ...args: any[]): boolean;
// Event removal
removeListener(event: string | symbol, listener: Function): this;
removeAllListeners(event?: string | symbol): this;
off(event: string | symbol, listener: Function): this;
// Event inspection
listeners(event: string | symbol): Function[];
listenerCount(event: string | symbol): number;
eventNames(): (string | symbol)[];import { EventEmitter } from "web3-utils";
class Web3Provider extends EventEmitter {
constructor() {
super();
this.setMaxListeners(100); // Allow many subscribers
}
connect() {
// Connection logic
this.emit('connect');
}
disconnect() {
// Disconnection logic
this.emit('disconnect');
}
onMessage(message: any) {
this.emit('message', message);
}
onError(error: Error) {
this.emit('error', error);
}
}
// Usage
const provider = new Web3Provider();
provider.on('connect', () => {
console.log('Provider connected');
});
provider.on('message', (message) => {
console.log('Received message:', message);
});
provider.on('error', (error) => {
console.error('Provider error:', error);
});import { EventEmitter } from "web3-utils";
class SubscriptionManager extends EventEmitter {
private subscriptions = new Map();
subscribe(id: string, callback: Function) {
this.on(id, callback);
this.subscriptions.set(id, callback);
}
unsubscribe(id: string) {
const callback = this.subscriptions.get(id);
if (callback) {
this.removeListener(id, callback);
this.subscriptions.delete(id);
}
}
notifySubscribers(id: string, data: any) {
this.emit(id, data);
}
cleanup() {
this.removeAllListeners();
this.subscriptions.clear();
}
}import { EventEmitter } from "web3-utils";
class DataProcessor extends EventEmitter {
constructor() {
super();
// Set up error handling
this.on('error', (error) => {
console.error('Processing error:', error);
});
}
processData(data: any) {
try {
// Process data
const result = this.transform(data);
this.emit('processed', result);
} catch (error) {
this.emit('error', error);
}
}
private transform(data: any) {
// Transformation logic
return data;
}
}
// Usage with error handling
const processor = new DataProcessor();
processor.on('processed', (result) => {
console.log('Data processed:', result);
});
processor.on('error', (error) => {
console.error('Failed to process data:', error);
});The EventEmitter class provides Node.js EventEmitter compatibility in browser environments by extending EventEmitter3. This ensures consistent behavior across different JavaScript runtime environments while maintaining the familiar Node.js EventEmitter API.
Key compatibility features:
Install with Tessl CLI
npx tessl i tessl/npm-web3-utils