import type { Id, IOLike, JsonObject, StateNotifierSubjects, SubscriptionCallback, UserInfo, WebSocketState, } from "@pluv/types"; import type { Subject } from "wonka"; import { makeSubject, subscribe } from "wonka"; type InferSubjectValue< TIO extends IOLike, TPresence extends Record, TSubject extends keyof StateNotifierSubjects, > = StateNotifierSubjects[TSubject] extends Subject ? Id : never; export class StateNotifier = {}> { public subjects: StateNotifierSubjects = { connection: makeSubject>>(), "my-presence": makeSubject(), myself: makeSubject>> | null>(), others: makeSubject>[]>(), "storage-loaded": makeSubject(), }; public subscribe>( name: TSubject, callback: SubscriptionCallback, ): () => void { const source = this.subjects[name]?.source as any; if (!source) throw new Error(`Could not subscribe to source: ${name}`); return subscribe(callback)(source).unsubscribe; } }