Skip to content

Commit

Permalink
Dispatch periodic sync
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyicker committed Aug 14, 2023
1 parent 68e7384 commit 31ea6e9
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.time.Duration

internal class OperationCoordinator
@OptIn(ExperimentalCoroutinesApi::class)
constructor(
private val mutableState: MutableState,
private val coroutineScope: CoroutineScope,
globalDispatcher: CoroutineDispatcher,
private val privateDispatcher: CoroutineDispatcher = globalDispatcher.limitedParallelism(1),
private val syncInterval: Duration,
private val toggleDispatcher: CoroutineDispatcher = globalDispatcher.limitedParallelism(1),
private val syncDispatcher: CoroutineDispatcher = globalDispatcher.limitedParallelism(1),
) {

fun dispatchStartSync() = dispatch(StartSyncRunnable(mutableState))
fun dispatchStartSync() = dispatch(
SyncEnable(mutableState, coroutineScope, syncDispatcher, syncInterval),
)

fun dispatchStopSync() = dispatch(StopSyncRunnable(mutableState))
fun dispatchStopSync() = dispatch(SyncDisable(mutableState))

private fun dispatch(block: () -> Unit) = coroutineScope.launch {
withContext(privateDispatcher) { block() }
}
private fun dispatch(block: () -> Unit) = coroutineScope.launch(toggleDispatcher) { block() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

internal class PlatformAgnosticSNTPClient(
val ntpServers: Array<out NTPServer>,
val referenceClock: ReadableClock,
val coroutineScope: CoroutineScope,
val synchronizationInterval: Duration = 64.seconds,
private val ntpServers: Array<out NTPServer>,
private val referenceClock: ReadableClock,
private val coroutineScope: CoroutineScope,
private val syncInterval: Duration = 64.seconds,
private val httpClient: HttpClient = HttpClientFactory()(),
private val operationCoordinator: OperationCoordinator =
OperationCoordinator(MutableState(), coroutineScope, Dispatchers.IO),
OperationCoordinator(MutableState(), coroutineScope, Dispatchers.IO, syncInterval),
) {
val synchronizedEpochTime: Duration?
get() = TODO("Get the time")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.tidal.networktime.internal

internal class StopSyncRunnable(private val mutableState: MutableState) : () -> Unit {
internal class SyncDisable(private val mutableState: MutableState) : () -> Unit {
override operator fun invoke() = with(mutableState) {
val job = job ?: return
if (!job.isCancelled) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.tidal.networktime.internal

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlin.time.Duration

internal class SyncEnable(
private val mutableState: MutableState,
private val coroutineScope: CoroutineScope,
private val syncDispatcher: CoroutineDispatcher,
private val syncInterval: Duration,
) : () -> Unit {
override operator fun invoke() = with(mutableState) {
val job = job
if (job != null && !job.isCancelled) {
return
}
this.job = coroutineScope.launch(syncDispatcher) { SyncPeriodic(syncInterval)() }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.tidal.networktime.internal

import kotlinx.coroutines.delay
import kotlin.time.Duration

internal class SyncPeriodic(private val syncInterval: Duration) {
suspend operator fun invoke() {
while (true) {
SyncSingular()()
delay(syncInterval)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tidal.networktime.internal

internal class SyncSingular {
suspend operator fun invoke() {
TODO()
}
}

0 comments on commit 31ea6e9

Please sign in to comment.