Files
sentence1/G102-sequence/sdk/package/dist/esm/EventEmitter.js
lubukhu 65fd0158a3
All checks were successful
Deploy to Production / deploy (push) Successful in 8s
up
2026-01-24 13:35:11 +07:00

61 lines
1.5 KiB
JavaScript

/**
* Game Iframe SDK - Event Emitter
* Simple typed event emitter for SDK
*/
export class EventEmitter {
constructor() {
this.handlers = new Map();
}
/**
* Subscribe to an event
*/
on(event, handler) {
if (!this.handlers.has(event)) {
this.handlers.set(event, new Set());
}
this.handlers.get(event).add(handler);
// Return unsubscribe function
return () => this.off(event, handler);
}
/**
* Subscribe to an event (once)
*/
once(event, handler) {
const wrappedHandler = (data) => {
this.off(event, wrappedHandler);
handler(data);
};
return this.on(event, wrappedHandler);
}
/**
* Unsubscribe from an event
*/
off(event, handler) {
this.handlers.get(event)?.delete(handler);
}
/**
* Emit an event
*/
emit(event, data) {
this.handlers.get(event)?.forEach(handler => {
try {
handler(data);
}
catch (err) {
console.error(`[EventEmitter] Error in handler for "${String(event)}":`, err);
}
});
}
/**
* Remove all handlers for an event (or all events)
*/
removeAllListeners(event) {
if (event) {
this.handlers.delete(event);
}
else {
this.handlers.clear();
}
}
}
//# sourceMappingURL=EventEmitter.js.map