Skip to content

Commit 95e249f

Browse files
authored
fix(realtime): add missing onPostgresChange overload (#528)
1 parent 5476190 commit 95e249f

File tree

4 files changed

+125
-3
lines changed

4 files changed

+125
-3
lines changed

Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ let package = Package(
120120
name: "RealtimeTests",
121121
dependencies: [
122122
.product(name: "CustomDump", package: "swift-custom-dump"),
123+
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
124+
.product(name: "InlineSnapshotTesting", package: "swift-snapshot-testing"),
123125
"PostgREST",
124126
"Realtime",
125127
"TestHelpers",

Sources/Realtime/PhoenixTransport.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ open class URLSessionTransport: NSObject, PhoenixTransport, URLSessionWebSocketD
200200
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
201201
var request = URLRequest(url: url)
202202

203-
headers.forEach { (key: String, value: Any) in
204-
guard let value = value as? String else { return }
203+
for (key, value) in headers {
204+
guard let value = value as? String else { continue }
205205
request.addValue(value, forHTTPHeaderField: key)
206206
}
207207

Sources/Realtime/V2/RealtimeChannelV2.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public final class RealtimeChannelV2: Sendable {
8686
let logger: (any SupabaseLogger)?
8787
let socket: Socket
8888

89-
private let callbackManager = CallbackManager()
89+
let callbackManager = CallbackManager()
9090
private let statusEventEmitter = EventEmitter<Status>(initialEvent: .unsubscribed)
9191

9292
public private(set) var status: Status {
@@ -472,6 +472,24 @@ public final class RealtimeChannelV2: Sendable {
472472
}
473473
}
474474

475+
/// Listen for postgres changes in a channel.
476+
public func onPostgresChange(
477+
_: AnyAction.Type,
478+
schema: String = "public",
479+
table: String? = nil,
480+
filter: String? = nil,
481+
callback: @escaping @Sendable (AnyAction) -> Void
482+
) -> Subscription {
483+
_onPostgresChange(
484+
event: .all,
485+
schema: schema,
486+
table: table,
487+
filter: filter
488+
) {
489+
callback($0)
490+
}
491+
}
492+
475493
/// Listen for postgres changes in a channel.
476494
public func onPostgresChange(
477495
_: InsertAction.Type,
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
//
2+
// RealtimeChannelTests.swift
3+
// Supabase
4+
//
5+
// Created by Guilherme Souza on 09/09/24.
6+
//
7+
8+
import InlineSnapshotTesting
9+
@testable import Realtime
10+
import XCTest
11+
import XCTestDynamicOverlay
12+
13+
final class RealtimeChannelTests: XCTestCase {
14+
var sut: RealtimeChannelV2!
15+
16+
func testOnPostgresChange() {
17+
sut = RealtimeChannelV2(
18+
topic: "topic",
19+
config: RealtimeChannelConfig(
20+
broadcast: BroadcastJoinConfig(),
21+
presence: PresenceJoinConfig(),
22+
isPrivate: false
23+
),
24+
socket: .mock,
25+
logger: nil
26+
)
27+
var subscriptions = Set<RealtimeChannelV2.Subscription>()
28+
sut.onPostgresChange(AnyAction.self) { _ in }.store(in: &subscriptions)
29+
sut.onPostgresChange(InsertAction.self) { _ in }.store(in: &subscriptions)
30+
sut.onPostgresChange(UpdateAction.self) { _ in }.store(in: &subscriptions)
31+
sut.onPostgresChange(DeleteAction.self) { _ in }.store(in: &subscriptions)
32+
33+
assertInlineSnapshot(of: sut.callbackManager.callbacks, as: .dump) {
34+
"""
35+
▿ 4 elements
36+
▿ RealtimeCallback
37+
▿ postgres: PostgresCallback
38+
- callback: (Function)
39+
▿ filter: PostgresJoinConfig
40+
▿ event: Optional<PostgresChangeEvent>
41+
- some: PostgresChangeEvent.all
42+
- filter: Optional<String>.none
43+
- id: 0
44+
- schema: "public"
45+
- table: Optional<String>.none
46+
- id: 1
47+
▿ RealtimeCallback
48+
▿ postgres: PostgresCallback
49+
- callback: (Function)
50+
▿ filter: PostgresJoinConfig
51+
▿ event: Optional<PostgresChangeEvent>
52+
- some: PostgresChangeEvent.insert
53+
- filter: Optional<String>.none
54+
- id: 0
55+
- schema: "public"
56+
- table: Optional<String>.none
57+
- id: 2
58+
▿ RealtimeCallback
59+
▿ postgres: PostgresCallback
60+
- callback: (Function)
61+
▿ filter: PostgresJoinConfig
62+
▿ event: Optional<PostgresChangeEvent>
63+
- some: PostgresChangeEvent.update
64+
- filter: Optional<String>.none
65+
- id: 0
66+
- schema: "public"
67+
- table: Optional<String>.none
68+
- id: 3
69+
▿ RealtimeCallback
70+
▿ postgres: PostgresCallback
71+
- callback: (Function)
72+
▿ filter: PostgresJoinConfig
73+
▿ event: Optional<PostgresChangeEvent>
74+
- some: PostgresChangeEvent.delete
75+
- filter: Optional<String>.none
76+
- id: 0
77+
- schema: "public"
78+
- table: Optional<String>.none
79+
- id: 4
80+
81+
"""
82+
}
83+
}
84+
}
85+
86+
extension Socket {
87+
static var mock: Socket {
88+
Socket(
89+
broadcastURL: unimplemented(),
90+
status: unimplemented(),
91+
options: unimplemented(),
92+
accessToken: unimplemented(),
93+
apiKey: unimplemented(),
94+
makeRef: unimplemented(),
95+
connect: unimplemented(),
96+
addChannel: unimplemented(),
97+
removeChannel: unimplemented(),
98+
push: unimplemented(),
99+
httpSend: unimplemented()
100+
)
101+
}
102+
}

0 commit comments

Comments
 (0)