Skip to content

Commit

Permalink
fix(realtime): add RealtimeSubscription and deprecate Subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Sep 27, 2024
1 parent d9ba673 commit 9a0f67d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"pins" : [
{
"identity" : "multipartformdata",
"kind" : "remoteSourceControl",
"location" : "https://github.com/grdsdev/MultipartFormData",
"state" : {
"revision" : "ed7abea9cfc6c3b5e77a73fe6842c57a372d2017",
"version" : "0.1.0"
}
},
{
"identity" : "swift-concurrency-extras",
"kind" : "remoteSourceControl",
Expand Down
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: "RealtimeRealtimeSubscription")
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

0 comments on commit 9a0f67d

Please sign in to comment.