-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hook in to AsyncStream’s termination notification mechanism, so that when the user discards a subscription or cancels the task in which they’re iterating over a subscription, we: - remove this subscription from our internal data structures - remove any corresponding ably-cocoa listeners that drive this subscription I’m sure that there will turn out to be a bunch of wrong stuff that I’ve done here, due to my still-shaky knowledge of concurrency stuff and AsyncSequence best practices, but it’s a start. Resolves #36.
- Loading branch information
1 parent
432fa11
commit 2554e1a
Showing
22 changed files
with
348 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Example/AblyChatExample/Mocks/MockSubscriptionStorage.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import Foundation | ||
|
||
// This is copied from ably-chat’s internal class `SubscriptionStorage`. | ||
class MockSubscriptionStorage<Element: Sendable>: @unchecked Sendable { | ||
// We hold a weak reference to the subscriptions that we create, so that the subscriptions’ termination handlers get called when the user releases their final reference to the subscription. | ||
private struct WeaklyHeldSubscription { | ||
weak var subscription: MockSubscription<Element>? | ||
} | ||
|
||
/// Access must be synchronised via ``lock``. | ||
private var subscriptions: [UUID: WeaklyHeldSubscription] = [:] | ||
private let lock = NSLock() | ||
|
||
// You must not call the `setOnTermination` method of a subscription returned by this function, as it will replace the termination handler set by this function. | ||
func create(randomElement: @escaping @Sendable () -> Element, interval: Double) -> MockSubscription<Element> { | ||
let subscription = MockSubscription<Element>(randomElement: randomElement, interval: interval) | ||
let id = UUID() | ||
|
||
lock.lock() | ||
subscriptions[id] = .init(subscription: subscription) | ||
lock.unlock() | ||
|
||
subscription.setOnTermination { [weak self] in | ||
self?.subscriptionDidTerminate(id: id) | ||
} | ||
|
||
return subscription | ||
} | ||
|
||
private func subscriptionDidTerminate(id: UUID) { | ||
lock.lock() | ||
subscriptions.removeValue(forKey: id) | ||
lock.unlock() | ||
} | ||
|
||
func emit(_ element: Element) { | ||
for subscription in subscriptions.values { | ||
subscription.subscription?.emit(element) | ||
} | ||
} | ||
} |
Oops, something went wrong.