Skip to content

fix(realtime): add RealtimeSubscription and deprecate Subscription #542

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

Merged
merged 1 commit into from
Sep 30, 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
9 changes: 9 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 11 additions & 10 deletions Sources/Realtime/V2/RealtimeChannelV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ extension Socket {
}

public final class RealtimeChannelV2: Sendable {
@available(*, deprecated, renamed: "RealtimeSubscription")
public typealias Subscription = ObservationToken

public enum Status: Sendable {
Expand Down Expand Up @@ -464,9 +465,9 @@ public final class RealtimeChannelV2: Sendable {
/// Listen for clients joining / leaving the channel using presences.
public func onPresenceChange(
_ callback: @escaping @Sendable (any PresenceAction) -> Void
) -> Subscription {
) -> RealtimeSubscription {
let id = callbackManager.addPresenceCallback(callback: callback)
return Subscription { [weak callbackManager, logger] in
return RealtimeSubscription { [weak callbackManager, logger] in
logger?.debug("Removing presence callback with id: \(id)")
callbackManager?.removeCallback(id: id)
}
Expand All @@ -479,7 +480,7 @@ public final class RealtimeChannelV2: Sendable {
table: String? = nil,
filter: String? = nil,
callback: @escaping @Sendable (AnyAction) -> Void
) -> Subscription {
) -> RealtimeSubscription {
_onPostgresChange(
event: .all,
schema: schema,
Expand All @@ -497,7 +498,7 @@ public final class RealtimeChannelV2: Sendable {
table: String? = nil,
filter: String? = nil,
callback: @escaping @Sendable (InsertAction) -> Void
) -> Subscription {
) -> RealtimeSubscription {
_onPostgresChange(
event: .insert,
schema: schema,
Expand All @@ -516,7 +517,7 @@ public final class RealtimeChannelV2: Sendable {
table: String? = nil,
filter: String? = nil,
callback: @escaping @Sendable (UpdateAction) -> Void
) -> Subscription {
) -> RealtimeSubscription {
_onPostgresChange(
event: .update,
schema: schema,
Expand All @@ -535,7 +536,7 @@ public final class RealtimeChannelV2: Sendable {
table: String? = nil,
filter: String? = nil,
callback: @escaping @Sendable (DeleteAction) -> Void
) -> Subscription {
) -> RealtimeSubscription {
_onPostgresChange(
event: .delete,
schema: schema,
Expand All @@ -553,7 +554,7 @@ public final class RealtimeChannelV2: Sendable {
table: String?,
filter: String?,
callback: @escaping @Sendable (AnyAction) -> Void
) -> Subscription {
) -> RealtimeSubscription {
precondition(
status != .subscribed,
"You cannot call postgresChange after joining the channel"
Expand All @@ -571,7 +572,7 @@ public final class RealtimeChannelV2: Sendable {
}

let id = callbackManager.addPostgresCallback(filter: config, callback: callback)
return Subscription { [weak callbackManager, logger] in
return RealtimeSubscription { [weak callbackManager, logger] in
logger?.debug("Removing postgres callback with id: \(id)")
callbackManager?.removeCallback(id: id)
}
Expand All @@ -581,9 +582,9 @@ public final class RealtimeChannelV2: Sendable {
public func onBroadcast(
event: String,
callback: @escaping @Sendable (JSONObject) -> Void
) -> Subscription {
) -> RealtimeSubscription {
let id = callbackManager.addBroadcastCallback(event: event, callback: callback)
return Subscription { [weak callbackManager, logger] in
return RealtimeSubscription { [weak callbackManager, logger] in
logger?.debug("Removing broadcast callback with id: \(id)")
callbackManager?.removeCallback(id: id)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Realtime/V2/RealtimeClientV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public final class RealtimeClientV2: Sendable {
/// - Note: Use ``statusChange`` if you prefer to use Async/Await.
public func onStatusChange(
_ listener: @escaping @Sendable (Status) -> Void
) -> ObservationToken {
) -> RealtimeSubscription {
statusEventEmitter.attach(listener)
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/Realtime/V2/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ public struct RealtimeClientOptions: Sendable {
return String(accessToken)
}
}

public typealias RealtimeSubscription = ObservationToken
Loading