|
| 1 | +[**@epicgames-ps/lib-pixelstreamingcommon-ue5.5**](../../../README.md) |
| 2 | + |
| 3 | +*** |
| 4 | + |
| 5 | +[@epicgames-ps/lib-pixelstreamingcommon-ue5.5](../../../README.md) / [Event/EventEmitter](../README.md) / EventEmitter |
| 6 | + |
| 7 | +# Class: EventEmitter |
| 8 | + |
| 9 | +A feature-limited, but _mostly_ drop-in replacement for Node's EventEmitter type that is implemented using EventTarget. |
| 10 | + |
| 11 | +For those unfamiliar with Node's EventEmitter, here is some info from the official docs: |
| 12 | + |
| 13 | +[In NodeJS] all objects that emit events are instances of the `EventEmitter` class. These |
| 14 | +objects expose an `eventEmitter.on()` function that allows one or more |
| 15 | +functions to be attached to named events emitted by the object. Typically, |
| 16 | +event names are camel-cased strings but any valid JavaScript property key |
| 17 | +can be used. |
| 18 | + |
| 19 | +When the `EventEmitter` object emits an event, all of the functions attached |
| 20 | +to that specific event are called _synchronously_. Any values returned by the |
| 21 | +called listeners are _ignored_ and discarded. |
| 22 | + |
| 23 | +The following example shows a simple `EventEmitter` instance with a single |
| 24 | +listener. The `eventEmitter.on()` method is used to register listeners, while |
| 25 | +the `eventEmitter.emit()` method is used to trigger the event. |
| 26 | + |
| 27 | +```js |
| 28 | +import { EventEmitter } from 'node:events'; |
| 29 | + |
| 30 | +class MyEmitter extends EventEmitter {} |
| 31 | + |
| 32 | +const myEmitter = new MyEmitter(); |
| 33 | +myEmitter.on('event', () => { |
| 34 | + console.log('an event occurred!'); |
| 35 | +}); |
| 36 | +myEmitter.emit('event'); |
| 37 | +``` |
| 38 | + |
| 39 | +## Extends |
| 40 | + |
| 41 | +- `EventTarget` |
| 42 | + |
| 43 | +## Extended by |
| 44 | + |
| 45 | +- [`SignallingProtocol`](../../../Protocol/SignallingProtocol/classes/SignallingProtocol.md) |
| 46 | +- [`ITransport`](../../../Transport/ITransport/interfaces/ITransport.md) |
| 47 | +- [`WebSocketTransport`](../../../Transport/WebSocketTransport/classes/WebSocketTransport.md) |
| 48 | +- [`WebSocketTransportNJS`](../../../Transport/WebSocketTransportNJS/classes/WebSocketTransportNJS.md) |
| 49 | + |
| 50 | +## Constructors |
| 51 | + |
| 52 | +### new EventEmitter() |
| 53 | + |
| 54 | +> **new EventEmitter**(): [`EventEmitter`](EventEmitter.md) |
| 55 | +
|
| 56 | +#### Returns |
| 57 | + |
| 58 | +[`EventEmitter`](EventEmitter.md) |
| 59 | + |
| 60 | +#### Overrides |
| 61 | + |
| 62 | +`EventTarget.constructor` |
| 63 | + |
| 64 | +#### Defined in |
| 65 | + |
| 66 | +[Event/EventEmitter.ts:67](https://github.com/mcottontensor/PixelStreamingInfrastructure/blob/457a0dc3b3c9a47385d92ffbc69496977cee683b/Common/src/Event/EventEmitter.ts#L67) |
| 67 | + |
| 68 | +## Methods |
| 69 | + |
| 70 | +### addListener() |
| 71 | + |
| 72 | +> **addListener**(`eventName`, `listener`): `this` |
| 73 | +
|
| 74 | +Alias for `emitter.on(eventName, listener)`. |
| 75 | + |
| 76 | +#### Parameters |
| 77 | + |
| 78 | +##### eventName |
| 79 | + |
| 80 | +`string` |
| 81 | + |
| 82 | +##### listener |
| 83 | + |
| 84 | +(...`args`) => `void` |
| 85 | + |
| 86 | +#### Returns |
| 87 | + |
| 88 | +`this` |
| 89 | + |
| 90 | +#### Defined in |
| 91 | + |
| 92 | +[Event/EventEmitter.ts:97](https://github.com/mcottontensor/PixelStreamingInfrastructure/blob/457a0dc3b3c9a47385d92ffbc69496977cee683b/Common/src/Event/EventEmitter.ts#L97) |
| 93 | + |
| 94 | +*** |
| 95 | + |
| 96 | +### emit() |
| 97 | + |
| 98 | +> **emit**(`eventName`, ...`args`): `boolean` |
| 99 | +
|
| 100 | +Synchronously calls each of the listeners registered for the event named `eventName`, in the order they were registered, passing the supplied arguments |
| 101 | +to each. |
| 102 | + |
| 103 | +Returns `true` if the event had listeners, `false` otherwise. |
| 104 | + |
| 105 | +```js |
| 106 | +import { EventEmitter } from 'node:events'; |
| 107 | +const myEmitter = new EventEmitter(); |
| 108 | + |
| 109 | +// First listener |
| 110 | +myEmitter.on('event', function firstListener() { |
| 111 | + console.log('Helloooo! first listener'); |
| 112 | +}); |
| 113 | +// Second listener |
| 114 | +myEmitter.on('event', function secondListener(arg1, arg2) { |
| 115 | + console.log(`event with parameters ${arg1}, ${arg2} in second listener`); |
| 116 | +}); |
| 117 | +// Third listener |
| 118 | +myEmitter.on('event', function thirdListener(...args) { |
| 119 | + const parameters = args.join(', '); |
| 120 | + console.log(`event with parameters ${parameters} in third listener`); |
| 121 | +}); |
| 122 | + |
| 123 | +console.log(myEmitter.listeners('event')); |
| 124 | + |
| 125 | +myEmitter.emit('event', 1, 2, 3, 4, 5); |
| 126 | + |
| 127 | +// Prints: |
| 128 | +// [ |
| 129 | +// [Function: firstListener], |
| 130 | +// [Function: secondListener], |
| 131 | +// [Function: thirdListener] |
| 132 | +// ] |
| 133 | +// Helloooo! first listener |
| 134 | +// event with parameters 1, 2 in second listener |
| 135 | +// event with parameters 1, 2, 3, 4, 5 in third listener |
| 136 | +``` |
| 137 | + |
| 138 | +#### Parameters |
| 139 | + |
| 140 | +##### eventName |
| 141 | + |
| 142 | +`string` |
| 143 | + |
| 144 | +##### args |
| 145 | + |
| 146 | +...`any`[] |
| 147 | + |
| 148 | +#### Returns |
| 149 | + |
| 150 | +`boolean` |
| 151 | + |
| 152 | +#### Defined in |
| 153 | + |
| 154 | +[Event/EventEmitter.ts:263](https://github.com/mcottontensor/PixelStreamingInfrastructure/blob/457a0dc3b3c9a47385d92ffbc69496977cee683b/Common/src/Event/EventEmitter.ts#L263) |
| 155 | + |
| 156 | +*** |
| 157 | + |
| 158 | +### off() |
| 159 | + |
| 160 | +> **off**(`eventName`, `listener`): `this` |
| 161 | +
|
| 162 | +Alias for `emitter.removeListener()`. |
| 163 | + |
| 164 | +#### Parameters |
| 165 | + |
| 166 | +##### eventName |
| 167 | + |
| 168 | +`string` |
| 169 | + |
| 170 | +##### listener |
| 171 | + |
| 172 | +(...`args`) => `void` |
| 173 | + |
| 174 | +#### Returns |
| 175 | + |
| 176 | +`this` |
| 177 | + |
| 178 | +#### Defined in |
| 179 | + |
| 180 | +[Event/EventEmitter.ts:197](https://github.com/mcottontensor/PixelStreamingInfrastructure/blob/457a0dc3b3c9a47385d92ffbc69496977cee683b/Common/src/Event/EventEmitter.ts#L197) |
| 181 | + |
| 182 | +*** |
| 183 | + |
| 184 | +### on() |
| 185 | + |
| 186 | +> **on**(`eventName`, `listener`): `this` |
| 187 | +
|
| 188 | +Adds the `listener` function to the end of the listeners array for the event |
| 189 | +named `eventName`. |
| 190 | + |
| 191 | +```js |
| 192 | +server.on('connection', (stream) => { |
| 193 | + console.log('someone connected!'); |
| 194 | +}); |
| 195 | +``` |
| 196 | + |
| 197 | +Returns a reference to the `EventEmitter`, so that calls can be chained. |
| 198 | + |
| 199 | +#### Parameters |
| 200 | + |
| 201 | +##### eventName |
| 202 | + |
| 203 | +`string` |
| 204 | + |
| 205 | +The name of the event. |
| 206 | + |
| 207 | +##### listener |
| 208 | + |
| 209 | +(...`args`) => `void` |
| 210 | + |
| 211 | +The callback function |
| 212 | + |
| 213 | +#### Returns |
| 214 | + |
| 215 | +`this` |
| 216 | + |
| 217 | +#### Defined in |
| 218 | + |
| 219 | +[Event/EventEmitter.ts:116](https://github.com/mcottontensor/PixelStreamingInfrastructure/blob/457a0dc3b3c9a47385d92ffbc69496977cee683b/Common/src/Event/EventEmitter.ts#L116) |
| 220 | + |
| 221 | +*** |
| 222 | + |
| 223 | +### once() |
| 224 | + |
| 225 | +> **once**(`eventName`, `listener`): `this` |
| 226 | +
|
| 227 | +Adds a **one-time** `listener` function for the event named `eventName`. The |
| 228 | +next time `eventName` is triggered, this listener is removed and then invoked. |
| 229 | + |
| 230 | +```js |
| 231 | +server.once('connection', (stream) => { |
| 232 | + console.log('Ah, we have our first user!'); |
| 233 | +}); |
| 234 | +``` |
| 235 | + |
| 236 | +Returns a reference to the `EventEmitter`, so that calls can be chained. |
| 237 | + |
| 238 | +#### Parameters |
| 239 | + |
| 240 | +##### eventName |
| 241 | + |
| 242 | +`string` |
| 243 | + |
| 244 | +The name of the event. |
| 245 | + |
| 246 | +##### listener |
| 247 | + |
| 248 | +(...`args`) => `void` |
| 249 | + |
| 250 | +The callback function |
| 251 | + |
| 252 | +#### Returns |
| 253 | + |
| 254 | +`this` |
| 255 | + |
| 256 | +#### Defined in |
| 257 | + |
| 258 | +[Event/EventEmitter.ts:149](https://github.com/mcottontensor/PixelStreamingInfrastructure/blob/457a0dc3b3c9a47385d92ffbc69496977cee683b/Common/src/Event/EventEmitter.ts#L149) |
| 259 | + |
| 260 | +*** |
| 261 | + |
| 262 | +### removeAllListeners() |
| 263 | + |
| 264 | +> **removeAllListeners**(`eventName`): `this` |
| 265 | +
|
| 266 | +Removes all listeners, or those of the specified `eventName`. |
| 267 | +Returns a reference to the `EventEmitter`, so that calls can be chained. |
| 268 | + |
| 269 | +#### Parameters |
| 270 | + |
| 271 | +##### eventName |
| 272 | + |
| 273 | +`string` |
| 274 | + |
| 275 | +#### Returns |
| 276 | + |
| 277 | +`this` |
| 278 | + |
| 279 | +#### Defined in |
| 280 | + |
| 281 | +[Event/EventEmitter.ts:205](https://github.com/mcottontensor/PixelStreamingInfrastructure/blob/457a0dc3b3c9a47385d92ffbc69496977cee683b/Common/src/Event/EventEmitter.ts#L205) |
| 282 | + |
| 283 | +*** |
| 284 | + |
| 285 | +### removeListener() |
| 286 | + |
| 287 | +> **removeListener**(`eventName`, `listener`): `this` |
| 288 | +
|
| 289 | +Removes the specified `listener` from this EventEmitter. |
| 290 | + |
| 291 | +```js |
| 292 | +const callback = (stream) => { |
| 293 | + console.log('someone connected!'); |
| 294 | +}; |
| 295 | +server.on('connection', callback); |
| 296 | +// ... |
| 297 | +server.removeListener('connection', callback); |
| 298 | +``` |
| 299 | +Returns a reference to the `EventEmitter`, so that calls can be chained. |
| 300 | + |
| 301 | +#### Parameters |
| 302 | + |
| 303 | +##### eventName |
| 304 | + |
| 305 | +`string` |
| 306 | + |
| 307 | +##### listener |
| 308 | + |
| 309 | +(...`args`) => `void` |
| 310 | + |
| 311 | +#### Returns |
| 312 | + |
| 313 | +`this` |
| 314 | + |
| 315 | +#### Defined in |
| 316 | + |
| 317 | +[Event/EventEmitter.ts:189](https://github.com/mcottontensor/PixelStreamingInfrastructure/blob/457a0dc3b3c9a47385d92ffbc69496977cee683b/Common/src/Event/EventEmitter.ts#L189) |
0 commit comments