Skip to content

Commit 03f111f

Browse files
committed
Simplify subscription disposal
unsubscribe without arguments "[u]nsubscribes all listeners to messages on this channel. This removes all earlier subscriptions." Also, Ably staff explained to me that "[...] you don't need to call leave() if calling detach(): the server enforces the invariant that a client cannot be present on a channel they are not attached to, so detach() will also cause that client to leave the presence set)."
1 parent 35e55dc commit 03f111f

1 file changed

Lines changed: 2 additions & 23 deletions

File tree

javascript_client/src/subscriptions/createAblyHandler.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ function createAblyHandler(options: AblyHandlerOptions) {
6868
observer.onCompleted()
6969
}
7070
}
71-
7271
;(async () => {
7372
try {
7473
// POST the subscription like a normal query
@@ -134,28 +133,9 @@ function createAblyHandler(options: AblyHandlerOptions) {
134133
try {
135134
if (channel) {
136135
const disposedChannel = channel
137-
disposedChannel.unsubscribe("update", updateHandler)
138-
139-
const leavePromise = new Promise((resolve, reject) => {
140-
const callback = (err: Types.ErrorInfo) => {
141-
if (err) {
142-
reject(new AblyError(err))
143-
} else {
144-
resolve()
145-
}
146-
}
147-
148-
if (isAnonymousClient()) {
149-
disposedChannel.presence.leaveClient(
150-
anonymousClientId,
151-
callback
152-
)
153-
} else {
154-
disposedChannel.presence.leave(callback)
155-
}
156-
})
136+
disposedChannel.unsubscribe()
157137

158-
const detachPromise = new Promise((resolve, reject) => {
138+
await new Promise((resolve, reject) => {
159139
disposedChannel.detach((err: Types.ErrorInfo) => {
160140
if (err) {
161141
reject(new AblyError(err))
@@ -165,7 +145,6 @@ function createAblyHandler(options: AblyHandlerOptions) {
165145
})
166146
})
167147

168-
await Promise.all([leavePromise, detachPromise])
169148
ably.channels.release(disposedChannel.name)
170149
}
171150
} catch (error) {

0 commit comments

Comments
 (0)