Skip to content

Commit

Permalink
Add SNTPClient.blockingEpochTime
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyicker committed Jul 4, 2024
1 parent 6436e6c commit af99c84
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class SNTPClient(
@ObjCName(name = "epochTimeMs")
val epochTime by delegate::epochTime

/**
* The calculated epoch time, blocking until it has been calculated at least once.
*/
@ObjCName(name = "blockingEpochTimeMs")
suspend fun blockingEpochTime() = delegate.blockingEpochTime()

/**
* Starts periodic synchronization. If it's already started, it does nothing. Otherwise, it
* requests an immediate dispatch of a synchronization and subsequent ones
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ constructor(
return synchronizedTime - synchronizedAt + referenceClock.referenceEpochTime
}

suspend fun blockingEpochTime(): Duration {
epochTime?.let { return it }
synchronizationResultProcessor.firstSynchronizationLock
.lock()
return epochTime!!
}

fun enableSynchronization() = operationCoordinator.dispatchStartSync()

fun disableSynchronization() = operationCoordinator.dispatchStopSync()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tidal.networktime.internal

import kotlinx.coroutines.sync.Mutex
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.okio.decodeFromBufferedSource
Expand Down Expand Up @@ -30,6 +31,7 @@ internal class SynchronizationResultProcessor(
}
set(value) {
mutableState.synchronizationResult = value
firstSynchronizationLock.unlock()
if (backupFilePath != null && value != null) {
try {
fileSystem.write(backupFilePath) {
Expand All @@ -39,4 +41,6 @@ internal class SynchronizationResultProcessor(
}
}
}

val firstSynchronizationLock = Mutex(locked = true)
}

0 comments on commit af99c84

Please sign in to comment.