ably-labs / ably-chat-swift

Ably Chat SDK for Swift to build chat experiences at scale
Apache License 2.0
0 stars 1 forks source link

Consider using of "broadcast" operator for async sequnce for multiple iterations over one sequnce #44

Open maratal opened 1 month ago

maratal commented 1 month ago
          You can't reuse the same `MockMessageSubscription`. It wraps an `AsyncStream`, which is only designed to be iterated over once (I think that the terminology I’ve seen used is that it’s "unicast"). This means that a user who calls `messages.subscribe(bufferingPolicy:)` twice and then iterates over the returned sequences will see that each iteration does not receive all of the messages. (To see this in a more concrete fashion, change your `AblyChatExampleApp` to include two `ContentView` instances instead of one, and you’ll see that when you send a message it only appears in one of the content views.)
          I’m not yet sure what the best way to solve this is — my first thought was to say to just create a new `MockMessageSubscription` each time `messages.subscribe(bufferingPolicy:)` is called, but this wouldn't be right because the timers wouldn't be in sync and the random elements would be different. The ideal thing would be to use some sort of "broadcast" operator (see e.g. [this swift-async-algorithms discussion](https://forums.swift.org/t/swift-async-algorithms-proposal-broadcast-previously-shared/61210)) to allow multiple iterations over a single subscription. Will think about it a bit more, keen to hear your thoughts too.
          Ditto for the other subscriptions (reactions etc).

_Originally posted by @lawrence-forooghian in https://github.com/ably-labs/ably-chat-swift/pull/34#discussion_r1751039672_