Skip to content

Commit 4ef61bb

Browse files
author
Guilherme Souza
committed
feat(realtime): add callback for handling system events
1 parent 70158cc commit 4ef61bb

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Sources/Realtime/RealtimeChannel+AsyncAwait.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ extension RealtimeChannelV2 {
104104

105105
return stream
106106
}
107+
108+
/// Listen for `system` event.
109+
public func system() -> AsyncStream<RealtimeMessageV2> {
110+
let (stream, continuation) = AsyncStream<RealtimeMessageV2>.makeStream()
111+
112+
let subscription = onSystem {
113+
continuation.yield($0)
114+
}
115+
116+
continuation.onTermination = { _ in
117+
subscription.cancel()
118+
}
119+
120+
return stream
121+
}
107122

108123
/// Listen for broadcast messages sent by other clients within the same channel under a specific `event`.
109124
@available(*, deprecated, renamed: "broadcastStream(event:)")

Sources/Realtime/V2/CallbackManager.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ final class CallbackManager: Sendable {
7575
}
7676
}
7777

78+
@discardableResult
79+
func addSystemCallback(callback: @escaping @Sendable (RealtimeMessageV2) -> Void) -> Int {
80+
mutableState.withValue {
81+
$0.id += 1
82+
$0.callbacks.append(.system(SystemCallback(id: $0.id, callback: callback)))
83+
return $0.id
84+
}
85+
}
86+
7887
func setServerChanges(changes: [PostgresJoinConfig]) {
7988
mutableState.withValue {
8089
$0.serverChanges = changes
@@ -167,16 +176,23 @@ struct PresenceCallback {
167176
var callback: @Sendable (any PresenceAction) -> Void
168177
}
169178

179+
struct SystemCallback {
180+
var id: Int
181+
var callback: @Sendable (RealtimeMessageV2) -> Void
182+
}
183+
170184
enum RealtimeCallback {
171185
case postgres(PostgresCallback)
172186
case broadcast(BroadcastCallback)
173187
case presence(PresenceCallback)
188+
case system(SystemCallback)
174189

175190
var id: Int {
176191
switch self {
177192
case let .postgres(callback): callback.id
178193
case let .broadcast(callback): callback.id
179194
case let .presence(callback): callback.id
195+
case let .system(callback): callback.id
180196
}
181197
}
182198
}

Sources/Realtime/V2/RealtimeChannelV2.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,14 @@ public final class RealtimeChannelV2: Sendable {
545545
}
546546
}
547547

548+
public func onSystem(callback: @escaping @Sendable (RealtimeMessageV2) -> Void) -> Subscription {
549+
let id = callbackManager.addSystemCallback(callback: callback)
550+
return Subscription { [weak callbackManager, logger] in
551+
logger?.debug("Removing system callback with id: \(id)")
552+
callbackManager?.removeCallback(id: id)
553+
}
554+
}
555+
548556
@discardableResult
549557
func push(_ event: String, ref: String? = nil, payload: JSONObject = [:]) async -> PushStatus {
550558
let push = mutableState.withValue {

0 commit comments

Comments
 (0)