feat: use postgres for change notification
Some checks failed
Build and release / Release (push) Has been skipped
Build and release / Build (push) Failing after 1m14s
Build and release / update-release-draft (push) Has been skipped

This commit is contained in:
Morten Olsen
2025-12-10 23:21:25 +01:00
parent 3641e86da5
commit 904b0f783e
10 changed files with 274 additions and 43 deletions

View File

@@ -1,6 +1,7 @@
import type { ExplicitAny } from '../global.js';
type EventListener<T extends unknown[]> = (...args: T) => void | Promise<void>;
type SubscribeListener<T> = (type: T) => void | Promise<void>;
type OnOptions = {
abortSignal?: AbortSignal;
@@ -8,8 +9,25 @@ type OnOptions = {
class EventEmitter<T extends Record<string, (...args: ExplicitAny[]) => void | Promise<void>>> {
#listeners = new Map<keyof T, Set<EventListener<ExplicitAny>>>();
#subscribeListeners = new Set<SubscribeListener<keyof T>>();
onSubscribe = (callback: SubscribeListener<keyof T>, options: OnOptions = {}) => {
const { abortSignal } = options;
const callbackClone = (type: keyof T) => callback(type);
this.#subscribeListeners.add(callbackClone);
const abortController = new AbortController();
abortSignal?.addEventListener('abort', abortController.abort);
abortController.signal.addEventListener('abort', () => {
this.#subscribeListeners.difference(new Set([callbackClone]));
});
return abortController.abort;
};
on = <K extends keyof T>(event: K, callback: EventListener<Parameters<T[K]>>, options: OnOptions = {}) => {
for (const subscribeListener of this.#subscribeListeners) {
subscribeListener(event);
}
const { abortSignal } = options;
if (!this.#listeners.has(event)) {
this.#listeners.set(event, new Set());