Skip to content

Commit 6725dec

Browse files
committed
channel coroutineScope fix
1 parent c1711de commit 6725dec

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

src/tasks/Request7Channels.kt

+17-25
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,33 @@
11
package tasks
22

3-
import contributors.GitHubService
4-
import contributors.RequestData
5-
import contributors.User
6-
import contributors.logRepos
3+
import contributors.*
74
import kotlinx.coroutines.channels.Channel
85
import kotlinx.coroutines.coroutineScope
9-
import samples.log
6+
import kotlinx.coroutines.launch
107

118
suspend fun loadContributorsChannels(
129
service: GitHubService,
1310
req: RequestData,
1411
updateResults: suspend (List<User>, completed: Boolean) -> Unit
15-
) {
16-
coroutineScope {
17-
val repos = service
18-
.getOrgRepos(req.org)
19-
.also { logRepos(req, it) }
20-
.bodyList()
12+
) = coroutineScope {
13+
val repos = service
14+
.getOrgRepos(req.org)
15+
.also { logRepos(req, it) }
16+
.bodyList()
2117

22-
var allUsers = emptyList<User>()
23-
val channel = Channel<List<User>>()
24-
25-
for ((index, repo) in repos.withIndex()) {
26-
log("for start")
18+
val channel = Channel<List<User>>()
19+
var allUsers = emptyList<User>()
20+
for (repo in repos) {
21+
launch {
2722
val users = service.getRepoContributors(req.org, repo.name)
28-
.also { log("repo: ${repo.name}") }
23+
.also { logUsers(repo, it) }
2924
.bodyList()
30-
log("users: ${users}")
3125
channel.send(users)
32-
log("after channel.send(users)")
33-
}
34-
repeat(repos.size) {
35-
val users = channel.receive()
36-
allUsers = (allUsers + users).aggregate()
37-
updateResults(allUsers, it == repos.lastIndex)
3826
}
39-
27+
}
28+
repeat(repos.size) {
29+
val users = channel.receive()
30+
allUsers = (allUsers + users).aggregate()
31+
updateResults(allUsers, it == repos.lastIndex)
4032
}
4133
}

0 commit comments

Comments
 (0)