@epicgames-ps/lib-pixelstreamingcommon-ue5.5
@epicgames-ps/lib-pixelstreamingcommon-ue5.5 / Event/EventEmitter / EventEmitter
Defined in: Event/EventEmitter.ts:63
A feature-limited, but mostly drop-in replacement for Node's EventEmitter type that is implemented using EventTarget.
For those unfamiliar with Node's EventEmitter, here is some info from the official docs:
[In NodeJS] all objects that emit events are instances of the EventEmitter class. These
objects expose an eventEmitter.on() function that allows one or more
functions to be attached to named events emitted by the object. Typically,
event names are camel-cased strings but any valid JavaScript property key
can be used.
When the EventEmitter object emits an event, all of the functions attached
to that specific event are called synchronously. Any values returned by the
called listeners are ignored and discarded.
The following example shows a simple EventEmitter instance with a single
listener. The eventEmitter.on() method is used to register listeners, while
the eventEmitter.emit() method is used to trigger the event.
import { EventEmitter } from 'node:events';
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');EventTarget
new EventEmitter():
EventEmitter
Defined in: Event/EventEmitter.ts:66
EventTarget.constructor
addListener(
eventName,listener):this
Defined in: Event/EventEmitter.ts:96
Alias for emitter.on(eventName, listener).
string
(...args) => void
this
emit(
eventName, ...args):boolean
Defined in: Event/EventEmitter.ts:262
Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments
to each.
Returns true if the event had listeners, false otherwise.
import { EventEmitter } from 'node:events';
const myEmitter = new EventEmitter();
// First listener
myEmitter.on('event', function firstListener() {
console.log('Helloooo! first listener');
});
// Second listener
myEmitter.on('event', function secondListener(arg1, arg2) {
console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
});
// Third listener
myEmitter.on('event', function thirdListener(...args) {
const parameters = args.join(', ');
console.log(`event with parameters ${parameters} in third listener`);
});
console.log(myEmitter.listeners('event'));
myEmitter.emit('event', 1, 2, 3, 4, 5);
// Prints:
// [
// [Function: firstListener],
// [Function: secondListener],
// [Function: thirdListener]
// ]
// Helloooo! first listener
// event with parameters 1, 2 in second listener
// event with parameters 1, 2, 3, 4, 5 in third listenerstring
...any[]
boolean
off(
eventName,listener):this
Defined in: Event/EventEmitter.ts:196
Alias for emitter.removeListener().
string
(...args) => void
this
on(
eventName,listener):this
Defined in: Event/EventEmitter.ts:115
Adds the listener function to the end of the listeners array for the event
named eventName.
server.on('connection', (stream) => {
console.log('someone connected!');
});Returns a reference to the EventEmitter, so that calls can be chained.
string
The name of the event.
(...args) => void
The callback function
this
once(
eventName,listener):this
Defined in: Event/EventEmitter.ts:148
Adds a one-time listener function for the event named eventName. The
next time eventName is triggered, this listener is removed and then invoked.
server.once('connection', (stream) => {
console.log('Ah, we have our first user!');
});Returns a reference to the EventEmitter, so that calls can be chained.
string
The name of the event.
(...args) => void
The callback function
this
removeAllListeners(
eventName):this
Defined in: Event/EventEmitter.ts:204
Removes all listeners, or those of the specified eventName.
Returns a reference to the EventEmitter, so that calls can be chained.
string
this
removeListener(
eventName,listener):this
Defined in: Event/EventEmitter.ts:188
Removes the specified listener from this EventEmitter.
const callback = (stream) => {
console.log('someone connected!');
};
server.on('connection', callback);
// ...
server.removeListener('connection', callback);Returns a reference to the EventEmitter, so that calls can be chained.
string
(...args) => void
this