Skip to content

Commit ac937fc

Browse files
authored
Merge pull request #747 from square/offer_->_trySend
replace use of `SendChannel.offer` with `SendChannel.trySend`
2 parents 81bdcb1 + 206d0e6 commit ac937fc

File tree

2 files changed

+4
-19
lines changed

2 files changed

+4
-19
lines changed

trace-encoder/src/main/java/com/squareup/tracing/TraceEncoder.kt

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package com.squareup.tracing
22

3-
import kotlinx.coroutines.CancellationException
43
import kotlinx.coroutines.CoroutineDispatcher
54
import kotlinx.coroutines.CoroutineScope
65
import kotlinx.coroutines.Dispatchers.IO
76
import kotlinx.coroutines.ObsoleteCoroutinesApi
87
import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED
9-
import kotlinx.coroutines.channels.ClosedSendChannelException
108
import kotlinx.coroutines.channels.SendChannel
119
import kotlinx.coroutines.channels.actor
1210
import kotlinx.coroutines.channels.consumeEach
@@ -74,7 +72,7 @@ public class TraceEncoder(
7472
val timestamp = getTimestampNow()
7573
val processNameEvent = createProcessNameEvent(processName, processId, timestamp)
7674
val threadNameEvent = createThreadNameEvent(threadName, processId, threadId, timestamp)
77-
events.safeOffer(listOf(processNameEvent, threadNameEvent))
75+
events.trySend(listOf(processNameEvent, threadNameEvent))
7876

7977
return object : TraceLogger {
8078
override fun log(eventBatch: List<TraceEvent>) = log(processId, threadId, eventBatch)
@@ -99,7 +97,7 @@ public class TraceEncoder(
9997
val chromeTraceEvents = eventBatch.map {
10098
it.toChromeTraceEvent(threadId, processId, timestampMicros)
10199
}
102-
events.safeOffer(chromeTraceEvents)
100+
events.trySend(chromeTraceEvents)
103101
}
104102

105103
internal fun log(
@@ -109,23 +107,10 @@ public class TraceEncoder(
109107
) {
110108
val timestampMicros = getTimestampNow()
111109
val chromeTraceEvents = event.toChromeTraceEvent(threadId, processId, timestampMicros)
112-
events.safeOffer(listOf(chromeTraceEvents))
110+
events.trySend(listOf(chromeTraceEvents))
113111
}
114112

115113
private fun getTimestampNow(): Long = start.elapsedNow
116-
117-
/**
118-
* Like [SendChannel.offer] but won't throw if the channel is closed.
119-
*/
120-
private fun <T> SendChannel<T>.safeOffer(value: T) {
121-
try {
122-
trySend(value)
123-
} catch (e: CancellationException) {
124-
// Ignore it.
125-
} catch (e: ClosedSendChannelException) {
126-
// Ignore it.
127-
}
128-
}
129114
}
130115

131116
/**

workflow-runtime/src/main/java/com/squareup/workflow1/internal/RealRenderContext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ internal class RealRenderContext<out PropsT, StateT, OutputT>(
4848
"Expected sink to not be sent to until after the render pass. Received action: $value"
4949
)
5050
}
51-
eventActionsChannel.offer(value)
51+
eventActionsChannel.trySend(value)
5252
}
5353

5454
override fun <ChildPropsT, ChildOutputT, ChildRenderingT> renderChild(

0 commit comments

Comments
 (0)