Skip to content

Commit

Permalink
Rename v2 types
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Dec 29, 2023
1 parent 20534e0 commit 1838f09
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 185 deletions.
2 changes: 1 addition & 1 deletion Examples/SlackClone/MessagesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class MessagesViewModel {
}
}

private var realtimeChannelV2: RealtimeChannel?
private var realtimeChannelV2: RealtimeChannelV2?
private var observationTask: Task<Void, Never>?

func startObservingNewMessages() {
Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
"version" : "1.1.0"
}
},
{
"identity" : "swift-custom-dump",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-custom-dump",
"state" : {
"revision" : "aedcf6f4cd486ccef5b312ccac85d4b3f6e58605",
"version" : "1.1.2"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
Expand Down
8 changes: 4 additions & 4 deletions Sources/Realtime/CallbackManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class CallbackManager: @unchecked Sendable {
@discardableResult
func addBroadcastCallback(
event: String,
callback: @escaping @Sendable (_RealtimeMessage) -> Void
callback: @escaping @Sendable (RealtimeMessageV2) -> Void
) -> Int {
mutableState.withValue {
$0.id += 1
Expand Down Expand Up @@ -96,7 +96,7 @@ final class CallbackManager: @unchecked Sendable {
}
}

func triggerBroadcast(event: String, message: _RealtimeMessage) {
func triggerBroadcast(event: String, message: RealtimeMessageV2) {
let broadcastCallbacks = mutableState.callbacks.compactMap {
if case let .broadcast(callback) = $0 {
return callback
Expand All @@ -110,7 +110,7 @@ final class CallbackManager: @unchecked Sendable {
func triggerPresenceDiffs(
joins: [String: Presence],
leaves: [String: Presence],
rawMessage: _RealtimeMessage
rawMessage: RealtimeMessageV2
) {
let presenceCallbacks = mutableState.callbacks.compactMap {
if case let .presence(callback) = $0 {
Expand Down Expand Up @@ -139,7 +139,7 @@ struct PostgresCallback {
struct BroadcastCallback {
var id: Int
var event: String
var callback: @Sendable (_RealtimeMessage) -> Void
var callback: @Sendable (RealtimeMessageV2) -> Void
}

struct PresenceCallback {
Expand Down
20 changes: 10 additions & 10 deletions Sources/Realtime/Channel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct RealtimeChannelConfig: Sendable {
public var presence: PresenceJoinConfig
}

public final class RealtimeChannel: @unchecked Sendable {
public final class RealtimeChannelV2: @unchecked Sendable {
public enum Status {
case unsubscribed
case subscribing
Expand Down Expand Up @@ -87,7 +87,7 @@ public final class RealtimeChannel: @unchecked Sendable {

debug("subscribing to channel with body: \(joinConfig)")

try? await socket?.send(_RealtimeMessage(
try? await socket?.send(RealtimeMessageV2(
joinRef: nil,
ref: socket?.makeRef().description ?? "",
topic: topic,
Expand All @@ -101,7 +101,7 @@ public final class RealtimeChannel: @unchecked Sendable {
debug("unsubscribing from channel \(topic)")

try await socket?.send(
_RealtimeMessage(
RealtimeMessageV2(
joinRef: nil,
ref: socket?.makeRef().description,
topic: topic,
Expand All @@ -114,7 +114,7 @@ public final class RealtimeChannel: @unchecked Sendable {
public func updateAuth(jwt: String) async throws {
debug("Updating auth token for channel \(topic)")
try await socket?.send(
_RealtimeMessage(
RealtimeMessageV2(
joinRef: nil,
ref: socket?.makeRef().description,
topic: topic,
Expand All @@ -129,7 +129,7 @@ public final class RealtimeChannel: @unchecked Sendable {
// TODO: use HTTP
} else {
try await socket?.send(
_RealtimeMessage(
RealtimeMessageV2(
joinRef: nil,
ref: socket?.makeRef().description,
topic: topic,
Expand All @@ -151,7 +151,7 @@ public final class RealtimeChannel: @unchecked Sendable {
)
}

try await socket?.send(_RealtimeMessage(
try await socket?.send(RealtimeMessageV2(
joinRef: nil,
ref: socket?.makeRef().description,
topic: topic,
Expand All @@ -165,7 +165,7 @@ public final class RealtimeChannel: @unchecked Sendable {
}

public func untrack() async throws {
try await socket?.send(_RealtimeMessage(
try await socket?.send(RealtimeMessageV2(
joinRef: nil,
ref: socket?.makeRef().description,
topic: topic,
Expand All @@ -177,7 +177,7 @@ public final class RealtimeChannel: @unchecked Sendable {
))
}

func onMessage(_ message: _RealtimeMessage) async throws {
func onMessage(_ message: RealtimeMessageV2) async throws {
guard let eventType = message.eventType else {
throw RealtimeError("Received message without event type: \(message)")
}
Expand Down Expand Up @@ -331,8 +331,8 @@ public final class RealtimeChannel: @unchecked Sendable {

/// Listen for broadcast messages sent by other clients within the same channel under a specific
/// `event`.
public func broadcast(event: String) -> AsyncStream<_RealtimeMessage> {
let (stream, continuation) = AsyncStream<_RealtimeMessage>.makeStream()
public func broadcast(event: String) -> AsyncStream<RealtimeMessageV2> {
let (stream, continuation) = AsyncStream<RealtimeMessageV2>.makeStream()

let id = callbackManager.addBroadcastCallback(event: event) {
continuation.yield($0)
Expand Down
84 changes: 0 additions & 84 deletions Sources/Realtime/Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,87 +9,3 @@ import Foundation

@available(*, deprecated, renamed: "RealtimeMessage")
public typealias Message = RealtimeMessage

extension RealtimeChannel {
@available(
*,
deprecated,
message: "Please use one of postgresChanges, presenceChange, or broadcast methods that returns an AsyncSequence instead."
)
@discardableResult
public func on(
_ event: String,
filter: ChannelFilter,
handler: @escaping (_RealtimeMessage) -> Void
) -> RealtimeChannel {
let stream: AsyncStream<HasRawMessage>

switch event.lowercased() {
case "postgres_changes":
switch filter.event?.uppercased() {
case "UPDATE":
stream = postgresChange(
UpdateAction.self,
schema: filter.schema ?? "public",
table: filter.table!,
filter: filter.filter
)
.map { $0 as HasRawMessage }
.eraseToStream()
case "INSERT":
stream = postgresChange(
InsertAction.self,
schema: filter.schema ?? "public",
table: filter.table!,
filter: filter.filter
)
.map { $0 as HasRawMessage }
.eraseToStream()
case "DELETE":
stream = postgresChange(
DeleteAction.self,
schema: filter.schema ?? "public",
table: filter.table!,
filter: filter.filter
)
.map { $0 as HasRawMessage }
.eraseToStream()
case "SELECT":
stream = postgresChange(
SelectAction.self,
schema: filter.schema ?? "public",
table: filter.table!,
filter: filter.filter
)
.map { $0 as HasRawMessage }
.eraseToStream()
default:
stream = postgresChange(
AnyAction.self,
schema: filter.schema ?? "public",
table: filter.table!,
filter: filter.filter
)
.map { $0 as HasRawMessage }
.eraseToStream()
}

case "presence":
stream = presenceChange().map { $0 as HasRawMessage }.eraseToStream()
case "broadcast":
stream = broadcast(event: filter.event!).map { $0 as HasRawMessage }.eraseToStream()
default:
fatalError(
"Unsupported event '\(event)'. Expected one of: postgres_changes, presence, or broadcast."
)
}

Task {
for await action in stream {
handler(action.rawMessage)
}
}

return self
}
}
12 changes: 6 additions & 6 deletions Sources/Realtime/PostgresAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public protocol HasOldRecord {
}

public protocol HasRawMessage {
var rawMessage: _RealtimeMessage { get }
var rawMessage: RealtimeMessageV2 { get }
}

public struct InsertAction: PostgresAction, HasRecord, HasRawMessage {
Expand All @@ -35,7 +35,7 @@ public struct InsertAction: PostgresAction, HasRecord, HasRawMessage {
public let columns: [Column]
public let commitTimestamp: Date
public let record: [String: AnyJSON]
public let rawMessage: _RealtimeMessage
public let rawMessage: RealtimeMessageV2
}

public struct UpdateAction: PostgresAction, HasRecord, HasOldRecord, HasRawMessage {
Expand All @@ -44,7 +44,7 @@ public struct UpdateAction: PostgresAction, HasRecord, HasOldRecord, HasRawMessa
public let columns: [Column]
public let commitTimestamp: Date
public let record, oldRecord: [String: AnyJSON]
public let rawMessage: _RealtimeMessage
public let rawMessage: RealtimeMessageV2
}

public struct DeleteAction: PostgresAction, HasOldRecord, HasRawMessage {
Expand All @@ -53,7 +53,7 @@ public struct DeleteAction: PostgresAction, HasOldRecord, HasRawMessage {
public let columns: [Column]
public let commitTimestamp: Date
public let oldRecord: [String: AnyJSON]
public let rawMessage: _RealtimeMessage
public let rawMessage: RealtimeMessageV2
}

public struct SelectAction: PostgresAction, HasRecord, HasRawMessage {
Expand All @@ -62,7 +62,7 @@ public struct SelectAction: PostgresAction, HasRecord, HasRawMessage {
public let columns: [Column]
public let commitTimestamp: Date
public let record: [String: AnyJSON]
public let rawMessage: _RealtimeMessage
public let rawMessage: RealtimeMessageV2
}

public enum AnyAction: PostgresAction, HasRawMessage {
Expand All @@ -82,7 +82,7 @@ public enum AnyAction: PostgresAction, HasRawMessage {
}
}

public var rawMessage: _RealtimeMessage {
public var rawMessage: RealtimeMessageV2 {
wrappedAction.rawMessage
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Realtime/Presence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public final class Presence {

// ----------------------------------------------------------------------
/// The channel the Presence belongs to
weak var channel: OldRealtimeChannel?
weak var channel: RealtimeChannel?

/// Caller to callback hooks
var caller: Caller
Expand Down Expand Up @@ -215,7 +215,7 @@ public final class Presence {
onSync = callback
}

public init(channel: OldRealtimeChannel, opts: Options = Options.defaults) {
public init(channel: RealtimeChannel, opts: Options = Options.defaults) {
state = [:]
pendingDiffs = []
self.channel = channel
Expand Down
2 changes: 1 addition & 1 deletion Sources/Realtime/PresenceAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ public protocol PresenceAction: HasRawMessage {
struct PresenceActionImpl: PresenceAction {
var joins: [String: Presence]
var leaves: [String: Presence]
var rawMessage: _RealtimeMessage
var rawMessage: RealtimeMessageV2
}
4 changes: 2 additions & 2 deletions Sources/Realtime/Push.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Foundation
/// Represnts pushing data to a `Channel` through the `Socket`
public class Push {
/// The channel sending the Push
public weak var channel: OldRealtimeChannel?
public weak var channel: RealtimeChannel?

/// The event, for example `phx_join`
public let event: String
Expand Down Expand Up @@ -62,7 +62,7 @@ public class Push {
/// - parameter payload: Optional. The Payload to send, e.g. ["user_id": "abc123"]
/// - parameter timeout: Optional. The push timeout. Default is 10.0s
init(
channel: OldRealtimeChannel,
channel: RealtimeChannel,
event: String,
payload: Payload = [:],
timeout: TimeInterval = Defaults.timeoutInterval
Expand Down
Loading

0 comments on commit 1838f09

Please sign in to comment.