import type { Id, IOLike, JsonObject, OtherSubscriptionCallback, OthersSubscriptionCallback, OthersSubscriptionEvent, UserInfo, } from "@pluv/types"; import type { Subject } from "wonka"; import { makeSubject, subscribe, TypeOfSource } from "wonka"; export class UsersNotifier = {}> { public readonly others = makeSubject<{ others: readonly Id>[]; event: OthersSubscriptionEvent; }>(); private _otherSubjects = new Map< [clientId: string][0], Subject> | null> >(); public clear(): void { this._otherSubjects.forEach((subject) => { subject.next(null); }); this._otherSubjects.clear(); } public delete(clientId: string): void { this.other(clientId).next(null); this._otherSubjects.delete(clientId); } public other(clientId: string): Subject> | null> { const subject = this._otherSubjects.get(clientId); if (subject) return subject; const newSubject = makeSubject>>(); this._otherSubjects.set(clientId, newSubject); return newSubject; } public subscribeOther( clientId: string, callback: OtherSubscriptionCallback, ): () => void { const source = this.other(clientId).source; const subscription = subscribe(callback)(source); return subscription.unsubscribe; } public subscribeOthers(callback: OthersSubscriptionCallback): () => void { const subscription = subscribe>( ({ others, event }) => callback(others, event), )(this.others.source); return subscription.unsubscribe; } }