// // JetstreamDelivery.swift // JetstreamKit // import CoreATProtocol /// Which events a ``JetstreamClient`` asks for. An empty list means no /// restriction, which on a v2 host includes every account's `identity`, /// `account` and `sync` events, so set `kinds` when only commits matter. public struct JetstreamFilter: Sendable, Equatable { /// Exact NSIDs or `prefix.*` patterns, at most 100. These constrain commits only. public var collections: [String] /// At most 10,000. public var dids: [String] /// Applied by v2 hosts. Legacy hosts deliver commits only, whatever this says. public var kinds: [JetstreamSubscription.Kind] public init(collections: [String] = [], dids: [String] = [], kinds: [JetstreamSubscription.Kind] = []) { self.collections = collections self.dids = dids self.kinds = kinds } } /// Where a consumer is in the stream: a sequence number on the host that /// issued it, and a witnessed time that resumes anywhere. public struct JetstreamPosition: Sendable, Equatable { /// The ``JetstreamHost/url`` of the host that delivered the event. public let host: String /// The host's sequence number. Only that host can resume from it, and /// legacy hosts have none. public let seq: Int64? /// When the host witnessed the event, in unix microseconds. public let timeMicros: Int64 public init(host: String, seq: Int64?, timeMicros: Int64) { self.host = host self.seq = seq self.timeMicros = timeMicros } } /// One thing a ``JetstreamClient`` has to tell its consumer. public enum JetstreamDelivery: Sendable { /// A socket opened. `resumedFromCursor` is false on the live tail. case connected(host: String, resumedFromCursor: Bool) /// An event, and the position to save once it has been handled. `isLive` /// is false only for a historical replay from a v1 index. case event(JetstreamEvent, position: JetstreamPosition, isLive: Bool) /// An advisory such as `FutureCursor`. `OutdatedCursor` arrives once the /// client has reconnected live, whether the host sent that frame or /// refused the cursor outright, and means saved positions are worthless: /// clear them. Events in the gap will not be delivered. case info(JetstreamInfo) /// The socket is gone. The client tries again after `willRetryAfter`. case disconnected(host: String, reason: JetstreamDisconnectReason, willRetryAfter: Duration) } /// Why a connection ended. public enum JetstreamDisconnectReason: Sendable { /// The socket could not be opened: the host is unreachable or refused the upgrade. case connectFailed(any Error) /// An open socket failed. case connectionLost(any Error) /// The server closed the socket without an error. case closedByServer /// The server ended the stream with a terminal frame such as `ConsumerTooSlow`. case streamError(JetstreamStreamError) /// The host replaced a cursor older than its retention, so the client is /// reconnecting on the live tail instead of replaying from the floor. case cursorOutdated }