Skip to content

Commit 87fa2f5

Browse files
committed
chore(core): replace fixedRateTimer with coroutine polling in NetworkUpdater's
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent c40e627 commit 87fa2f5

4 files changed

Lines changed: 31 additions & 76 deletions

File tree

apps/flipcash/core/src/main/kotlin/com/flipcash/app/core/internal/updater/ProfileUpdater.kt

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,12 @@ package com.flipcash.app.core.internal.updater
22

33
import com.flipcash.app.core.updater.NetworkUpdater
44
import com.flipcash.services.controllers.ProfileController
5-
import kotlinx.coroutines.CoroutineScope
6-
import kotlinx.coroutines.coroutineScope
7-
import kotlinx.coroutines.launch
85
import javax.inject.Inject
9-
import kotlin.concurrent.fixedRateTimer
10-
import kotlin.time.Duration
116

127
class ProfileUpdater @Inject constructor(
138
private val profileController: ProfileController,
149
) : NetworkUpdater() {
15-
16-
override fun poll(
17-
key: Any?,
18-
scope: CoroutineScope,
19-
frequency: Duration,
20-
startIn: Duration
21-
) {
22-
stop()
23-
updater = fixedRateTimer(
24-
name = "update profile",
25-
initialDelay = startIn.inWholeMilliseconds,
26-
period = frequency.inWholeMilliseconds
27-
) {
28-
scope.launch {
29-
coroutineScope {
30-
profileController.updateUserProfile()
31-
}
32-
}
33-
}
10+
override suspend fun doUpdate() {
11+
profileController.updateUserProfile()
3412
}
35-
}
13+
}
Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
package com.flipcash.app.core.updater
22

33
import kotlinx.coroutines.CoroutineScope
4-
import java.util.Timer
4+
import kotlinx.coroutines.Job
5+
import kotlinx.coroutines.delay
6+
import kotlinx.coroutines.isActive
7+
import kotlinx.coroutines.launch
58
import kotlin.time.Duration
69
import kotlin.time.Duration.Companion.seconds
710

811
abstract class NetworkUpdater {
9-
protected var updater: Timer? = null
12+
private var job: Job? = null
1013

11-
abstract fun poll(
14+
protected abstract suspend fun doUpdate()
15+
16+
fun poll(
1217
key: Any? = null,
1318
scope: CoroutineScope,
1419
frequency: Duration,
1520
startIn: Duration = 0.seconds,
16-
)
17-
18-
open fun stop() {
19-
updater?.cancel()
20-
updater = null
21+
) {
22+
stop()
23+
job = scope.launch {
24+
delay(startIn)
25+
while (isActive) {
26+
doUpdate()
27+
delay(frequency)
28+
}
29+
}
30+
}
2131

32+
fun stop() {
33+
job?.cancel()
34+
job = null
2235
}
23-
}
36+
}
Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
package com.flipcash.app.activityfeed
22

33
import com.flipcash.app.core.updater.NetworkUpdater
4-
import kotlinx.coroutines.CoroutineScope
5-
import kotlinx.coroutines.launch
64
import javax.inject.Inject
7-
import kotlin.concurrent.fixedRateTimer
8-
import kotlin.time.Duration
95

106
class ActivityFeedUpdater @Inject constructor(
117
private val coordinator: ActivityFeedCoordinator,
128
): NetworkUpdater() {
13-
override fun poll(
14-
key: Any?,
15-
scope: CoroutineScope,
16-
frequency: Duration,
17-
startIn: Duration,
18-
) {
19-
stop()
20-
updater = fixedRateTimer(
21-
name = "update activity feed",
22-
initialDelay = startIn.inWholeMilliseconds,
23-
period = frequency.inWholeMilliseconds
24-
) {
25-
scope.launch {
26-
coordinator.fetchSinceLatest(count = 50)
27-
}
28-
}
9+
override suspend fun doUpdate() {
10+
coordinator.fetchSinceLatest(count = 50)
2911
}
30-
}
12+
}
Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
package com.flipcash.app.tokens
22

33
import com.flipcash.app.core.updater.NetworkUpdater
4-
import kotlinx.coroutines.CoroutineScope
5-
import kotlinx.coroutines.launch
64
import javax.inject.Inject
7-
import kotlin.concurrent.fixedRateTimer
8-
import kotlin.time.Duration
95

106
class TokenUpdater @Inject constructor(
117
private val coordinator: TokenCoordinator,
128
) : NetworkUpdater() {
13-
override fun poll(
14-
key: Any?,
15-
scope: CoroutineScope,
16-
frequency: Duration,
17-
startIn: Duration,
18-
) {
19-
stop()
20-
updater = fixedRateTimer(
21-
name = "update tokens",
22-
initialDelay = startIn.inWholeMilliseconds,
23-
period = frequency.inWholeMilliseconds
24-
) {
25-
scope.launch {
26-
coordinator.update()
27-
}
28-
}
9+
override suspend fun doUpdate() {
10+
coordinator.update()
2911
}
30-
}
12+
}

0 commit comments

Comments
 (0)