@tanstack/db
Version:
A reactive client store for building super fast apps on sync
95 lines (94 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
class EventEmitter {
constructor() {
this.listeners = /* @__PURE__ */ new Map();
}
/**
* Subscribe to an event
* @param event - Event name to listen for
* @param callback - Function to call when event is emitted
* @returns Unsubscribe function
*/
on(event, callback) {
if (!this.listeners.has(event)) {
this.listeners.set(event, /* @__PURE__ */ new Set());
}
this.listeners.get(event).add(callback);
return () => {
this.listeners.get(event)?.delete(callback);
};
}
/**
* Subscribe to an event once (automatically unsubscribes after first emission)
* @param event - Event name to listen for
* @param callback - Function to call when event is emitted
* @returns Unsubscribe function
*/
once(event, callback) {
const unsubscribe = this.on(event, (eventPayload) => {
callback(eventPayload);
unsubscribe();
});
return unsubscribe;
}
/**
* Unsubscribe from an event
* @param event - Event name to stop listening for
* @param callback - Function to remove
*/
off(event, callback) {
this.listeners.get(event)?.delete(callback);
}
/**
* Wait for an event to be emitted
* @param event - Event name to wait for
* @param timeout - Optional timeout in milliseconds
* @returns Promise that resolves with the event payload
*/
waitFor(event, timeout) {
return new Promise((resolve, reject) => {
let timeoutId;
const unsubscribe = this.on(event, (eventPayload) => {
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = void 0;
}
resolve(eventPayload);
unsubscribe();
});
if (timeout) {
timeoutId = setTimeout(() => {
timeoutId = void 0;
unsubscribe();
reject(new Error(`Timeout waiting for event ${String(event)}`));
}, timeout);
}
});
}
/**
* Emit an event to all listeners
* @param event - Event name to emit
* @param eventPayload - Event payload
* @internal For use by subclasses - subclasses should wrap this with a public emit if needed
*/
emitInner(event, eventPayload) {
this.listeners.get(event)?.forEach((listener) => {
try {
listener(eventPayload);
} catch (error) {
queueMicrotask(() => {
throw error;
});
}
});
}
/**
* Clear all listeners
*/
clearListeners() {
this.listeners.clear();
}
}
exports.EventEmitter = EventEmitter;
//# sourceMappingURL=event-emitter.cjs.map