zak.taccardi
06/12/2019, 2:38 PMactorConflatedBroadcastChannelFlowclass CountRepository: CoroutineScope by CoroutineScope(Dispatchers.Default) {
    private val stateChannel = ConflatedBroadcastChannel<Int>(0)
    
    private val actor = actor<Int>(capacity = Channel.UNLIMITED) {
        var count = stateChannel.value
        for (message in channel) {
            count += message
            
            stateChannel.send(count)
        }
    }
    fun states(): ReceiveChannel<Int> = stateChannel.openSubscription()
    fun increment() {
        actor.offer(1)
    }
    fun decrement() {
        actor.offer(-1)
    }
}Zach Klippenstein (he/him) [MOD]
06/12/2019, 4:34 PMfun states()Flow<Int>fun states(): Flow<Int> = stateChannel.asFlow()zak.taccardi
06/12/2019, 4:34 PMbloder
06/12/2019, 5:24 PMConflatedBroadcastChannelconsumeEachconsumeFlowcollectZach Klippenstein (he/him) [MOD]
06/12/2019, 5:28 PMconsumeEachFlow.collectcollectcollectopenSubscriptionbloder
06/12/2019, 5:35 PMfun states(): Flow<Int> = stateChannel.asFlow()states().collect { println(it) }stateChannelstateChannel.send(count)collectZach Klippenstein (he/him) [MOD]
06/12/2019, 6:11 PMcollectstateChannelcollectConflatedBroadcastChannelbloder
06/12/2019, 6:13 PMbohsen
06/13/2019, 6:24 AM