Skip to content

Commit

Permalink
Simplify subscription disposal
Browse files Browse the repository at this point in the history
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)."
  • Loading branch information
jscheid committed Jun 15, 2020
1 parent 35e55dc commit 03f111f
Showing 1 changed file with 2 additions and 23 deletions.
25 changes: 2 additions & 23 deletions javascript_client/src/subscriptions/createAblyHandler.ts
Expand Up @@ -68,7 +68,6 @@ function createAblyHandler(options: AblyHandlerOptions) {
observer.onCompleted()
}
}

;(async () => {
try {
// POST the subscription like a normal query
Expand Down Expand Up @@ -134,28 +133,9 @@ function createAblyHandler(options: AblyHandlerOptions) {
try {
if (channel) {
const disposedChannel = channel
disposedChannel.unsubscribe("update", updateHandler)

const leavePromise = new Promise((resolve, reject) => {
const callback = (err: Types.ErrorInfo) => {
if (err) {
reject(new AblyError(err))
} else {
resolve()
}
}

if (isAnonymousClient()) {
disposedChannel.presence.leaveClient(
anonymousClientId,
callback
)
} else {
disposedChannel.presence.leave(callback)
}
})
disposedChannel.unsubscribe()

const detachPromise = new Promise((resolve, reject) => {
await new Promise((resolve, reject) => {
disposedChannel.detach((err: Types.ErrorInfo) => {
if (err) {
reject(new AblyError(err))
Expand All @@ -165,7 +145,6 @@ function createAblyHandler(options: AblyHandlerOptions) {
})
})

await Promise.all([leavePromise, detachPromise])
ably.channels.release(disposedChannel.name)
}
} catch (error) {
Expand Down

0 comments on commit 03f111f

Please sign in to comment.