or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

conversion.mddata-manipulation.mdevents.mdhashing.mdindex.mdjson-rpc.mdpromises.mdproviders.mdrandom-validation.md

events.mddocs/

0

# Event System

1

2

Node.js-compatible EventEmitter implementation for browser environments with support for max listeners configuration. Provides a consistent event handling interface across different JavaScript environments.

3

4

## Capabilities

5

6

### EventEmitter Class

7

8

Browser-compatible EventEmitter that copies the behavior of Node.js EventEmitter class.

9

10

```typescript { .api }

11

/**

12

* EventEmitter class compatible with Node.js EventEmitter interface

13

* Extends EventEmitter3 for browser environment compatibility

14

*/

15

class EventEmitter extends EventEmitter3 {

16

/**

17

* Sets the maximum number of listeners for this EventEmitter

18

* @param maxListeners - Maximum number of listeners

19

* @returns this EventEmitter instance for chaining

20

*/

21

setMaxListeners(maxListeners: number): this;

22

23

/**

24

* Gets the current maximum number of listeners

25

* @returns Current maximum listeners value

26

*/

27

getMaxListeners(): number;

28

}

29

```

30

31

## Usage Examples

32

33

```typescript

34

import { EventEmitter } from "web3-utils";

35

36

// Create new EventEmitter instance

37

const emitter = new EventEmitter();

38

39

// Set maximum listeners (default is Number.MAX_SAFE_INTEGER)

40

emitter.setMaxListeners(10);

41

console.log(emitter.getMaxListeners()); // 10

42

43

// Standard event handling (inherited from EventEmitter3)

44

emitter.on('data', (data) => {

45

console.log('Received data:', data);

46

});

47

48

emitter.once('connect', () => {

49

console.log('Connected once');

50

});

51

52

// Emit events

53

emitter.emit('data', { message: 'Hello World' });

54

emitter.emit('connect');

55

56

// Remove listeners

57

emitter.removeListener('data', handler);

58

emitter.removeAllListeners('connect');

59

60

// Check listener count

61

const listenerCount = emitter.listenerCount('data');

62

```

63

64

## Inherited Methods from EventEmitter3

65

66

The EventEmitter class inherits all standard EventEmitter methods:

67

68

```typescript { .api }

69

// Event registration

70

on(event: string | symbol, listener: Function): this;

71

once(event: string | symbol, listener: Function): this;

72

addListener(event: string | symbol, listener: Function): this;

73

74

// Event emission

75

emit(event: string | symbol, ...args: any[]): boolean;

76

77

// Event removal

78

removeListener(event: string | symbol, listener: Function): this;

79

removeAllListeners(event?: string | symbol): this;

80

off(event: string | symbol, listener: Function): this;

81

82

// Event inspection

83

listeners(event: string | symbol): Function[];

84

listenerCount(event: string | symbol): number;

85

eventNames(): (string | symbol)[];

86

```

87

88

## Common Usage Patterns

89

90

### Provider Event Handling

91

92

```typescript

93

import { EventEmitter } from "web3-utils";

94

95

class Web3Provider extends EventEmitter {

96

constructor() {

97

super();

98

this.setMaxListeners(100); // Allow many subscribers

99

}

100

101

connect() {

102

// Connection logic

103

this.emit('connect');

104

}

105

106

disconnect() {

107

// Disconnection logic

108

this.emit('disconnect');

109

}

110

111

onMessage(message: any) {

112

this.emit('message', message);

113

}

114

115

onError(error: Error) {

116

this.emit('error', error);

117

}

118

}

119

120

// Usage

121

const provider = new Web3Provider();

122

123

provider.on('connect', () => {

124

console.log('Provider connected');

125

});

126

127

provider.on('message', (message) => {

128

console.log('Received message:', message);

129

});

130

131

provider.on('error', (error) => {

132

console.error('Provider error:', error);

133

});

134

```

135

136

### Subscription Management

137

138

```typescript

139

import { EventEmitter } from "web3-utils";

140

141

class SubscriptionManager extends EventEmitter {

142

private subscriptions = new Map();

143

144

subscribe(id: string, callback: Function) {

145

this.on(id, callback);

146

this.subscriptions.set(id, callback);

147

}

148

149

unsubscribe(id: string) {

150

const callback = this.subscriptions.get(id);

151

if (callback) {

152

this.removeListener(id, callback);

153

this.subscriptions.delete(id);

154

}

155

}

156

157

notifySubscribers(id: string, data: any) {

158

this.emit(id, data);

159

}

160

161

cleanup() {

162

this.removeAllListeners();

163

this.subscriptions.clear();

164

}

165

}

166

```

167

168

### Error Handling Pattern

169

170

```typescript

171

import { EventEmitter } from "web3-utils";

172

173

class DataProcessor extends EventEmitter {

174

constructor() {

175

super();

176

177

// Set up error handling

178

this.on('error', (error) => {

179

console.error('Processing error:', error);

180

});

181

}

182

183

processData(data: any) {

184

try {

185

// Process data

186

const result = this.transform(data);

187

this.emit('processed', result);

188

} catch (error) {

189

this.emit('error', error);

190

}

191

}

192

193

private transform(data: any) {

194

// Transformation logic

195

return data;

196

}

197

}

198

199

// Usage with error handling

200

const processor = new DataProcessor();

201

202

processor.on('processed', (result) => {

203

console.log('Data processed:', result);

204

});

205

206

processor.on('error', (error) => {

207

console.error('Failed to process data:', error);

208

});

209

```

210

211

## Browser Compatibility

212

213

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.

214

215

Key compatibility features:

216

- All standard EventEmitter methods

217

- Maximum listeners configuration

218

- Memory leak warnings (when exceeding max listeners)

219

- Proper event emission return values

220

- Symbol and string event name support