Skip to content

Commit

Permalink
fix: Thread safe subscription watcher cancel (#523)
Browse files Browse the repository at this point in the history
* fix: Thread safe subscription watcher cancel

* use guard
  • Loading branch information
lawmicha authored Mar 7, 2022
1 parent 5339230 commit b052e40
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions AWSAppSyncClient/AWSAppSyncSubscriptionWatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class AWSAppSyncSubscriptionWatcher<Subscription: GraphQLSubscripti
private var subscriptionItem: SubscriptionItem?

private let handlerQueue: DispatchQueue
private let subscriptionsQueue: DispatchQueue
private var resultHandler: SubscriptionResultHandler<Subscription>?
private var connectedCallback: (() -> Void)?
private var statusChangeHandler: SubscriptionStatusChangeHandler?
Expand Down Expand Up @@ -57,6 +58,7 @@ public final class AWSAppSyncSubscriptionWatcher<Subscription: GraphQLSubscripti
resultHandler(result, transaction, error)
}
}
self.subscriptionsQueue = subscriptionsQueue
subscriptionsQueue.async { [weak self] in
guard let self = self else {return}
if !self.isCancelled {
Expand Down Expand Up @@ -177,22 +179,26 @@ public final class AWSAppSyncSubscriptionWatcher<Subscription: GraphQLSubscripti
/// Specifically, this means that cancelling a subscription watcher will not invoke `statusChangeHandler` or
/// `resultHandler`, although it will set the internal state of the watcher to `.disconnected`
public func cancel() {
guard !self.isCancelled else { return }

if self.cancellationSource == .none {
self.cancellationSource = .user
}
performCleanUpTasksOnCancel()
self.performCleanUpTasksOnCancel()
}

internal func performCleanUpTasksOnCancel() {
isCancelled = true
status = .disconnected
resultHandler = nil
statusChangeHandler = nil

if self.cancellationSource != .error, let item = subscriptionItem {
connection?.unsubscribe(item: item)
subscriptionsQueue.async { [weak self] in
guard let self = self else {return}
if self.cancellationSource != .error, let item = self.subscriptionItem {
self.connection?.unsubscribe(item: item)
}
self.connection = nil
}
connection = nil
}

}

0 comments on commit b052e40

Please sign in to comment.