Skip to content

Commit

Permalink
Introduced EVEvent protocol and allowed custom events implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Recouse committed Nov 17, 2024
1 parent 1b623ad commit 74070d9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Sources/EventSource/EventParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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, *) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/EventSource/EventSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
66 changes: 40 additions & 26 deletions Sources/EventSource/ServerEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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

Expand Down Expand Up @@ -97,7 +111,7 @@ public struct ServerEvent {
}
}

if message.isEmpty() {
if message.isEmpty {
return nil
}

Expand Down

0 comments on commit 74070d9

Please sign in to comment.