withoutclass
08/24/2018, 6:23 PMlouiscad
08/24/2018, 6:25 PMwithoutclass
08/24/2018, 6:25 PMlouiscad
08/24/2018, 6:26 PMwithoutclass
08/24/2018, 6:30 PMActivity
that alters it’s state/responds to information received from incoming bluetooth packets. I do not want to thrash the UI, so I want to read in all the packets, then delay some time before checking to see if there are more packets.internal suspend fun listenForPackets(packetBroadcastChannel: BroadcastChannel<Packet>,
activityActionChannel: SendChannel<Action>) {
val packetReceiveChannel = packetBroadcastChannel.openSubscription()
try {
while (isActive) {
delay(4000)
var count = 0
while (!packetReceiveChannel.isEmpty) {
packetReceiveChannel.receive()
count++
}
when (count) {
0 -> activityActionChannel.send(Action.DoThing)
}
}
} finally {
withContext(NonCancellable) {
packetReceiveChannel.cancel()
}
}
}
louiscad
08/24/2018, 6:35 PMpacketBroadcastChannel
parameter Activity bound or application bound in regards to its lifecycle?withoutclass
08/24/2018, 6:47 PMBroadcastChannel
, consuming each element, and caches them for some delay before producing them.use()
will also work which I did not knowopenSubscription().use {}
louiscad
08/24/2018, 8:56 PMopenSubscription()
returns a new ReceiveChannel
which you can consume
without closing the source BroadcastChannel
, so it should work for you in place of use
(and is more explicit regarding the consumption of a channel). What you are looking for though is probably some "debounce". See this gist by @kevinherron (and mind my comment on it to be sure you consume the channel properly 😉 ) https://gist.github.com/kevinherron/5a895911a45eb5752ff247a205d618b7kevinherron
08/24/2018, 9:01 PMconsumeEach
originally, but changed it while debugging and never switched back