Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(realtime): add missing onPostgresChange overload #528

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ let package = Package(
name: "RealtimeTests",
dependencies: [
.product(name: "CustomDump", package: "swift-custom-dump"),
.product(name: "XCTestDynamicOverlay", package: "xctest-dynamic-overlay"),
.product(name: "InlineSnapshotTesting", package: "swift-snapshot-testing"),
"PostgREST",
"Realtime",
"TestHelpers",
Expand Down
4 changes: 2 additions & 2 deletions Sources/Realtime/PhoenixTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ open class URLSessionTransport: NSObject, PhoenixTransport, URLSessionWebSocketD
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
var request = URLRequest(url: url)

headers.forEach { (key: String, value: Any) in
guard let value = value as? String else { return }
for (key, value) in headers {
guard let value = value as? String else { continue }
request.addValue(value, forHTTPHeaderField: key)
}

Expand Down
20 changes: 19 additions & 1 deletion Sources/Realtime/V2/RealtimeChannelV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public final class RealtimeChannelV2: Sendable {
let logger: (any SupabaseLogger)?
let socket: Socket

private let callbackManager = CallbackManager()
let callbackManager = CallbackManager()
private let statusEventEmitter = EventEmitter<Status>(initialEvent: .unsubscribed)

public private(set) var status: Status {
Expand Down Expand Up @@ -472,6 +472,24 @@ public final class RealtimeChannelV2: Sendable {
}
}

/// Listen for postgres changes in a channel.
public func onPostgresChange(
_: AnyAction.Type,
schema: String = "public",
table: String? = nil,
filter: String? = nil,
callback: @escaping @Sendable (AnyAction) -> Void
) -> Subscription {
_onPostgresChange(
event: .all,
schema: schema,
table: table,
filter: filter
) {
callback($0)
}
}

/// Listen for postgres changes in a channel.
public func onPostgresChange(
_: InsertAction.Type,
Expand Down
102 changes: 102 additions & 0 deletions Tests/RealtimeTests/RealtimeChannelTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// RealtimeChannelTests.swift
// Supabase
//
// Created by Guilherme Souza on 09/09/24.
//

import InlineSnapshotTesting
@testable import Realtime
import XCTest
import XCTestDynamicOverlay

final class RealtimeChannelTests: XCTestCase {
var sut: RealtimeChannelV2!

func testOnPostgresChange() {
sut = RealtimeChannelV2(
topic: "topic",
config: RealtimeChannelConfig(
broadcast: BroadcastJoinConfig(),
presence: PresenceJoinConfig(),
isPrivate: false
),
socket: .mock,
logger: nil
)
var subscriptions = Set<RealtimeChannelV2.Subscription>()
sut.onPostgresChange(AnyAction.self) { _ in }.store(in: &subscriptions)
sut.onPostgresChange(InsertAction.self) { _ in }.store(in: &subscriptions)
sut.onPostgresChange(UpdateAction.self) { _ in }.store(in: &subscriptions)
sut.onPostgresChange(DeleteAction.self) { _ in }.store(in: &subscriptions)

assertInlineSnapshot(of: sut.callbackManager.callbacks, as: .dump) {
"""
▿ 4 elements
▿ RealtimeCallback
▿ postgres: PostgresCallback
- callback: (Function)
▿ filter: PostgresJoinConfig
▿ event: Optional<PostgresChangeEvent>
- some: PostgresChangeEvent.all
- filter: Optional<String>.none
- id: 0
- schema: "public"
- table: Optional<String>.none
- id: 1
▿ RealtimeCallback
▿ postgres: PostgresCallback
- callback: (Function)
▿ filter: PostgresJoinConfig
▿ event: Optional<PostgresChangeEvent>
- some: PostgresChangeEvent.insert
- filter: Optional<String>.none
- id: 0
- schema: "public"
- table: Optional<String>.none
- id: 2
▿ RealtimeCallback
▿ postgres: PostgresCallback
- callback: (Function)
▿ filter: PostgresJoinConfig
▿ event: Optional<PostgresChangeEvent>
- some: PostgresChangeEvent.update
- filter: Optional<String>.none
- id: 0
- schema: "public"
- table: Optional<String>.none
- id: 3
▿ RealtimeCallback
▿ postgres: PostgresCallback
- callback: (Function)
▿ filter: PostgresJoinConfig
▿ event: Optional<PostgresChangeEvent>
- some: PostgresChangeEvent.delete
- filter: Optional<String>.none
- id: 0
- schema: "public"
- table: Optional<String>.none
- id: 4

"""
}
}
}

extension Socket {
static var mock: Socket {
Socket(
broadcastURL: unimplemented(),
status: unimplemented(),
options: unimplemented(),
accessToken: unimplemented(),
apiKey: unimplemented(),
makeRef: unimplemented(),
connect: unimplemented(),
addChannel: unimplemented(),
removeChannel: unimplemented(),
push: unimplemented(),
httpSend: unimplemented()
)
}
}
Loading