Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103import EffemModelsimport Foundation
/// One frame on the `xyz.effem.feed.subscribeComments` stream, encoded as/// `{"type": "...", ...}` with the same fields the Go handler wrote.struct CommentStreamEvent: Encodable, Sendable { enum Kind: String, Codable, Sendable { case created case deleted case liked case unliked }
let type: Kind var comment: Comment? var uri: String? var commentUri: String? var likeCount: Int? var actorDid: String?
static func created(_ comment: Comment) -> CommentStreamEvent { CommentStreamEvent(type: .created, comment: comment) }
static func deleted(uri: String) -> CommentStreamEvent { CommentStreamEvent(type: .deleted, uri: uri) }
static func liked(commentURI: String, likeCount: Int, actorDID: String) -> CommentStreamEvent { CommentStreamEvent(type: .liked, commentUri: commentURI, likeCount: likeCount, actorDid: actorDID) }
static func unliked(commentURI: String, likeCount: Int, actorDID: String) -> CommentStreamEvent { CommentStreamEvent(type: .unliked, commentUri: commentURI, likeCount: likeCount, actorDid: actorDID) }}
/// Fan-out from the comment event listener to the WebSocket handlers in/// this process. Each subscriber watches one episode subject. Streams hold/// 32 events; a subscriber that falls further behind loses the newest ones,/// and the client treats a gap as a signal to reload.////// Events travel between processes through Postgres (see/// ``CommentEventPublisher`` and ``CommentEventListener``), because the/// process that indexes a comment is rarely the one holding the stream/// open for it. The listener is started with the first subscriber and/// stopped with the last, so instances without open streams hold no/// listening connection.actor CommentEventBus { typealias Listener = @Sendable (CommentEventBus) async -> Void
struct Subscription: Sendable { let id: UUID let events: AsyncStream<CommentStreamEvent> }
private let listener: Listener? private var listenerTask: Task<Void, Never>? private var subscribers: [String: [UUID: AsyncStream<CommentStreamEvent>.Continuation]] = [:]
/// - Parameter listener: runs while there are subscribers and delivers /// events with ``broadcast(subject:_:)``; nil for a bus fed directly. init(listener: Listener? = nil) { self.listener = listener }
func subscribe(subject: String) -> Subscription { let id = UUID() let (stream, continuation) = AsyncStream<CommentStreamEvent>.makeStream(bufferingPolicy: .bufferingOldest(32)) subscribers[subject, default: [:]][id] = continuation if listenerTask == nil, let listener { listenerTask = Task { await listener(self) } } return Subscription(id: id, events: stream) }
func unsubscribe(subject: String, id: UUID) { guard var subs = subscribers[subject] else { return } subs[id]?.finish() subs[id] = nil subscribers[subject] = subs.isEmpty ? nil : subs if subscribers.isEmpty { listenerTask?.cancel() listenerTask = nil } }
func broadcast(subject: String, _ event: CommentStreamEvent) { guard !subject.isEmpty, let subs = subscribers[subject] else { return } for continuation in subs.values { continuation.yield(event) } }
var subscriberCount: Int { subscribers.values.reduce(0) { $0 + $1.count } }
var isListening: Bool { listenerTask != nil }}