-
Notifications
You must be signed in to change notification settings - Fork 1
/
SummaryState.kt
32 lines (27 loc) · 931 Bytes
/
SummaryState.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
import io.sellmair.evas.State
import io.sellmair.evas.collectEventsAsync
import io.sellmair.evas.launchState
import kotlinx.coroutines.CoroutineScope
data class SummaryState(
val files: Int,
val directories: Int,
val size: Long,
) : State {
companion object Key : State.Key<SummaryState> {
override val default: SummaryState = SummaryState(files = 0, directories = 0, size = 0)
}
}
fun CoroutineScope.launchSummaryState() = launchState(SummaryState) {
var statistics = SummaryState.default
collectEventsAsync<DirectoryDiscoveredEvent> {
statistics = statistics.copy(directories = statistics.directories + 1)
statistics.emit()
}
collectEventsAsync<FileDiscoveredEvent> { event ->
statistics = statistics.copy(
files = statistics.files + 1,
size = statistics.size + (event.metadata.size ?: 0)
)
statistics.emit()
}
}