-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Coroutines.kt
144 lines (123 loc) · 4.22 KB
/
Coroutines.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package io.ktor.utils.io
import kotlinx.coroutines.*
import kotlin.coroutines.*
/**
* A coroutine job that is reading from a byte channel
*/
interface ReaderJob : Job {
/**
* A reference to the channel that this coroutine is reading from
*/
val channel: ByteWriteChannel
}
/**
* A coroutine job that is writing to a byte channel
*/
interface WriterJob : Job {
/**
* A reference to the channel that this coroutine is writing to
*/
val channel: ByteReadChannel
}
interface ReaderScope : CoroutineScope {
val channel: ByteReadChannel
}
interface WriterScope : CoroutineScope {
val channel: ByteWriteChannel
}
fun CoroutineScope.reader(
coroutineContext: CoroutineContext = EmptyCoroutineContext,
channel: ByteChannel,
block: suspend ReaderScope.() -> Unit
): ReaderJob = launchChannel(coroutineContext, channel, attachJob = false, block = block)
fun CoroutineScope.reader(
coroutineContext: CoroutineContext = EmptyCoroutineContext,
autoFlush: Boolean = false,
block: suspend ReaderScope.() -> Unit
): ReaderJob = launchChannel(coroutineContext, ByteChannel(autoFlush), attachJob = true, block = block)
@Deprecated("Use scope.reader instead")
fun reader(
coroutineContext: CoroutineContext,
channel: ByteChannel,
parent: Job? = null,
block: suspend ReaderScope.() -> Unit
): ReaderJob {
val newContext = if (parent != null) GlobalScope.newCoroutineContext(coroutineContext + parent)
else GlobalScope.newCoroutineContext(coroutineContext)
return CoroutineScope(newContext).reader(EmptyCoroutineContext, channel, block)
}
@Suppress("DEPRECATION")
@Deprecated("Use scope.reader instead")
fun reader(
coroutineContext: CoroutineContext,
autoFlush: Boolean = false, parent: Job? = null,
block: suspend ReaderScope.() -> Unit
): ReaderJob {
val channel = ByteChannel(autoFlush)
return reader(coroutineContext, channel, parent, block).also {
channel.attachJob(it)
}
}
fun CoroutineScope.writer(
coroutineContext: CoroutineContext = EmptyCoroutineContext,
channel: ByteChannel,
block: suspend WriterScope.() -> Unit
): WriterJob = launchChannel(coroutineContext, channel, attachJob = false, block = block)
fun CoroutineScope.writer(
coroutineContext: CoroutineContext = EmptyCoroutineContext,
autoFlush: Boolean = false,
block: suspend WriterScope.() -> Unit
): WriterJob = launchChannel(coroutineContext, ByteChannel(autoFlush), attachJob = true, block = block)
@Deprecated("Use scope.writer instead")
fun writer(
coroutineContext: CoroutineContext,
channel: ByteChannel, parent: Job? = null,
block: suspend WriterScope.() -> Unit
): WriterJob {
val newContext = if (parent != null) GlobalScope.newCoroutineContext(coroutineContext + parent)
else GlobalScope.newCoroutineContext(coroutineContext)
return CoroutineScope(newContext).writer(EmptyCoroutineContext, channel, block)
}
@Suppress("DEPRECATION")
@Deprecated("Use scope.writer instead")
fun writer(
coroutineContext: CoroutineContext,
autoFlush: Boolean = false, parent: Job? = null,
block: suspend WriterScope.() -> Unit
): WriterJob {
val channel = ByteChannel(autoFlush)
return writer(coroutineContext, channel, parent, block).also {
channel.attachJob(it)
}
}
/**
* @param S not exactly safe (unchecked cast is used) so should be [ReaderScope] or [WriterScope]
*/
private fun <S : CoroutineScope> CoroutineScope.launchChannel(
context: CoroutineContext,
channel: ByteChannel,
attachJob: Boolean,
block: suspend S.() -> Unit
): ChannelJob {
val job = launch(context) {
if (attachJob) {
channel.attachJob(coroutineContext[Job]!!)
}
@Suppress("UNCHECKED_CAST")
block(ChannelScope(this, channel) as S)
}
job.invokeOnCompletion { cause ->
channel.close(cause)
}
return ChannelJob(job, channel)
}
private class ChannelScope(
delegate: CoroutineScope,
override val channel: ByteChannel
) : ReaderScope, WriterScope, CoroutineScope by delegate
private class ChannelJob(
private val delegate: Job,
override val channel: ByteChannel
) : ReaderJob, WriterJob, Job by delegate {
override fun toString(): String = "ChannelJob[$delegate]"
}