dave08
03/15/2018, 1:40 PMany
but the channel was closed before the offer
in the callback being wrapped...elizarov
03/15/2018, 1:41 PMsuspend fun awaitCancel(): Nothing = suspendCancellableCoroutine {}
dave08
03/15/2018, 1:41 PMelizarov
03/15/2018, 1:48 PMdave08
03/15/2018, 1:52 PMsuspend fun awaitCancel(): Nothing = suspendCancellableCoroutine {}
suspend fun registerBroadcastReceiver(context: Context, intentFilter: IntentFilter): ReceiveChannel<Intent> =
produce<Intent>(capacity = Channel.UNLIMITED) {
var broadcastReceiver:BroadcastReceiver? = null
try {
broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
offer(intent)
}
}.also {
context.registerReceiver(it, intentFilter)
awaitCancel()
}
} catch(e: CancellationException) {
context.unregisterReceiver(broadcastReceiver)
}
}
But it's still not suspending, it just continues on with the code...deviant
03/15/2018, 2:01 PMdelay(Long.MAX_LONG)
for such cases. probably not the best solution though 🙂elizarov
03/15/2018, 2:02 PMdelay(Long.MAX_LONG)
overflows or something like that (and does not wait)deviant
03/15/2018, 2:03 PMelizarov
03/15/2018, 2:04 PMdelay(Long.MAX_LONG)
separately and if it does not wait report a separate issue about it, pleasedeviant
03/15/2018, 2:05 PM/2
🙂elizarov
03/15/2018, 2:05 PMval deadline = curTime + timeout //MAX_LONG !!
// so deadline is negative
whiile (curTime < deadline) wait() // aha!!!
//
deviant
03/15/2018, 2:05 PMdave08
03/15/2018, 2:06 PMawaitCancel
... when we did it with delay it worked...?elizarov
03/15/2018, 2:06 PMsuspendCancellableCoroutine {}
should wait properly. No chance for it to wakeup, but on cancellationdave08
03/15/2018, 2:08 PMsuspend fun registerBroadcastReceiver(context: Context, intentFilter: IntentFilter): ReceiveChannel<Intent> =
produce<Intent>(capacity = Channel.UNLIMITED) {
var broadcastReceiver:BroadcastReceiver? = null
try {
broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
offer(intent)
}
}.also {
context.registerReceiver(it, intentFilter)
awaitCancel()
}
} finally {
context.unregisterReceiver(broadcastReceiver)
}
}
elizarov
03/15/2018, 2:09 PMdave08
03/15/2018, 2:45 PMelizarov
03/15/2018, 3:06 PMchannel.any
, then you try to send(“hello”) thereval result = async {
channel.any {
if (it.action == "hello") {
channel.cancel(); true
} else false
}
}
context.send("hello")
assert(result.await())
dave08
03/15/2018, 3:14 PM