belozierov / SwiftCoroutine

Swift coroutines for iOS, macOS and Linux.
https://belozierov.github.io/SwiftCoroutine
MIT License
836 stars 51 forks source link

Can a `CoChannel` have multiple receivers? #38

Closed funct7 closed 3 years ago

funct7 commented 3 years ago

I have two objects that call makeIterator() on the same instance of a CoChannel, and only the first caller is receiving values:

class SomeClass {
    func observe(channel: CoChannel<SomeData?>) {
        scheduler.startCoroutine(in: scope) { [weak self] in
            for item in channel.makeIterator() {
                self?.item = item
            }
        }
    }
}

let obj1 = SomeClass()
let obj2 = SomeClass()

obj1.observe(channel: someChannel) // this one receives
obj2.observe(channel: someChannel) // this one doesn't

It seems like all the methods on a CoChannel only allow consuming the value (remove and return), and I'm guessing that may be the reason the second observer never receives anything.

Is it possible to just peek, and therefore allow have multiple observers, like Observable of Rx or SharedFlow of Kotlin coroutine?

belozierov commented 3 years ago

Hi @funct7 Unfortunately no. Under the hood FIFO queue is used to manage receivers, and new value is received by first registered "listener" (in iteration every new loop is a new "listener"). You can have several iterators for CoChannel and if first iterator is "busy" with processing received value, then second iterator will receive the new value. CoChannel is thread safe and you can be sure that different iterators for the same channel will not receive the same value. It make possible to perform different "read/receive" operations with channel in parallel.

Your idea is the complete opposite 😀 I think you are looking something similar to Kotlin's BroadcastChannel. I wanted to implement that but now I don't see any sense to add new features to this library (except bug fixes) because of Apple's async/await release.

I can only give you advice to create you own wrapper around CoChannel (if you really need it) and implement it by yourself 🙂

funct7 commented 3 years ago

It's understandable. Thank you for the reply.