-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.kt
188 lines (169 loc) · 5.64 KB
/
Main.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import com.mmolosay.resource.Resource
import com.mmolosay.resource.context.ResourceContext
import com.mmolosay.resource.invoke
import com.mmolosay.resource.resource
import com.mmolosay.resource.scope.ExhaustiveScope
import com.mmolosay.resource.scope.ResourceScope
import com.mmolosay.resource.scope.ResourceStateProducer
import com.mmolosay.resource.state.AbstractResourceState
import com.mmolosay.resource.state.Empty
import com.mmolosay.resource.state.Loading
import com.mmolosay.resource.state.ResourceState
import com.mmolosay.resource.with
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlin.random.Random
fun main() {
println("Enter desired demonstration.")
println("1: default resource context")
println("2: custom resource context")
while (true) {
when (readLine()?.toInt() ?: 1) {
1 -> {
println("Demonstrating default resource context:")
defaultContextSample()
break
}
2 -> {
println("Demonstration custom resource context:")
customContextSample()
break
}
else -> println("Invalid input. Try again.")
}
}
}
/**
* Demonstrates usage of default resource context.
*/
private fun defaultContextSample() =
runBlocking {
val flow = MutableStateFlow(resource<String>())
launch {
flow.collect { resource ->
resource.invoke(
onEmpty = { println("empty") },
onLoading = { println("loading") },
onSuccess = { println("success, data=$it") },
onFailure = { println("failure, cause=$it") }
)
}
}
getData(flow)
}
/**
* Demonstrates usage of custom resource context.
*/
private fun customContextSample() =
runBlocking {
val flow = MutableStateFlow(ReachingResource())
launch {
flow.collect { resource ->
resource.invoke(
onEmpty = { println("empty") },
onLoading = { println("connecting") },
onConnected = { elapsed -> println("success, connected after $elapsed ms") },
onConnectionError = { code -> println("connection error with code $code") }
)
}
}
flow.update { it with { loading() } }
// randomly connect successfully or get connection error
if (Random.nextBoolean()) {
delay(2_000L) // simulates connection is being established
flow.update { it with { connected(after = 2_000L) } }
} else {
delay(10_000L) // simulates connection is being established
flow.update { it with { connectionError(408) } } // connection timeout with corresponding code
}
}
/**
* Obtains data and propagates progress in specified [dest] flow.
*/
private suspend fun getData(dest: MutableStateFlow<Resource<String, ExhaustiveScope>>) {
dest.update { it with { loading() } }
try {
getDataAsync { data ->
dest.update { it with { success(data) } }
}
} catch (e: Exception) {
dest.update { it with { failure(e) } }
}
}
/**
* Simulates obtaining data asynchronously.
*/
private suspend fun getDataAsync(action: (data: String) -> Unit) {
delay(2_000L)
// randomly decide whether to return data or throw exception
if (Random.nextBoolean()) {
action("obtained data")
} else {
throw IllegalStateException("oops")
}
}
/**
* Custom state. Represents connected to remove data source state.
*/
data class Connected(val elapsedTimeMs: Long) : AbstractResourceState<Nothing>(Element) {
companion object Element : ResourceContext.Element
/**
* Produces instances of [Connected] state.
*/
interface Producer {
fun connected(after: Long): ResourceState<Nothing> =
Connected(after)
}
}
/**
* Custom state. Represents state of connecting failure with error [code].
*/
data class ConnectionError(val code: Int) : AbstractResourceState<Nothing>(Element) {
companion object Element : ResourceContext.Element
/**
* Produces instances of [ConnectionError] state.
*/
interface Producer {
fun connectionError(code: Int): ResourceState<Nothing> =
ConnectionError(code)
}
}
/**
* Custom scope of reaching to some remote component.
*/
object ReachingScope :
ResourceScope,
Empty.Producer,
Loading.Producer,
Connected.Producer,
ConnectionError.Producer {
override val context: ResourceContext =
Empty + Loading + Connected + ConnectionError
}
/**
* Builder function for custom scope with empty state by default.
* It uses `Nothing` as resource's data type, because we don't want any data.
*/
fun ReachingResource(
producer: ResourceStateProducer<ReachingScope, Nothing> = { empty() }
): Resource<Nothing, ReachingScope> =
Resource(ReachingScope, producer)
/**
* Extension for resource with custom scope.
*/
inline fun <V> Resource<V, ReachingScope>.invoke(
onEmpty: () -> Unit = {},
onLoading: () -> Unit = {},
onConnected: (elapsed: Long) -> Unit = {},
onConnectionError: (code: Int) -> Unit = {}
) =
when (val s = this.state) {
is Empty -> onEmpty()
is Loading -> onLoading()
is Connected -> onConnected(s.elapsedTimeMs)
is ConnectionError -> onConnectionError(s.code)
else -> throw IllegalStateException("unreachable in runtime")
}