diff --git a/Sources/EventSource/EventParser.swift b/Sources/EventSource/EventParser.swift index 3226c02..a21b757 100644 --- a/Sources/EventSource/EventParser.swift +++ b/Sources/EventSource/EventParser.swift @@ -10,14 +10,14 @@ import Foundation /// Event parser is used to parse text data into ``ServerEvent``. public struct EventParser: Sendable { - public var parse: @Sendable (_ data: Data) -> [ServerEvent] + public var parse: @Sendable (_ data: Data) -> [EVEvent] } public extension EventParser { static let lf: UInt8 = 0x0A static let colon: UInt8 = 0x3A - nonisolated(unsafe) static let live = Self(parse: { data in + static let live = Self(parse: { data in // Split message with double newline let rawMessages: [Data] if #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, visionOS 1.0, *) { diff --git a/Sources/EventSource/EventSource.swift b/Sources/EventSource/EventSource.swift index 62f787b..1e39e70 100644 --- a/Sources/EventSource/EventSource.swift +++ b/Sources/EventSource/EventSource.swift @@ -28,7 +28,7 @@ public struct EventSource { /// Event type. public enum EventType { case error(Error) - case event(ServerEvent) + case event(EVEvent) case open case closed } diff --git a/Sources/EventSource/ServerEvent.swift b/Sources/EventSource/ServerEvent.swift index a78b3ae..90b3bf5 100644 --- a/Sources/EventSource/ServerEvent.swift +++ b/Sources/EventSource/ServerEvent.swift @@ -8,7 +8,45 @@ import Foundation -public struct ServerEvent { +/// Protocol for defining a basic event structure. It is used by the ``EventParser`` +/// and should be implemented as a custom type when a custom ``EventParser`` is required. +public protocol EVEvent { + var id: String? { get } + var event: String? { get } + var data: String? { get } + var other: [String: String]? { get } + var time: String? { get } +} + +public extension EVEvent { + /// Checks if all event fields are empty. + var isEmpty: Bool { + if let id, !id.isEmpty { + return false + } + + if let event, !event.isEmpty { + return false + } + + if let data, !data.isEmpty { + return false + } + + if let other, !other.isEmpty { + return false + } + + if let time, !time.isEmpty { + return false + } + + return true + } +} + +/// Default implementation of ``EventSourceEvent`` used in the package. +public struct ServerEvent: EVEvent { public var id: String? public var event: String? public var data: String? @@ -29,30 +67,6 @@ public struct ServerEvent { self.time = time } - private func isEmpty() -> Bool { - if let id, !id.isEmpty { - return false - } - - if let event, !event.isEmpty { - return false - } - - if let data, !data.isEmpty { - return false - } - - if let other, !other.isEmpty { - return false - } - - if let time, !time.isEmpty { - return false - } - - return true - } - public static func parse(from data: Data) -> ServerEvent? { let rows = data.split(separator: EventParser.lf) // Separate message fields @@ -97,7 +111,7 @@ public struct ServerEvent { } } - if message.isEmpty() { + if message.isEmpty { return nil }