From fd60e702c17e4af593fc60541fbb306c8db842a0 Mon Sep 17 00:00:00 2001 From: Josh Kasten Date: Thu, 21 Mar 2024 15:52:26 -0400 Subject: [PATCH] Improve Waiter to not block when calling wake We don't want to do any waiting when we call wake, so use trySend instead. Also added result checking, to ensure we throw instead of silently handling the error so we know if there is an issue at any time. --- .../com/onesignal/common/threading/Waiter.kt | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/threading/Waiter.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/threading/Waiter.kt index b6dcdfff2e..fd5fda83a1 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/threading/Waiter.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/common/threading/Waiter.kt @@ -1,7 +1,6 @@ package com.onesignal.common.threading import kotlinx.coroutines.channels.Channel -import kotlinx.coroutines.runBlocking /** * An abstraction which allows for a suspending function to coordinate @@ -18,7 +17,13 @@ class Waiter { /** * Wake the suspending function that has called [waitForWake]. */ - fun wake() = runBlocking { channel.send(null) } + fun wake() { + val result = channel.trySend(null) + if (result.isFailure) { + // Most likely only happens when the chanel is misconfigured or misused + throw Exception("Waiter.wait failed", result.exceptionOrNull()) + } + } } /** @@ -40,5 +45,11 @@ open class WaiterWithValue { * * @param value The data to be returned by the [waitForWake]. */ - fun wake(value: TType) = runBlocking { channel.send(value) } + fun wake(value: TType) { + val result = channel.trySend(value) + if (result.isFailure) { + // Most likely only happens when the chanel is misconfigured or misused + throw Exception("WaiterWithValue.wait failed", result.exceptionOrNull()) + } + } }