override suspend fun receive()

in hot-reload-core/src/main/kotlin/org/jetbrains/compose/reload/core/Queue.kt [77:101]


    override suspend fun receive(): T {
        val sender = lock.withLock { senders.removeFirstOrNull() }
        /* Fast path: We have a suspended sender: Let's take the value from there */
        if (sender != null) {
            sender.continuation?.resume(Unit)
            return sender.value
        }

        return suspendStoppableCoroutine { continuation ->
            val senderOrNull = lock.withLock {
                /* Double Check: Until this point a new sender could be available! */
                if (senders.isNotEmpty()) {
                    return@withLock senders.removeFirst()
                }

                receivers.addLast(SuspendedReceiver(continuation))
                null
            }

            if (senderOrNull != null) {
                senderOrNull.continuation?.resume(Unit)
                continuation.resume(senderOrNull.value)
            }
        }.getOrThrow()
    }